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

例如,我有两个阵列列表


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

List<date> b;
contains : 10/10/2016


如何在列表之间检查
a


b

, 返回丢失的值
b

?例如
10/10/2014


</date></date>
已邀请:

龙天

赞同来自:

您可以将它们转换为集合
Set

并对他们进行手术 set difference.

类似于这个:


Set<date> ad = new HashSet<date>/a/;
Set<date> bd = new HashSet<date>/b/;
ad.removeAll/bd/;


</date></date></date></date>

喜特乐

赞同来自:

如果您只想找到缺少的值 b, 你能做到吗:


List toReturn = new ArrayList/a/;
toReturn.removeAll/b/;

return toReturn;


如果您想知道任何列表中存在的值,则可以执行两次顶级代码。 更改列表。

诸葛浮云

赞同来自:

我看起来像,但我需要任何名单的差异 /每个不寻常的元素 2 清单/:

让我说我有:


List<string> oldKeys = Arrays.asList/"key0","key1","key2","key5"/;
List<string> newKeys = Arrays.asList/"key0","key2","key5", "key6"/;


而且我想知道添加了哪个键,删除了哪个键,即我想获得

/key1, key6

/

使用

org.apache.commons.collections.CollectionUtils


List<string> list = new ArrayList&lt;&gt;/CollectionUtils.disjunction/newKeys, oldKeys//;


结果

[key1,key6]
</string></string></string>

江南孤鹜

赞同来自:

您可以使用流媒体库过滤器 Java 8


List<string> aList = Arrays.asList/"l","e","t","'","s"/;
List<string> bList = Arrays.asList/"g","o","e","s","t"/;

List<string> result = aList.stream//.filter/aObject -&gt; {
return bList.contains/aObject/;
}/.collect/Collectors.toList///;

//or more reduced without curly braces and return
List<string> result2 = aList.stream//.filter/aObject -&gt;
!bList.contains/aObject//.collect/Collectors.toList///;

System.out.println/result/;


结果:


[e, t, s]


</string></string></string></string>

奔跑吧少年

赞同来自:

您可以使用 CollectionUtils 的
https://commons.apache.org/pro ... .html
:


new ArrayList<>/CollectionUtils.subtract/a, b//

涵秋

赞同来自:

首先将列表转换为集。


// create an empty set 
Set<t> set = new HashSet&lt;&gt;//;

// Add each element of list into the set
for /T t : list/
set.add/t/;


您可以使用
Sets.difference/Set1, Set2/

, 返回出现的其他物品 Set1.

您可以使用
Sets.difference/Set2, Set1/

, 返回出现的其他物品 Set2.
</t>

石油百科

赞同来自:

你可以调用该方法
U.difference/lists/

在图书馆里
https://github.com/javadev/underscore-java
. 我是一个随附的项目。
https://www.jdoodle.com/embed/v0/tIG

import com.github.underscore.U;
import java.util.Arrays;
import java.util.List;

public class Main {
public static void main/String[] args/ {
List<integer> list1 = Arrays.asList/1, 2, 3/;
List<integer> list2 = Arrays.asList/1, 2/;
List<integer> list3 = U.difference/list1, list2/;
System.out.println/list3/;
// [3]
}
}


</integer></integer></integer>

裸奔

赞同来自:

这是这个问题的一般解决方案。


public <t> List<t> difference/List<t> first, List<t> second/ {
List<t> toReturn = new ArrayList&lt;&gt;/first/;
toReturn.removeAll/second/;
return toReturn;
}


</t></t></t></t></t>

喜特乐

赞同来自:

我正在寻找另一个问题并遇到它,所以我会向相关问题添加我的决定:两张牌的比较。


// 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////;
}


它与其他解决方案相同的原则工作,而且还比较了值存在,还可以比较它们包含相同的含义。 基本上,在从两个来源的数据比较数据时,我在会计软件中使用了这一点。 /员工和经理输入价值的巧合; 客户和公司交易的巧合; ... 等等/
</string,string></string,></string,string></string,string></string,string></string,string>

郭文康

赞同来自:

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]


</string></string></string></string>

要回复问题请先登录注册