void (@NotNull MostlySingularMultiMap<K, V> other) { if (other.isEmpty()) { return; } for (Map.Entry<? extends K, Object> entry : other.myMap.entrySet()) { K key = entry.getKey(); Object otherValue = entry.getValue(); Object myValue = myMap.get(key); if (myValue == null) { if (otherValue instanceof ValueList) { //noinspection unchecked myMap.put(key, new ValueList<>((ValueList<? extends V>)otherValue)); } else { myMap.put(key, otherValue); } } else if (myValue instanceof ValueList) { //noinspection unchecked List<V> myListValue = (ValueList<V>)myValue; if (otherValue instanceof ValueList) { //noinspection unchecked myListValue.addAll((ValueList<? extends V>)otherValue); } else { //noinspection unchecked myListValue.add((V)otherValue); } } else { if (otherValue instanceof ValueList) { //noinspection unchecked List<V> otherListValue = (ValueList<V>)otherValue; List<V> newList = new ValueList<>(otherListValue.size() + 1); //noinspection unchecked newList.add((V)myValue); newList.addAll(otherListValue); myMap.put(key, newList); } else { List<Object> newList = new ValueList<>(); newList.add(myValue); newList.add(otherValue); myMap.put(key, newList); } } } }