public void addEntry( String structureName, String hostname, T structure, boolean assumeAllUnique) { Map<Integer, Set<NamedStructureEquivalenceSet<T>>> sameNamedStructuresByHash = _sameNamedStructuresByNameAndHash.computeIfAbsent(structureName, s -> new HashMap<>()); String structureJson = writeObject(structure); int hash = structureJson.hashCode(); Set<NamedStructureEquivalenceSet<T>> eqSetsWithSameHash = sameNamedStructuresByHash.computeIfAbsent(hash, h -> new HashSet<>()); if (assumeAllUnique || eqSetsWithSameHash.isEmpty()) { eqSetsWithSameHash.add(new NamedStructureEquivalenceSet<>(hostname, structure)); } else { Optional<NamedStructureEquivalenceSet<T>> potentialMatchingSet = eqSetsWithSameHash.stream() .filter( s -> checkJsonStringEquals(structureJson, writeObject(s.getNamedStructure()))) .findAny(); if (potentialMatchingSet.isPresent()) { NamedStructureEquivalenceSet<T> matchingSet = potentialMatchingSet.get(); matchingSet.setNodes( new ImmutableSortedSet.Builder<String>(Comparator.naturalOrder()) .addAll(matchingSet.getNodes()) .add(hostname) .build()); } else { eqSetsWithSameHash.add(new NamedStructureEquivalenceSet<>(hostname, structure)); } } }