By Rick Mercer with help from - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

By Rick Mercer with help from

Description:

Discuss what a hash method does. translates a string key into an integer ... key could be your SID, your telephone number, social security number, account number, ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 33
Provided by: rickmercer
Category:
Tags: help | mercer | rick | sid

less

Transcript and Presenter's Notes

Title: By Rick Mercer with help from


1
A Hash Table and a little of Java's Collection
Framework
  • By Rick Mercer with help from
  • The Java Tutorial, The Collections Trail, by
    Joshua Block

2
Hash Tables Rick Mercer
3

Hash TablesAn implementation of a Map
  • Outline
  • Discuss what a hash method does
  • translates a string key into an integer
  • Discuss a few strategies for implementing a hash
    table
  • linear probing
  • quadratic probing
  • separate chaining hashing

4
The Map ADT
  • Map describes a type that stores a collection of
    elements that consists of a key and a value
  • A Map associates (maps) a key the it's value
  • The keys must be unique
  • the values need not be unique

5
Hash Tables
  • A Map is often implemented with a hash table
  • Hash tables provide virtually direct access to
    objects based on a key (String or Integer)
  • key could be your SID, your telephone number,
    social security number, account number,
  • The direct access is made possible by converting
    a String or Integer key to an array index

6
Hashing
  • A key such as "555-1234" results in an integer
    index from 0 to an array upper bound index
  • Elements can be found, inserted, and removed
    using the integer index as an array index
  • A hash table uses the same "address calculator"
    to (insert), get (find), and remove
  • Could convert a String key to 4 like this
  • "able" 97 98 108 101 404
  • array index 404 array capacity

7
Hash Function
  • Ideally, every key has a unique hash value
  • Then the hash value could be used as an array
    index, however ....
  • Cannot rely on every key "hashing" to a unique
    array index
  • can get "close"
  • need a way to handle "collisions"
  • "abc" may hash to the same integer as "cba"

8
Hash function works something like this
Convert a String key into an integer that will be
in the range of 0 through the maximum capacity-1
Assume the array
capacity is 9997
hash(key)
AAAAAAAA
8482
1273
zzzzzzzz
hash(key)
Domain "!" .. "zzzzzzzz"
Range 0 ... 9996
9
Hash Function
  • What if the ASCII value of individual chars of
    the string key added up to a number from ("A") 65
    to possibly 488 ("zzzz") 4 chars max
  • If the array has size 309, mod the sum
  • 390 TABLE_SIZE 81
  • 394 TABLE_SIZE 85
  • 404 TABLE_SIZE 95
  • Keys converted to array indices could be

81 85 95
"abba" "abcd" "able"
10
A "too simple" hash function
  • // Return an int in the range of 0..TABLE_SIZE-1
  • public int hash(String key)
  • int result 0
  • int n key.length()
  • for (int j 0 j lt n j)
  • result key.charAt(j) // add up the chars
  • return result TABLE_SIZE
  • public static final int TABLE_SIZE 309
  • public void testHashFunction()
  • assertEquals(81, hash("abba"))
  • assertEquals(81, hash("baab"))
  • assertEquals(85, hash("abcd"))
  • assertEquals(86, hash("abce"))
  • assertEquals(308, hash("IKLT"))
  • assertEquals(308, hash("KLMP"))

11
Collisions
  • A good hash method
  • executes quickly
  • distributes keys equitably
  • But you still have to handle collisions when two
    keys have the same hash value
  • the hash method is not guaranteed to return a
    unique integer for each key
  • example simple hash method with "baab" and
    "abba"
  • There are several ways to handle collisions
  • Consider separate chaining hashing

12
An Array of LinkedList Objects Implementation
  • An array of linked lists

0
321
365
1
2
13
Insert is Easy
  • public void put(String key, Comparable x)
  • int pos hash(key)
  • // Place the element in the correct list
  • tablepos.add (x) // Is addFirst faster?
  • public void display()
  • for(int j 0 j lt TABLE_SIZE j)
  • System.out.print(j ". ")
  • // Print an entire linked list
  • System.out.println(tablej)

14
Insert Six Objects
  • MyHashTableltString, BankAccountgt h
  • new MyHashTableltString,
    BankAccountgt()
  • BankAccount a1 new BankAccount("abba",
    100.00)
  • BankAccount a2 new BankAccount("abcd",
    200.00)
  • BankAccount a3 new BankAccount("abce",
    300.00)
  • BankAccount a4 new BankAccount("baab",
    400.00)
  • BankAccount a5 new BankAccount("KLMP",
    500.00)
  • BankAccount a6 new BankAccount("IKLT",
    600.00)
  • // Insert BankAccount objects using ID as the
    key
  • h.put(a1.getID(), a1)
  • h.put(a2.getID(), a2)
  • h.put(a3.getID(), a3)
  • h.put(a4.getID(), a4)
  • h.put(a5.getID(), a5)
  • h.put(a6.getID(), a6)
  • h.display()

15
A Better Hash methodFrom Mark Alan Weiss
  • Java requires the key to overrode hashCode
  • String and integer already do
  • // Return an int in the range of 0..TABLE_SIZE-1
  • public int hash(K key)
  • return key.hashCode() TABLE_SIZE

16
The output when TABLE_SIZE10
  • 0. abbaabba 100.00, baabbaab 400.00
  • 1.
  • 2.
  • 3.
  • 4. abcdabcd 200.00
  • 5. abceabce 300.00
  • 6.
  • 7.
  • 8. KLMPKLMP 500.00, IKLTIKLT 600.00
  • 9.
  • public void display()
  • for (int j 0 j lt TABLE_SIZE j)
  • System.out.print(j ". ")
  • // Print an entire linked list
  • System.out.println(tablej)

17
Array of LinkedLists
  • import java.util.LinkedList
  • public class MyHashTableltK, Vgt
  • private final static int TABLE_SIZE 10
  • private LinkedList table
  • private class HashNode
  • private K key
  • private V data
  • private HashNode(K key, V data)
  • this.key key
  • this.data data
  • _at_Override
  • public String toString()
  • return key.toString() ""
    data.toString()

18
LinkedList is Java Collection class, available to
all
  • public MyHashTable()
  • table new LinkedListTABLE_SIZE
  • for (int j 0 j lt TABLE_SIZE j)
  • tablej new LinkedList()
  • // return an int in the range of 0..TABLE_SIZE-1
  • public int hash(K key)
  • return key.hashCode() TABLE_SIZE
  • public void put(K key, V x)
  • HashNode hn new HashNode(key, x)
  • int pos hash(key)
  • tablepos.add(hn)

19
Collection Framework
  • Java's collection framework is a unified
    architecture for representing and manipulating
    collections. It has
  • Interfaces abstract data types representing
    collections
  • Implementations concrete implementations of the
    collection interfaces
  • Algorithms methods that perform useful
    computations, such as searching and sorting
  • These algorithms are said to be polymorphic the
    same method can be used on different
    implementations

20
Interfaces
  • An interface describes a set of methods
  • no constructors or instance variables
  • Interfaces must be implemented by classes
  • 646 java classes implement gt 1 interfaces (02)
  • 2 or more classes implement an interface
  • Classes guaranteed to have the same methods
  • Objects can be treated as the same type
  • Can use different algorithms / instance variables

21
Collection interfaces
Queue
22
Implementations
  • A collection class
  • implements an ADT as a Java class
  • implements all methods of the interface
  • selects appropriate instance variables
  • can be instantiated
  • Java provides several implementations (classes)
  • ArrayList, LinkedList, HashMap, HashSet, Stack,
    TreeMap, TreeSet
  • Some of this will be on the quiz

23
Algorithms
  • Java has polymorphic algorithms to provide
    functionality for different types of collections
  • Sorting (e.g. sort)
  • Shuffling (e.g. shuffle)
  • Routine Data Manipulation (e.g. reverse, addAll)
  • Searching (e.g. binarySearch)
  • Composition (e.g. frequency)
  • Finding Extreme Values (e.g. max)

24
Map and SortedMap
  • The Map interface defines as get, put, contains,
    keySet, values, and entrySet
  • TreeMap implements Map
  • put, get, remove O(log n)
  • HashMap implements Map
  • put, get, remove O(1)
  • SortedMap extends Map by adding
  • SortedMapltK,Vgt subMap(K fromKey,
    K toKey) SortedMapltK,Vgt
    tailMap(K fromKey)
  • K lastKey() K firstKey()

25
Set and SortedSet
  • The Set interface
  • add, addAll, remove, size, but no get!
  • Some implementations
  • TreeSet values stored in order, O(log n)
  • HashSet values in a hash table, no order, O(1)
  • SortedSet extends Set by adding methods
    SortedSetltEgt tailSet(E fromElement) E
    first()
  • SortedSetltEgt headSet(E fromElement) E
    last()
  • SortedSetltEgt subSet(E fromElement,
    E toElement)

26
2 SortedMap methods
  • TreeMapltString, BankAccountgt c
  • new TreeMapltString, BankAccountgt()
  • c.put("M", new BankAccount("Michel", 111.11))
  • c.put("G", new BankAccount("George", 222.22))
  • c.put("S", new BankAccount("Sam", 333.33))
  • c.put("P", new BankAccount("Pei", 444.44))
  • c.put("C", new BankAccount("Casey", 555.55))
  • assertEquals("C", c.firstKey())
  • assertEquals("S", c.lastKey())
  • // Get the set of keys in the Map
  • Set s c.keySet()
  • assertEquals("C, G, M, P, S", s.toString())

27
Sorted keys?
  • TreeSet keySet returns a sorted set
  • HashMap keySet returns keys in chaotic order
  • A HashMap converts the key into an integer that
    represents an array index
  • The key/value mapping is added to the list in
    that location
  • There can be several keys that "hash" to the same
    array index so search the small list O(1)

28
HashMap keys not in order
  • Change TreeMap to HashMap and the keys are not in
    order
  • HashMapltString, BankAccountgt c
  • new HashMapltString, BankAccountgt()
  • c.put("M", new BankAccount("Michel", 111.11))
  • // 3 puts not shown
  • c.put("C", new BankAccount("Casey", 555.55))
  • SetltStringgt s c.keySet()
  • System.out.println(s.toString())
  • Output
  • S, M, C, P, G

29
Iterators
  • Iterators provide a general way to traverse all
    elements in a collection
  • ArrayListltStringgt list new
    ArrayListltStringgt()
  • list.add("1-FiRsT")
  • list.add("2-SeCoND")
  • list.add("3-ThIrD")
  • IteratorltStringgt itr list.iterator()
  • while (itr.hasNext())
  • System.out.println(itr.next().toLowerCase())

Output 1-first 2-second 3-third
30
Enhanced for Loop
  • The for loop has been enhanced to iterate over
    collections
  • General form
  • for (Type element collection)
  • element is the next thing visited each
    iteration
  • for (String key s)
  • System.out.println(key " ")

31
Map methods
  • Map objects do not have an iterator
  • However, methods do exist that return a set
  • // What is returned?
  • SetltStringgt s tm.keySet()
  • IteratorltStringgt itr s.iterator()

32
Algorithms
  • See the Java API for the Collections class
  • play with sort, binarySearch, reverse, shuffle
  • your choice, or what's relevant for final project
  • Use Java's API to examine the Collections methods
  • shuffle
  • binarySearch
Write a Comment
User Comments (0)
About PowerShow.com