3 ms·
This is true for Java as well, a `HashSet` is just a `HashMap<T, Object>` where the value is: // Dummy value to associate with an Object in the backing Map
by cmckn 5y ago
This is true for Java as well, a `HashSet` is just a `HashMap<T, Object>` where the value is:
// Dummy value to associate with an Object in the backing Map
private static final Object PRESENT = new Object();
https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/HashSet.java#L97 https://github.com/openjdk/jdk/blob/master/src/java.base/sha...
A thread-safe implementation is available with `ConcurrentHashMap.newKeySet()`, which uses a similar approach:
public static <K> KeySetView<K,Boolean> newKeySet() {
return new KeySetView<K,Boolean>
(new ConcurrentHashMap<K,Boolean>(), Boolean.TRUE);
}
https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/HashSet.java#L97 https://github.com/openjdk/jdk/blob/master/src/java.base/sha...