简体   繁体   English

如何在Java中检查两个集合之间差异的有效方法

[英]Efficient way how to check differences between two collections in Java

I am having a List of strings {1,2,7}我有一个字符串List {1,2,7}

Now I have a new List : {2,4,9}现在我有一个新List{2,4,9}

Rules are simple:规则很简单:

  1. if element on List A doesn't exist on List B we add it to new delete-list如果列表 A 上的元素在列表 B 中不存在,我们将其添加到新的删除列表中
  2. if element on List B doesn't exist on List A we add it to new create-list如果列表 B 上的元素在列表 A 中不存在,我们将其添加到新的创建列表
  3. if element exists on both lists we don't touch it如果元素在两个列表中都存在,我们就不要碰它

so the outcome is two new lists:所以结果是两个新列表:

delete list: {1,7}删除列表: {1,7}

create list: {4,9}创建列表: {4,9}

Any idea how to make it efficient within java?知道如何在java中使其高效吗? perhaps using Java8 to make it easier?也许使用Java8使它更容易?

My lists have about 1000 elements.我的列表有大约 1000 个元素。

delete_list = A.copy();
create_list = B.copy();
delete_list.stream().removeIf(e -> B.contains(e));
create_list.stream().removeIf(e -> A.contains(e));

Basically we just create two distinct list.基本上我们只是创建两个不同的列表。 In set theory we can write the output like this:在集合论中,我们可以这样写输出:

delete_list = A \\ B delete_list = A \\ B
create_list = B \\ A create_list = B\\A

Thanks @SashaSalauyou for calling my attention on a mistake i made.感谢@SashaSalauyou 提醒我注意我犯的错误。

If ordering is not important, use Set bulk operations:如果排序不重要,请使用Set批量操作:

Set<String> a = new HashSet<>(Arrays.asList("1", "2", "7"));
Set<String> b = new HashSet<>(Arrays.asList("2", "4", "9"));

Set<String> deleteSet = new HashSet<>(a);
deleteSet.removeAll(b);                   // [1, 7]
Set<String> createSet = new HashSet<>(b);
createSet.removeAll(a);                   // [4, 9]

If you want Java 8, just filter elements and collect:如果您想要 Java 8,只需过滤元素并收集:

List<String> deleteList = a.stream().filter(e -> !b.contains(e)).collect(Collectors.toList());
List<String> createList = b.stream().filter(e -> !a.contains(e)).collect(Collectors.toList());

Here, a and b could be lists, but it is highly recommended to make them Set s since contains() operation is O(n) in most list implementations.这里, ab可以是列表,但强烈建议将它们Set s,因为在大多数列表实现中contains()操作是 O(n)。

Though not very elegant solution but we can also use Streams in Java8:虽然不是很优雅的解决方案,但我们也可以在 Java8 中使用Streams

Set<String> setOne = new HashSet<>(Arrays.asList("1","2","7"));
Set<String> setTwo = new HashSet<>(Arrays.asList("2","4","9"));
System.out.println(setOne.stream().filter(i-> !setTwo.contains(i)).collect(Collectors.toList()));
System.out.println(setTwo.stream().filter(i-> !setOne.contains(i)).collect(Collectors.toList()));

It prints:它打印:

[1, 7] [4, 9]

One important point is that setOne and setTwo should be effectively final to work in a lambda expression.重要的一点是setOnesetTwo应该是有效的 final 才能在 lambda 表达式中工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Java中查找两个大组之间差异的有效方法 - Efficient way to find differences between two large groups in Java 如何找到两个集合之间的差异 - How to find differences between two collections Java中的双向集合 - Two way collections in Java java - 如何以懒惰的方式连接Java中的两个集合? - How to concatenate two collections in java in a lazy way? Java Collections - 查找两个集合之间是否存在公共元素的最快方法 - Java Collections - Quickest way to find if there is a Common element between two Sets 找到两个 collections 之间的最高平均相似度/距离的有效方法是什么? - What is an efficient way to find the highest average similarity/distance between two collections? 检查两个java对象的Deep Equal的最快捷有效的方法是什么? - What is the fastest and efficient way to check Deep Equal for two java objects? 在Java中检查二维数组中邻居的更有效方法 - More efficient way to check neighbours in a two-dimensional array in Java 计算两组(Java)之间交叉点的最有效方法是什么? - What is the most efficient way to count the intersections between two sets (Java)? 在java中找到两个圆之间距离的最有效方法? - Most efficient way to find distance between two circles in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM