DefinitionMap (DefinitionMap other) { if (other == this || other == NEUTRAL) { return this; } if (this == NEUTRAL) { return other; } Int2ObjectMap<IntSet> newMap = new Int2ObjectOpenHashMap<>(myMap); List<Integer> toRemove = new SmartList<>(); for (int key : myMap.keySet()) { if (!other.myMap.containsKey(key)) { toRemove.add(key); } } for (int key : toRemove) { newMap.remove(key); } for (Int2ObjectMap.Entry<IntSet> entry : other.myMap.int2ObjectEntrySet()) { int varIndex = entry.getIntKey(); IntSet otherVarDefs = entry.getValue(); IntSet myDefs = newMap.get(varIndex); if (myDefs != null) { IntSet newDefs = new IntArraySet(myDefs); newDefs.addAll(otherVarDefs); newMap.put(varIndex, newDefs); } } return new DefinitionMap(newMap); }