V (K key, V value) { V oldValue = myKeyToValueMap.put(key, value); if (oldValue != null) { if (oldValue.equals(value)) { return oldValue; } List<K> array = myValueToKeysMap.get(oldValue); array.remove(key); if (array.isEmpty()) { myValueToKeysMap.remove(oldValue); } } myValueToKeysMap.computeIfAbsent(value, __ -> new SmartList<>()).add(key); return oldValue; }