我有两个数组列表,例如:
List<Date> a;
contains : 10/10/2014, 10/11/2016
List<Date> b;
contains : 10/10/2016
如何对比列表a
和b
,以便返回在b
中缺失的值?例如:10/10/2014
Set
集合,然后执行一个集合差异操作。Set<Date> ad = new HashSet<Date>(a);
Set<Date> bd = new HashSet<Date>(b);
ad.removeAll(bd);
removeAll
返回一个布尔值,而非一个集合。 - Lukazoidbd
HashSet
是不必要的。你只需要调用ad.removeAll(b)
即可。 - SlavaSt如果你只想在 b 中查找缺失值,可以执行以下操作:
List toReturn = new ArrayList(a);
toReturn.removeAll(b);
return toReturn;
如果您想查找存在于任一列表中的值,可以执行上面的代码两次。使用更改后的列表。
我想寻找类似的项目,但我希望找到两个列表之间不同的元素(即这两个列表中独特的元素)。
假设我有:
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<>(CollectionUtils.disjunction(newKeys, oldKeys));
结果
["key1", "key6"]
https://mvnrepository.com/artifact/org.apache.commons/commons-collections4/4.4
但似乎不是正确的。 - paradocsloverStream
库中使用filter
。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());
< p > System.out.println(difference);
的输出结果:
< blockquote >
< p >[e, t, s]
< /blockquote >
new ArrayList<>(CollectionUtils.subtract(a, b))
public static <O> Collection<O> subtract(Iterable<? extends O> a, Iterable<? extends O> b)
,根据文档说明:“O - 泛型类型,能够表示输入集合中包含的类型。” - contrapostpublic static Collection subtract(final Collection a, final Collection b)
签名。很高兴看到在 4.0 版本中对此进行了改进。 - Per Lundberg首先将列表转换为集合。
// create an empty set
Set<T> set = new HashSet<>();
// 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中存在的额外项。
List<String> aWithoutB = a.stream()
.filter(element -> !b.contains(element))
.collect(Collectors.toList());
List<String> bWithoutA = b.stream()
.filter(element -> !a.contains(element))
.collect(Collectors.toList());
以下是这个问题的通用解决方案。
public <T> List<T> difference(List<T> first, List<T> second) {
List<T> toReturn = new ArrayList<>(first);
toReturn.removeAll(second);
return toReturn;
}
您可以在underscore-java库中调用Underscore.difference(lists)
方法。实时示例
import com.github.underscore.Underscore;
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 = Underscore.difference(list1, list2);
System.out.println(list3);
// [3]
}
}
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]
Set
的,我认为在这里使用removeAll
可能更合适。 - user180100a
。从副本中删除所有在b
中出现的元素。 - Sotirios DelimanolisCollectionUtils.subtract
; 有关更多详细信息,请参见此答案。 - Per Lundberg