Sets.<E>newHashSetWithExpectedSize(int expectedSize) Method

Definition

Returns a new hash set using the smallest initial table size that can hold expectedSize elements without resizing. Note that this is not what HashSet#HashSet(int) does, but it is what most users want and expect it to do.

This behavior can't be broadly guaranteed, but has been tested with OpenJDK 1.7 and 1.8.

public static HashSet<E> <E>newHashSetWithExpectedSize(int expectedSize)

Parameters

expectedSize
int

the number of elements you expect to add to the returned set

Returns

java.util.HashSet<E>

a new, empty hash set with enough capacity to hold expectedSize elements without resizing

Applies to