Java 如何返回两个列表之间的差异?

Java 如何返回两个列表之间的差异?,java,list,object,filtering,Java,List,Object,Filtering,我有两个数组列表,例如 List<Date> a; contains : 10/10/2014, 10/11/2016 List<Date> b; contains : 10/10/2016 列表a; 包含:2014年10月10日、2016年11月10日 名单b; 包含:10/10/2016 如何在列表a和b之间进行检查,以返回b中缺少的值?例如10/10/2014您可以将它们转换为集合集并对其执行集合差异操作 像这样: Set<Date> ad = n

我有两个数组列表,例如

List<Date> a;
contains : 10/10/2014, 10/11/2016

List<Date> b;
contains : 10/10/2016
列表a;
包含:2014年10月10日、2016年11月10日
名单b;
包含:10/10/2016

如何在列表
a
b
之间进行检查,以返回
b
中缺少的值?例如
10/10/2014

您可以将它们转换为
集合集
并对其执行集合差异操作

像这样:

Set<Date> ad = new HashSet<Date>(a);
Set<Date> bd = new HashSet<Date>(b);
ad.removeAll(bd);
Set ad=新哈希集(a);
Set bd=新哈希集(b);
移动电话(bd);

如果您只想查找b中缺少的值,可以执行以下操作:

List toReturn = new ArrayList(a);
toReturn.removeAll(b);

return toReturn;

如果您想找出出现在任一列表中的值,可以执行两次上面的代码。更改列表。

我正在寻找一个不同的问题,遇到了这个问题,因此我将添加一个相关问题的解决方案:比较两个地图

    // make a copy of the data
    Map<String,String> a = new HashMap<String,String>(actual);
    Map<String,String> e = new HashMap<String,String>(expected);
    // check *every* expected value
    for(Map.Entry<String, String> val : e.entrySet()){
        // check for presence
        if(!a.containsKey(val.getKey())){
            System.out.println(String.format("Did not find expected value: %s", val.getKey()));
        }
        // check for equality
        else{
            if(0 != a.get(val.getKey()).compareTo(val.getValue())){
                System.out.println(String.format("Value does not match expected: %s", val.getValue()));
            }
            // we have found the item, so remove it 
            // from future consideration. While it 
            // doesn't affect Java Maps, other types of sets
            // may contain duplicates, this will flag those
            // duplicates. 
            a.remove(val.getKey());
        }
    }
    // check to see that we did not receive extra values
    for(Map.Entry<String,String> val : a.entrySet()){
        System.out.println(String.format("Found unexpected value: %s", val.getKey()));
    }
//复制数据
映射a=新的HashMap(实际);
Map e=新的HashMap(预期);
//检查*每个*期望值
对于(Map.Entry val:e.entrySet()){
//检查是否存在
如果(!a.containsKey(val.getKey())){
System.out.println(String.format(“未找到预期值:%s”,val.getKey());
}
//检查是否相等
否则{
如果(0!=a.get(val.getKey()).compareTo(val.getValue()){
System.out.println(String.format(“值与预期值不匹配:%s”,val.getValue());
}
//我们已找到该项目,请将其删除
//从未来的考虑。虽然它
//不影响Java映射和其他类型的集合
//可能包含重复项,这将标记这些
//复制品。
a、 移除(val.getKey());
}
}
//检查以确保我们没有收到额外的值
对于(Map.Entry val:a.entrySet()){
System.out.println(String.format(“找到意外值:%s”,val.getKey());
}

它的工作原理与其他解决方案相同,而且不仅比较存在的值,还比较它们包含的值是否相同。在比较两个来源(员工和经理输入的值匹配;客户和公司交易匹配;等等)的数据时,我通常在会计软件中使用此选项。

您可以从以下来源使用CollectionUtils:

newarraylist(CollectionUtils.subtract(a,b))

您可以在Java 8
库中使用
过滤器

List<String> aList = List.of("l","e","t","'","s");
List<String> bList = List.of("g","o","e","s","t");

List<String> difference = aList.stream()
    .filter(aObject -> {
        return ! bList.contains(aObject);
      })
    .collect(Collectors.toList());

//more reduced: no curly braces, no return
List<String> difference2 = aList.stream()
    .filter(aObject -> ! bList.contains(aObject))
    .collect(Collectors.toList());
List aList=List.of(“l”、“e”、“t”、“s”);
列表bList=列表中的(“g”、“o”、“e”、“s”、“t”);
列表差异=aList.stream()
.filter(AOObject->{
return!bList.contains(AOObject);
})
.collect(Collectors.toList());
//更多减少:没有花括号,没有回报
列表差异2=aList.stream()
.filter(AOObject->!bList.contains(AOObject))
.collect(Collectors.toList());
System.out.println的结果(差异)

[e,t,s]


您可以调用库中的
U.difference(list)
方法。我是项目的维护者

import com.github.underline.U;
导入java.util.array;
导入java.util.List;
公共班机{
公共静态void main(字符串[]args){
list1=Arrays.asList(1,2,3);
list2=Arrays.asList(1,2);
列表3=U.差异(列表1,列表2);
System.out.println(列表3);
// [3]
}
}

我看起来很相似,但我希望在两个列表中有所不同(两个列表之间的不常见元素)

假设我有:

List<String> oldKeys = Arrays.asList("key0","key1","key2","key5");
List<String> newKeys = Arrays.asList("key0","key2","key5", "key6");
结果

["key1", "key6"]

首先将列表转换为集合

// create an empty set 
Set<T> set = new HashSet<>(); 

// Add each element of list into the set 
for (T t : list) 
    set.add(t); 
//创建一个空集
Set=newhashset();
//将列表中的每个元素添加到集合中
对于(T:列表)
增加(t);
您可以使用
Sets.difference(Set1,Set2)
,它返回Set1中存在的额外项目。

您可以使用
Sets.difference(Set2,Set1)
,它返回Set2中存在的额外项。

这是解决此问题的通用方法

List<String> l1 = new ArrayList<String>();
l1.add("apple");
l1.add("orange");
l1.add("banana");
l1.add("strawberry");

List<String> l2 = new ArrayList<String>();
l2.add("apple");
l2.add("orange");

System.out.println(l1);
System.out.println(l2);

for (String A: l2) {
  if (l1.contains(A))
    l1.remove(A);
}

System.out.println("output");
System.out.println(l1);
public <T> List<T> difference(List<T> first, List<T> second) {
    List<T> toReturn = new ArrayList<>(first);
    toReturn.removeAll(second);
    return toReturn;
}
公共列表差异(先列表,后列表){
List toReturn=新阵列列表(第一);
toReturn.removeAll(第二);
回归回归;
}

使用流API,您可以执行以下操作:

List aWithoutB=a.stream()
.过滤器(元件->!b.包含(元件))
.collect(Collectors.toList());
列表b不带A=b.stream()
.过滤器(元件->!a.包含(元件))
.collect(Collectors.toList());

guava有一些不同的工具,但它是用于
Set
,我认为
removeAll
可能适合您复制列表
a
。从副本中删除出现在
b
中的所有元素。如果希望两者之间的分离,CollectionUtil有一个返回差异的分离方法。这可能是指
CollectionUtils。从Apache Commons集合中减去
;有关更多详细信息,请参阅。
removeAll
返回一个正确的
boolean
而不是
Set
,但当您查看文档时,您将看到此boolean通知是否“此集合因调用而更改”。在此更改之后,您可以对集合进行寻址,您会注意到它具有预期的结果。不需要创建
bd
HashSet
。您只需调用
ad.removeAll(b)
。如果bd的元素比ad多怎么办?在这种情况下会发生什么?如果它是不可修改的列表呢?既然你创建了一个新的列表,这也适用于不可修改的列表。优雅但简单,谢谢你。我不知道有关于地图的问题。如果您发现一个关于地图的问题,请随时链接到此答案。答案应针对所问问题。如果您认为您有一个有用的代码片段/解释,可以在不存在问题的情况下自己提问,并使用内置的站点功能自行回答。不要把你的答案贴在一个无关的问题下。我的答案和这个问题的区别不在于用途
// create an empty set 
Set<T> set = new HashSet<>(); 

// Add each element of list into the set 
for (T t : list) 
    set.add(t); 
List<String> l1 = new ArrayList<String>();
l1.add("apple");
l1.add("orange");
l1.add("banana");
l1.add("strawberry");

List<String> l2 = new ArrayList<String>();
l2.add("apple");
l2.add("orange");

System.out.println(l1);
System.out.println(l2);

for (String A: l2) {
  if (l1.contains(A))
    l1.remove(A);
}

System.out.println("output");
System.out.println(l1);
[apple, orange, banana, strawberry]
[apple, orange]
output
[banana, strawberry]
public <T> List<T> difference(List<T> first, List<T> second) {
    List<T> toReturn = new ArrayList<>(first);
    toReturn.removeAll(second);
    return toReturn;
}