System.identityHashCode , - IdentityHashMap.
boolean containsCircularReference(Iterable<?> iterable) {
return containsCircularReference(
iterable,
Collections.newSetFromMap(new IdentityHashMap<Object, Boolean>()));
}
private boolean containsCircularReference(Object o, Set<Object> seen) {
if (seen.contains(o)) {
return true;
}
seen.add(o);
if (o instanceof Iterable) {
for (Object o2 : (Iterable<?>) o) {
if (containsCircularReference(o2, seen)) {
return true;
}
}
}
return false;
}
System.identityHashCode . 2 ^ 32 JVM, 2 ^ 32 identityHashCode...
Iterable, , , . , equals hashCode ; , equals hashCode , .