1 / 12

JCF Hashmap & Hashset

JCF Hashmap & Hashset. JCF HashMap stores <Key, Value> pairs in a hash table. Example: HashMap<String, Student> students; //String is the key //Student is the value Key/Value pairs are stored in a Map.Entry object public class HashMap<K, V> implements Map <K, V>

lisle
Download Presentation

JCF Hashmap & Hashset

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JCF Hashmap & Hashset CS-2851Dr. Mark L. Hornick

  2. JCF HashMap stores <Key, Value> pairs in a hash table • Example:HashMap<String, Student> students;//String is the key//Student is the value • Key/Value pairs are stored in a Map.Entry object public class HashMap<K, V> implements Map<K, V> extends AbstractMap<K, V> CS-2851Dr. Mark L. Hornick

  3. JCF HashMap Implements methods such as • put(k,v) – insert value v with key k into the table • get(k) – return the value associated with key k • remove(k) – remove entry associated with key k • containsKey(k) – search for entry with key k • containsValue(v) – search for entry with value v • size() • clear() CS-2851Dr. Mark L. Hornick

  4. HashMap’s advantage is overall performance • Specifically, performance for the following operations is O(k); that is, constant time: • put(k,v) – insert value v with key k into the table • get(k) – return the value associated with key k • remove(k) – remove entry associated with key k • containsKey(k) – search for entry with key k CS-2851Dr. Mark L. Hornick

  5. Performance is gained in exchange for memory utilization efficiency • Hash tables map keys to table indices • Some keys map to the same table index • Some indices remain unmapped CS-2851Dr. Mark L. Hornick

  6. The JCF HashMap collision handling mechanism • At index 873 in the table, store the linked list of all elements whose keys hash to 873 • This is called chaining • It implements a simple singly-linked list CS-2851Dr. Mark L. Hornick

  7. As chains get long, performance degrades to O(M) • M is the number of entries chained together • To maintain good performance, once a JCF HashMap becomes 75% full, it is resized • All indices are recalculated • Chains are removed or reduced CS-2851Dr. Mark L. Hornick

  8. CS-2851Dr. Mark L. Hornick

  9. CS-2851Dr. Mark L. Hornick

  10. Another collision handler • In Open Address hashing, when a collision occurs, the next available index is used to store the key/value • This leads to some interesting practical implementation problems (see the text) CS-2851Dr. Mark L. Hornick

  11. HashSets CS-2851Dr. Mark L. Hornick

  12. A HashSet is an unordered Collection in which the Value is also the Key • All Values in a HashSet must be unique • Since Values are the Keys • The HashSet class has all of the methods in the Collection interface (rather than Map) • add, remove, size, contains, … • plus toString (inherited from AbstractCollection) CS-2851Dr. Mark L. Hornick

More Related