简体   繁体   English

在Java中检查4个Arraylist的最简单方法不包含相同的值

[英]Easiest way to check 4 Arraylists in java do not contain the same values

I think the title says it all, but 我认为标题说明了一切,但

List<Integer> stack ;
List<Integer> exchange;
List<Integer> quora;

A value in stack can not be in exchange or quoroa, a value in exchange can not be in stack and quora, and so on. 堆栈中的值不能交换或法定人数,交换中的值不能在堆栈和法定人数中,依此类推。

Construct a Set full of your elements with some Guava glue and then compare the sizes: 使用一些番石榴胶水构建一个充满元素的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)
}

Use Sets to see if there are duplicates. 使用集查看是否有重复项。

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!

How do you like subtract from CollectionUtil ? 您如何从CollectionUtil中 减去 Don't know if it is the best way, but it is definitely a very easy approach! 不知道这是否是最好的方法,但这绝对是一种非常简单的方法!

Rationally, you can do something like intersection of sets, so that to make sure there is no intersection. 合理地讲,您可以执行类似集合的交集的操作,以确保没有交集。 It should look more meaningful: 它看起来应该更有意义:

By using Guava library: 通过使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM