void () { Random r = new Random(1); for(int n=0; n<15; n++) { Map<Key<Object>, Object> hashMap = new HashMap<>(); KeyFMap fMap = KeyFMap.EMPTY_MAP; for(int i=0; i<n; i++) { Object value = "Value#" + i; Key<Object> key; while (true) { key = KEYS.get(r.nextInt(KEYS.size())); if (hashMap.putIfAbsent(key, value) == null) break; } KeyFMap newFMap = fMap.plus(key, value); assertNotSame(fMap, newFMap); // new key is added: must be not same fMap = newFMap; } assertEquals(hashMap + ":" + fMap, hashMap.hashCode(), fMap.hashCode()); KeyFMap fMap2 = KeyFMap.EMPTY_MAP; for (Map.Entry<Key<Object>, Object> entry : hashMap.entrySet()) { fMap2 = fMap2.plus(entry.getKey(), entry.getValue()); } assertEquals(fMap, fMap2); Iterator<Key<Object>> iterator = hashMap.keySet().iterator(); while(iterator.hasNext()) { Key<Object> key = iterator.next(); iterator.remove(); assertEquals(fMap, fMap2); fMap = fMap.minus(key); assertFalse(fMap.equals(fMap2)); fMap2 = fMap2.minus(key); assertEquals(fMap, fMap2); assertEquals(fMap.hashCode(), fMap2.hashCode()); } assertTrue(fMap.isEmpty()); } }