Skip to main content
ICT
Lesson AB28 - Sets and Maps
 
Main Previous Next
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

C. HashSet page 5 of 11

  1. The java.util.HashSet class in the Java standard class library is an implementation of the Set interface using a hash key. A hash key is an index that is calculated from a value by a hashing algorithm (see Lesson AB32, Hash-Coded Data Storage for more information on hash keys). The HashSet class implements the Set interface; therefore, Figure 28.1 describes HashSet’s methods as well.

    Set <String> s = new HashSet <String>();
    s.add("Lynn");
    s.add("Nancy");
    s.add("David");
    s.add("Lynn");
    System.out.println("Size of set = " + s.size());
    Iterator itr = s.iterator();
    while(itr.hasNext()){
       System.out.println(itr.next());
    }

    Run Output:
    Lynn
    David
    Nancy

  2. Since a HashSet does not guarantee any particular order, the order of the output is not known. All we do know is that we will get all of the elements in the Set.

  3. Hashing is a way for the data to be stored in an array in such a way that it can be retrieved very efficiently. Hashing is O(1) for add, remove, and contains.

 

Main Previous Next
Contact
 © ICT 2006, All Rights Reserved.