Set<K> () { if (keySet == null) { keySet = new AbstractSet<K>() { @Override public @NotNull Iterator<K> iterator() { return new MyIterator<K>() { @Override K fieldElement(int offset) { return offset == 0 ? k1 : offset == 1 ? k2 : k3; } @SuppressWarnings("unchecked") @Override K tableElement(int offset) { return (K)data[offset]; } }; } @Override public void forEach(Consumer<? super K> action) { if (k1 != null) { if (k2 != null) { if (k3 != null) { action.accept(k3); } action.accept(k2); } action.accept(k1); } for (int i = 0; i < data.length; i += 2) { Object key = data[i]; if (key != null) { @SuppressWarnings("unchecked") K k = (K)data[i]; action.accept(k); } } } @Override public boolean contains(Object o) { return containsKey(o); } @Override public int size() { return UnmodifiableHashMap.this.size(); } }; } return keySet; }