Skip to main content
ICT
Lesson AB32 - Hash-Coded Data Storage
 
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 >  
 

D. HashSet and HashMap page 6 of 8

  1. The HashSet and HashMap classes are implementations of the Set and Map interfaces from the Java standard class Library. A hash table is used to store their elements.

  2. The methods from the HashSet and HashMap that are included in the AP Subset are shown below:

    Methods of the HashSet Class included in the AP Subset*

    // Adds the specified element to this set if it is not
    // already present. Returns true if the set did not already
    // contain the specified element.
    boolean add(Object obj);

    // Returns true if this set contains the specified element.
    boolean contains(Object obj);

    // Returns an iterator over the elements in this set.
    // The elements are returned in no particular order.
    Iterator iterator()

    // Removes the specified element from this set if it is
    // present. Returns true if the set contained the specified
    // element.
    boolean remove(Object obj);

    // Returns the number of elements in this set.
    int size();

    Methods of the HashMap Class included in the AP Subset*

    // Returns true if this map contains a mapping for the
    // specified key.
    boolean containsKey(Object key);

    // Returns the value to which the specified key is mapped,
    // or null if the map contains no mapping for this key.
    boolean get(Object key);

    // Returns a set containing the keys in this map.
    Set keySet()

    // Adds the key-value pair to this map. Returns the previous
    // value associated with specified key, or null if there was
    // no mapping for key.
    boolean put(Object key, Object value);

    // Returns the number of key-value pairs in this map.
    int size();

  3. In the HashSet class, a hash of the element is used to find its location in the hashtable. In the HashMap class, a hash of the key is used. The hashCode method, which exists on all objects, calculates the hash code.

  4. The basic operations on HashSet and HashMap objects run in constant, O(1), time due to the hash table implementation employed by the class.

  5. The iterator that is returned by the iterator method of HashSet does not order the objects returned.

 

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