Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "JCF Hashmap & Hashset Winter 2005 CS-2851 Dr. Mark L. Hornick."— Presentation transcript:

1 JCF Hashmap & Hashset Winter 2005 CS-2851 Dr. 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> Winter 2005 CS-2851 Dr. 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() Winter 2005 CS-2851 Dr. 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 Winter 2005 CS-2851 Dr. 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 Winter 2005 CS-2851 Dr. 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 Winter 2005 CS-2851 Dr. 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 Winter 2005 CS-2851 Dr. Mark L. Hornick

8 Winter 2005 CS-2851 Dr. Mark L. Hornick

9 Winter 2005 CS-2851 Dr. 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) Winter 2005 CS-2851 Dr. Mark L. Hornick

11 HashSets Winter 2005 CS-2851 Dr. 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) Winter 2005 CS-2851 Dr. Mark L. Hornick


Download ppt "JCF Hashmap & Hashset Winter 2005 CS-2851 Dr. Mark L. Hornick."

Similar presentations


Ads by Google