Given the code below and the fact that 4 HashSet populated elsewhere.
My goal is to contain all the elements (elements) that are common to all 4 HashSets.
My question is, first of all, am I doing this right? Secondly, if I do it right, is there a better way to do this? If not, what solution do I have for this problem?
static Set<String> one=new HashSet<>(); static Set<String> two=new HashSet<>(); static Set<String> three=new HashSet<>(); static Set<String> four=new HashSet<>(); private static void createIntersectionQrels() { ArrayList<String> temp = new ArrayList<>(); Set<String> interQrels = new HashSet<>(); temp.addAll(one); one.retainAll(two); interQrels.addAll(one); one.addAll(temp); one.retainAll(three); interQrels.addAll(one); one.addAll(temp); one.retainAll(four); interQrels.addAll(one); one.addAll(temp); interQrels.retainAll(two); interQrels.retainAll(three); interQrels.retainAll(four); }
source share