8284780: Need methods to create pre-sized HashSet and LinkedHashSet

Reviewed-by: naoto, bpb, dfuchs, ascarpino
This commit is contained in:
XenoAmess 2022-06-09 01:50:54 +00:00 committed by Stuart Marks
parent a941bc2de6
commit e01cd7c3ed
29 changed files with 147 additions and 47 deletions

View file

@ -2640,14 +2640,7 @@ public abstract class Calendar implements Serializable, Cloneable, Comparable<Ca
}
private static class AvailableCalendarTypes {
private static final Set<String> SET;
static {
Set<String> set = new HashSet<>(3);
set.add("gregory");
set.add("buddhist");
set.add("japanese");
SET = Collections.unmodifiableSet(set);
}
private static final Set<String> SET = Set.of("gregory", "buddhist", "japanese");
private AvailableCalendarTypes() {
}
}

View file

@ -125,6 +125,10 @@ public class HashSet<E>
* Constructs a new, empty set; the backing {@code HashMap} instance has
* the specified initial capacity and the specified load factor.
*
* @apiNote
* To create a {@code HashSet} with an initial capacity that accommodates
* an expected number of elements, use {@link #newHashSet(int) newHashSet}.
*
* @param initialCapacity the initial capacity of the hash map
* @param loadFactor the load factor of the hash map
* @throws IllegalArgumentException if the initial capacity is less
@ -138,6 +142,10 @@ public class HashSet<E>
* Constructs a new, empty set; the backing {@code HashMap} instance has
* the specified initial capacity and default load factor (0.75).
*
* @apiNote
* To create a {@code HashSet} with an initial capacity that accommodates
* an expected number of elements, use {@link #newHashSet(int) newHashSet}.
*
* @param initialCapacity the initial capacity of the hash table
* @throws IllegalArgumentException if the initial capacity is less
* than zero
@ -372,4 +380,21 @@ public class HashSet<E>
public <T> T[] toArray(T[] a) {
return map.keysToArray(map.prepareArray(a));
}
/**
* Creates a new, empty HashSet suitable for the expected number of elements.
* The returned set uses the default load factor of 0.75, and its initial capacity is
* generally large enough so that the expected number of elements can be added
* without resizing the set.
*
* @param numElements the expected number of elements
* @param <T> the type of elements maintained by the new set
* @return the newly created set
* @throws IllegalArgumentException if numElements is negative
* @since 19
*/
public static <T> HashSet<T> newHashSet(int numElements) {
return new HashSet<>(HashMap.calculateHashMapCapacity(numElements));
}
}

View file

@ -126,6 +126,10 @@ public class LinkedHashSet<E>
* Constructs a new, empty linked hash set with the specified initial
* capacity and load factor.
*
* @apiNote
* To create a {@code LinkedHashSet} with an initial capacity that accommodates
* an expected number of elements, use {@link #newLinkedHashSet(int) newLinkedHashSet}.
*
* @param initialCapacity the initial capacity of the linked hash set
* @param loadFactor the load factor of the linked hash set
* @throws IllegalArgumentException if the initial capacity is less
@ -139,6 +143,10 @@ public class LinkedHashSet<E>
* Constructs a new, empty linked hash set with the specified initial
* capacity and the default load factor (0.75).
*
* @apiNote
* To create a {@code LinkedHashSet} with an initial capacity that accommodates
* an expected number of elements, use {@link #newLinkedHashSet(int) newLinkedHashSet}.
*
* @param initialCapacity the initial capacity of the LinkedHashSet
* @throws IllegalArgumentException if the initial capacity is less
* than zero
@ -166,7 +174,7 @@ public class LinkedHashSet<E>
* @throws NullPointerException if the specified collection is null
*/
public LinkedHashSet(Collection<? extends E> c) {
super(Math.max(2*c.size(), 11), .75f, true);
super(HashMap.calculateHashMapCapacity(Math.max(c.size(), 12)), .75f, true);
addAll(c);
}
@ -193,4 +201,21 @@ public class LinkedHashSet<E>
public Spliterator<E> spliterator() {
return Spliterators.spliterator(this, Spliterator.DISTINCT | Spliterator.ORDERED);
}
/**
* Creates a new, empty LinkedHashSet suitable for the expected number of elements.
* The returned set uses the default load factor of 0.75, and its initial capacity is
* generally large enough so that the expected number of elements can be added
* without resizing the set.
*
* @param numElements the expected number of elements
* @param <T> the type of elements maintained by the new set
* @return the newly created set
* @throws IllegalArgumentException if numElements is negative
* @since 19
*/
public static <T> LinkedHashSet<T> newLinkedHashSet(int numElements) {
return new LinkedHashSet<>(HashMap.calculateHashMapCapacity(numElements));
}
}