Presentation is loading. Please wait.

Presentation is loading. Please wait.

Podcast Ch21f Title: HashSet Class

Similar presentations


Presentation on theme: "Podcast Ch21f Title: HashSet Class"— Presentation transcript:

1 Podcast Ch21f Title: HashSet Class
Description: Overview; add() method; iterator; remove() method; performance Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) Textbook: Data Structures for Java; William H. Ford and William R. Topp

2 HashSet Class The HashSet class uses a HashMap by composition. The class defines a static Object reference called PRESENT. This becomes the value component for each entry in the map. The constant reference serves as a dummy placeholder in an entry pair. Declare a private instance variable map of type HashMap having T as the type of the set elements and Object as the value type. The constructor instantiates the map collection. This has the effect of creating an empty set.

3 HashSet Class (continued)
public class HashSet<T> implements Set<T> { // value for each key in the map private static final Object PRESENT = new Object(); // set implemented using a hash map private HashMap<T, Object> map; // create an empty set object public HashSet() { map = new HashMap<T,Object>(); } . . . }

4 HashSet add() The set methods are implemented with map methods that use the entry <item, PRESENT> as the argument. add() uses the map method put(). If a duplicate exists, then put() simply updates the value field of the entry to PRESENT which is its current value. The map method returns null if a new element is added, so a return value of null indicates that the add() inserted item. public boolean add(T item) { return map.put(item, PRESENT) == null; }

5 When implementing the HashSet<T> class using the HashMap<T,V> class and composition, the element item in HashSet is stored as the entry <item PRESENT> in the HashMap. The value PRESENT is defined as (a) PRESENT = null; (b) PRESENT = new Object(); (c) PRESENT = new V(); (d) None of these

6 The operation to insert a new element in collection(s) _______________ has O(1) or constant runtime efficiency. (a) ArrayList (b) Stack (c) TreeSet (d) HashSet

7 HashSet iterator() The HashSet iterator must traverse the keys in the map. Implement the method iterator() by returning an iterator for the key set collection view of the map. // returns an iterator for the elements in the set public Iterator<T> iterator() { return map.keySet().iterator(); }

8 HashSet remove() The HashSet remove() method calls the remove() method for the map. To determine whether an element was removed from the set, verify that the return value from the map remove() call is the reference PRESENT. public boolean remove(Object obj) { return map.remove(obj) == PRESENT; }

9 Hash Table Performance
A good hash function provides a uniform distribution of hash values. Hash table performance is measured by using the load factor  = n/m, where n is the number of elements in the hash table and m is the number of buckets. For linear probe, 0 ≤  ≤ 1. For chaining with separate lists, it is possible that  > 1.

10 Hash Table Performance (continued)
The worst case linear probe or chaining with separate lists occurs when all data items hash to the same table location. If the table contains n elements, the search time is O(n), no better than that for the sequential search.

11 Hash Table Performance (continued)
Assume that the hash function uniformly distributes indices around the hash table. We can expect  = n/m elements in each bucket. On the average, an unsuccessful search makes  comparisons before arriving at the end of a list and returning failure. Mathematical analysis shows that the average number of probes for a successful search is approximately 1 + /2.

12 Hash Table Performance (concluded)
Assume the number of elements n in the hash table is bounded by some amount, say, R*m, where m is the table size. In this case,  = n/m  (R*m)/m = R, and the following relationships hold for the average cases, so the average running time is O(1)! S  1 + /2 ≤ 1 + R/2 (Successful Search) U =  ≤ R (Unsuccessful Search)

13 Evaluating Ordered and Unordered Sets and Maps
Use an ordered set or map if an iteration should return elements in order (average search O(log2n)). Use an unordered set or map when fast access and updates are needed without any concern for the ordering of elements (average search time O(1)).

14 Iterators for the collection(s) __________ scan elements in ascending order
(a) ArrayList (b) OrderedList (c) TreeSet (d) HashSet

15 Timing Example Program SearchComp.java:
Reads a file of randomly ordered words and inserts each word into a TreeSet and into a HashSet. Determines the amount of time required to build both of the data structures. Shuffles the input from the file and times a search of the TreeSet and HashSet for each word in the shuffled input. Displays the time required for each search technique.

16 Timing Example (concluded)
Run: Number of words is 25025 Built TreeSet in seconds Built HashSet in seconds TreeSet search time is seconds HashSet search time is seconds Note that the HashSet search time is considerably better than that for a TreeSet.

17 The method toStringOrder() takes a HashSet as an argument and returns a string that lists the elements in ascending order. Hint: use Arrays methods. public static <T> String toStringOrder(HashSet<T> hset) { Object[] arr = hset.toArray(); Arrays.sort(arr); return Arrays.toString(arr); }


Download ppt "Podcast Ch21f Title: HashSet Class"

Similar presentations


Ads by Google