![](/img/trans.png)
[英]How to compare two Arraylists which contain objects of the same class in Java?
[英]Easiest way to check 4 Arraylists in java do not contain the same values
我认为标题说明了一切,但
List<Integer> stack ;
List<Integer> exchange;
List<Integer> quora;
堆栈中的值不能交换或法定人数,交换中的值不能在堆栈和法定人数中,依此类推。
使用一些番石榴胶水构建一个充满元素的Set
,然后比较大小:
final Iterable<Integer> all = Iterables.concat(stack, exchange, quora, ...);
final Set<Integer> unique = Sets.newHashSet(all);
if (Iterables.size(all) > unique.size()) {
// inputs contain duplicate(s)
}
使用集查看是否有重复项。
HashSet<Integer> stackSet = new HashSet<>(stack);
HashSet<Integer> exchangeSet = new HashSet<>(exchange);
HashSet<Integer> quoraSet = new HashSet<>(quora);
if(stackSet.removeAll(quoraSet))
; // Error, there were common elements!
您如何从CollectionUtil中 减去 ? 不知道这是否是最好的方法,但这绝对是一种非常简单的方法!
合理地讲,您可以执行类似集合的交集的操作,以确保没有交集。 它看起来应该更有意义:
通过使用Guava库:
HashSet<Integer> stackSet = new HashSet<>(stack);
HashSet<Integer> exchangeSet = new HashSet<>(exchange);
if (! Sets.intersect(stackSet, exchangeSet).isEmpty()) {
// there is intersection between stackSet and exchangeSet
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.