Hacker News new | ask | show | jobs
by cmckn 1788 days 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/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/sha...