java - 如何获取两个ArrayList的重复值?

标签 java arraylist duplicates array-merge

假设我有 2 个点的 ArrayList:

  1. (0,2)->(0,3)->(0,4)
  2. (0,2)->(0,3)->(0,6)

我想获得一个新列表:(0,2)->(0,3)

我该怎么做?

当前解决方案

使用两个 foreach 循环逐个元素地比较两个列表。我认为这是一种非常低效的方式。还有其他方法吗?

最佳答案

您可以使用 List#retainAll(Collection<?> c) 方法,其中:

Retains only the elements in this list that are contained in the specified collection (optional operation). In other words, removes from this list all of its elements that are not contained in the specified collection.

List<Point> first = ...
List<Point> second = ...
first.retainAll(second);

关于java - 如何获取两个ArrayList的重复值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30524949/

相关文章:

java - Java 中的同步 - Vector 与 ArrayList

r - R 中的多次重复(2 次、3 次、...)

jdbc - docker input jdbc 插件中的 Logstash 未获取新行

ios - 如何检查和删除 TableView 中的重复值?

java - RandomAccessSubList 未序列化

java - JPanel 上出现的视觉伪像

java - 在数组列表中搜索最大的数字

java - Java 的方法执行操作更好吗?

java - 如何使用 Java 5 获取主机 mac 地址?

java - 将元素添加到数组列表并测试该数字是否已存在