Java Reference
In-Depth Information
to contain c 's elements and a load factor of 0.75. This constructor throws
NullPointerException when c contains the null reference.
HashSet(int initialCapacity) creates a new, empty hashset where
the backing HashMap instance has the capacity specified by initialCa-
pacity and a load factor of 0.75. This constructor throws IllegalArgu-
mentException when initialCapacity 's value is less than 0.
HashSet(int initialCapacity, float loadFactor) createsa
new,emptyhashsetwherethebacking HashMap instancehasthecapacityspe-
cified by initialCapacity andthe load factor specified by loadFact-
or . This constructor throws IllegalArgumentException when ini-
tialCapacity islessthan0orwhen loadFactor islessthanorequalto
0.
Listing 5-4 demonstrates a hashset.
Listing 5-4. A demonstration of a hashset with String elements unordered
import java.util.HashSet;
import java.util.Set;
class HashSetDemo
{
public static void main(String[] args)
{
Set<String> ss = new HashSet<>();
String[] fruits = {"apples", "pears", "grapes", "ba-
nanas", "kiwis",
"pears", null};
for (String fruit: fruits)
ss.add(fruit);
dump("ss:", ss);
}
static void dump(String title, Set<String> ss)
{
System.out.print(title+" ");
for (String s: ss)
System.out.print(s+" ");
System.out.println();
 
Search WWH ::




Custom Search