24

I have this list:

private List<Set<Address>> scanList;

So my list contains multiple scans as you can see. After each scan I add new set into the list.

After all scans are finished I would like to take only the addresses that occur in every set and put it into:

private List<Address> addresses;

Does something like this already exists in Set/TreeSet/HashSet?

EDIT: after answers, retainAll() is the right method. Thank you. Here is the source:

Set<Address> addressCross = scanList.get(0);
for (int i = 1; i < scanList.size(); i++) {
    addressCross.retainAll(scanList.get(i));
}   
for (Address address : addressCross) {
    addresses.add(address);
}

4 Answers 4

16

you can use retainAll(Collection<?> c), check it out here

A side note: that operation is called intersection.

To convert then it to a List you can use the method addAll(Collection<? extends E> c) which should work between all kinds of containers.

eg:

ArrayList<Address> list = new ArrayList<Address>();
list.addAll(yourSet);
12

See "retainAll()".

8

With Guava, you could do it like this:

Set<Address> intersection = scanList.get(0);
for (Set<Address> scan : scanList.subList(1, scanList.size())) {
  intersection = Sets.intersection(intersection, scan);
}
List<Address> addresses = Lists.newArrayList(intersection);

This creates a view of the intersection of all the sets in the scanList and then copies the addresses in the intersection into a List. You would need to ensure your scanList has at least one element in it, of course.

4
  • Beware! According to Louis Wasserman, Sets.union() has poor performance for more than 2 sets. I believe the same holds true for Sets.intersection() (because both methods return views).
    – Gili
    Sep 1, 2015 at 2:55
  • @Gili: This is generally true, though it's mitigated by the fact that the intersection set is copied at the end, which means that the cost of the nested sets is only incurred there. The issue of nested view sets is bigger if you then pass the set off to some other code that may do many contains calls on it.
    – ColinD
    Sep 1, 2015 at 15:51
  • I've come to this discussion a bit late but doesn't this solution assume the first set contains the "key" elements. For example consider the first set with integers (11,12), the 2nd with (1,3,4,5,6), and so. When I code this up the final multi-intersection set is empty! Jul 28, 2017 at 16:02
  • 3
    @user25029: The goal is to create a list containing only those elements that appear in all sets. So if there is any pair of sets in the input that do not contain any of the same elements (i.e. their intersection is empty), the final result must be empty.
    – ColinD
    Jul 29, 2017 at 14:37
0

There's another nice solution over here: https://stackoverflow.com/a/38266681/349169

Set<Address> intersection = scanList.stream()
    .skip(1) // optional
    .collect(()->new HashSet<>(scanList.get(0)), Set::retainAll, Set::retainAll);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.