Skip to content

Instantly share code, notes, and snippets.

@oxc
Last active March 19, 2020 16:48
Show Gist options
  • Save oxc/8e8e41e36fed6bb4065453df90449a0e to your computer and use it in GitHub Desktop.
Save oxc/8e8e41e36fed6bb4065453df90449a0e to your computer and use it in GitHub Desktop.
OpenJDK snippet demonstrating correct HashMap constructor usage
/**
* Constructs a new set containing the elements in the specified collection.
* The {@code HashMap} is created with default load factor (0.75) and an initial
* capacity sufficient to contain the elements in the specified collection.
*/
public HashSet(Collection<? extends E> c) {
map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
addAll(c);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment