假设我有 2 个点的 ArrayList:
(0,2)->(0,3)->(0,4)
(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/