Find self-references in (possibly nested) collections

March 16, 2018

I found a nice trick reading part of the ElasticSearch client for Java. Say you are given an object (could be a map, a list, an array or anything) and you need to make sure the same reference does not show up in any of the children objects (or theirs). Here is how this the ElasticSearch guys solved this problem: static void ensureNoSelfReferences(Object value) { ensureNoSelfReferences(value, Collections.newSetFromMap(new IdentityHashMap<>())); } static void ensureNoSelfReferences(final Object value, final Set<Object> ancestors) { if (value ! ... Read more

© 2017 | Powered by Hugo ♥ | Art by Clip Art ETC