Finding duplicates between n Sets I have n Objects,
Each Object has a Set of "nested" "Neighboring Objects".
I need to check if there are duplicates between the "Neighboring Objects" Sets (there are no duplicates inside a single Set of "Neighboring Objects", but there might be between Sets).

At the end I need a new List of shared "Neighboring Objects"

In order to qualify, an Object must be present in every "Neighboring Set"

public List&lt;Object&gt; getOverlapingNeighbors(Object... d) {

    Objects.requireNonNull(d);

    List&lt;Object&gt; overlapingNeighbores = new ArrayList&lt;Object&gt;();

    Set&lt;Object&gt; neighbores = d[0].getNeighboringObjects(); // returns the "Neighboring Objects" Set

    for (Object candidate : neighbores) {

        boolean flag1 = true;

        for (int i = 1; i &lt; d.length; i++) {

            boolean flag2 = false;

            for (Object other : d[i].getNeighboringObjects()) {
                if (candidate.equals(other)) {
                    flag2 = true;
                    break;
                }
            }

            if (!flag2) {
                flag1 = false;
                break;
            }

        }

        if (flag1) {
            overlapingNeighbores.add(candidate);
        }

    }

    return overlapingNeighbores;
} 


I don't feel comfortable with this approach...
I was hoping some one smarter has implemented a way of doing this better
First off, I'm assuming Object isn't java.lang.Object, but your own class. In that case you really should find a better name for it. Even if java.lang.Object didn't exist, "Object" is a terribly generic name giving absolutely no clue on what it represents.



The variable names flag1 and flag2 are beyond terrible, plus I believe you only need one boolean flag, if at all. Reading the algorithm gave me a headache, so I gave up after a while, so I can't say if it does what it should correctly.



Considering the method is working with sets it should return a Set and not a List.



Is the use of variable arguments really a valid usage pattern? That is usually only sensible if you call the method with separate arguments in code. This seems more like a situation where you have a Collection of objects to start with.



d is a bad variable name (again).



You should check if d actually contains at least one object (and return an empty list/set, if not), otherwise the user of the method will get an unhelpful IndexOutOfBoundsException.



In your method you only access the "neighboring objects" property of your Object and no other property. Instead of giving the method a list/array of your Objects, it would make sense to instead just give it a list/array of the sets of neighboring objects.



As @markspace says, the algorithm you are describing seems to simply be the intersection of all sets, which you can get using the method .retainAll() of Set.



Based on this the simplest attempt would be something like this (using Java 8 Streams):

// This first method is only needed if you really need the varargs:
public static Set&lt;Object&gt; getOverlapingNeighbors(Object... objects) {
   return getOverlapingNeighbors(Arrays.asList(d));
}

public static Set&lt;Object&gt; getOverlapingNeighbors(List&lt;Object&gt; objects) {
   return intersectionOf(objects.stream().map(Object::getNeighboringObjects).collect(Collectors.toList()));
}

// These are two generic methods that calculate the intersection of Sets of any (java.utils.)Objects. 
// It could/should be placed in a general utility class. 
public static &lt;T&gt; Set&lt;T&gt; intersectionOf(Collection&lt;Set&lt;T&gt;&gt; sets) {
    // It may be useful to first sort the list of sets by size, to optimize 
    // runtime. 
    // It is not the most optimal implementation, since it creates a new HashSet 
    // in each call of intersectionOf(Set&lt;T&gt; a, Set&lt;T&gt; b)
    return sets.stream().reduce((r, s) -&gt; intersectionOf(r, s)).orElse(Collections.emptySet());
}


public static &lt;T&gt; Set&lt;T&gt; intersectionOf(Set&lt;T&gt; a, Set&lt;T&gt; b) {
    Set&lt;T&gt; newA = new HashSet&lt;&gt;(a);
    newA.retainAll(b);
    return newA;
}