![](/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.