Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java get不包含列表b中列表a的对象_Java_List - Fatal编程技术网

Java get不包含列表b中列表a的对象

Java get不包含列表b中列表a的对象,java,list,Java,List,有没有更好的方法来做到这一点-它就是这样一个样板代码。 我使用Java8,我会用streams来实现这一点,但我需要一些帮助来实现这一点。我用…removeIf()试过,但不起作用 final List<CalendarEventUserConnection> calendarEventUserConnectionsToDelete = new ArrayList<>(); for (final CalendarEventUserConnection existi

有没有更好的方法来做到这一点-它就是这样一个样板代码。 我使用Java8,我会用streams来实现这一点,但我需要一些帮助来实现这一点。我用…removeIf()试过,但不起作用

final List<CalendarEventUserConnection> calendarEventUserConnectionsToDelete = new ArrayList<>();
    for (final CalendarEventUserConnection existingCalendarEventUserConnection : existingCalendarEventUserConnections) {
        boolean contains = false;
        for (final CalendarEventUserConnection newCalendarEventUserConnection : calendarEventUserConnections) {
            if (newCalendarEventUserConnection.getId() != null
                && newCalendarEventUserConnection.getId().equals(existingCalendarEventUserConnection.getId())) {
                contains = true;
            }
        }
        if (contains == false) {
            calendarEventUserConnectionsToDelete.add(existingCalendarEventUserConnection);
        }
    }
final List calendarEventUserConnectionsToDelete=new ArrayList();
对于(最终CalendarEventUserConnection existingCalendarEventUserConnection:existingCalendarEventUserConnections){
布尔包含=假;
对于(最终CalendarEventUserConnection newCalendarEventUserConnection:calendarEventUserConnections){
if(newCalendarEventUserConnection.getId()!=null
&&newCalendarEventUserConnection.getId().equals(existingCalendarEventUserConnection.getId()){
包含=真;
}
}
if(contains==false){
calendarEventUserConnectionsToDelete.add(现有CalendarEventUserConnection);
}
}

您可以将其简化。看起来您正在筛选一个列表,以查看另一个列表中是否有与之匹配的内容,并在另一个列表中收集结果

所以你可以使用,和

final List todelite=existingCalendarEventUserConnections.stream()
.filter(c->!calendarEventUserConnections.stream()
.map(CalendarEventUserConnection::getId)
.anyMatch(id->id!=null&&id.equals(c.getId()))
.collect(Collectors.toList());

如果要获取listA上而不是listB上的所有对象

public static <T> List<T> aNotB(List<T> listA, List<T> listB) {

    List<T> result = new ArrayList(listA);
    result.removeAll(listB);

    return result;
}
公共静态列表aNotB(列表A、列表B){
列表结果=新的ArrayList(listA);
结果:removeAll(列表B);
返回结果;
}
只有在正确执行了
T
equals
方法时,此方法才有效。

您自己的搜索是O(NxM),其中N是一个列表中的元素数,M是另一个列表中的元素数

我建议将
calendarEventUserConnections
中的所有ID收集到一个集合中

然后,您可以将
existingCalendarEventUserConnections
中ID位于该集中的所有元素收集到删除列表中

假设您的ID是字符串,这类似于:

Set<String> idsToDelete = calendarEventUserConnections.stream()
                          .map( CalendarEventUserConnection::getId )
                          .filter( Objects::nonNull )
                          .collect(Collectors.toCollection(HashSet::new));
List<CalendarEventUserConnection> connectionsToDelete =
                          existingCalendarEventUserConnections.stream()
                          .filter( idsToDelete::contains )
                          .collect(Collectors.toList());
Set idsToDelete=calendarEventUserConnections.stream()
.map(CalendarEventUserConnection::getId)
.filter(对象::非空)
.collect(Collectors.toCollection(HashSet::new));
列表连接删除=
existingCalendarEventUserConnections.stream()
.filter(idsToDelete::contains)
.collect(Collectors.toList());
(代码未经测试)


考虑到您使用的是
哈希集
,这将把复杂性降低到O(M+N)而不是O(MxN)

有没有更好的方法来问这个问题?它几乎不包含任何信息。这样做-什么是“这”?如果你不能解释,我们当然不会。不要使用超过20个字符的变量名。不要使用
==false
。这就是<代码>运算符适用于。@StephenC:“==false”是合法的。!=“true”增加了一点复杂性。至少这与风格有关。@Chris311
contains==false
-->
!包含
@assylas好主意。感谢结果取决于如何实现
equals
,这可能与问题中的逻辑不同。但在这方面,问题非常开放。。。和其他人。。。我假设
equals
的实现是正确的。这个问题比较了对象的ID,这意味着实际的对象甚至可能没有覆盖
equals
。@realponsign-可能没有,可能有-OP没有解释他/她真正需要什么,顺便说一句,假设具有相同ID的两个对象必须被视为相等,这并不奇怪
Set<String> idsToDelete = calendarEventUserConnections.stream()
                          .map( CalendarEventUserConnection::getId )
                          .filter( Objects::nonNull )
                          .collect(Collectors.toCollection(HashSet::new));
List<CalendarEventUserConnection> connectionsToDelete =
                          existingCalendarEventUserConnections.stream()
                          .filter( idsToDelete::contains )
                          .collect(Collectors.toList());