By Rick Mercer with help from - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

By Rick Mercer with help from

Description:

Hash tables provide virtually direct access to objects based on a key ... This hash function adds ASCII values and mods the sum by array capacity ' ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 19
Provided by: rickmercer
Category:
Tags: help | mercer | mods | rick

less

Transcript and Presenter's Notes

Title: By Rick Mercer with help from


1
Hashing and Some of Java's Collection Framework
  • By Rick Mercer with help from
  • The Java Tutorial, The Collections Trail, by
    Joshua Block

2
Hash Tables
  • Hash tables provide virtually direct access to
    objects based on a key
  • key could be your SID, your telephone number,
    social security number, account number,
  • The direct access is made possible by converting
    the key to an array index
  • a.k.a. hashing http//en.wikipedia.org/wiki/Has
    h_function

3
Hashing
  • A key such as "S555555" can result in an integer
    from 0 to an array upper bound index
  • Elements can be found, inserted, and removed
    using this integer index as an array index
  • A hash table uses the same hash function for put,
    get, and remove
  • This hash function adds ASCII values and mods the
    sum by array capacity
  • "able" 97 98 108 101 404
  • array index 309 array.length

4
A hash function that needs improvement
  • public static final int TABLE_SIZE 309
  • // Return an int in the range of 0..TABLE_SIZE-1
  • public int hash(String key)
  • int result 0
  • for (int j 0 j lt key.length() j)
  • result key.charAt(j) // add up the chars
  • return result TABLE_SIZE
  • A unit test
  • _at_Test 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"))

5
One way to use the hash value (there are many
others)
  • Add the mapping to one of many linked lists
  • A collision is when different keys result is the
    same hash value

80
"baab" value
"abba" value
81
82
6
A better 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, but can get close
  • Need a way to handle "collisions"
  • "abc" may hash to the same integer as "cba"
  • A better hash java.util.String hashCode()
  • s031(n-1) s131(n-2) ... sn-1

7
Runtimes
  • Runtime for put
  • hasCode() is O(1)
  • Put get a reference to a list, which is O(1),
    then add to that list
  • There will be many lists with only a few mappings
  • get?
  • remove?
  • Now consider Java collections, a few of which use
    hash tables . . .

8
Collection Framework
  • Java's collection framework is a unified
    architecture for representing and manipulating
    collections. It has
  • Interfaces abstract data types to specify
    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

9
Collection interface hierarchy
  • Collection specifies methods common to a
    collection
  • contains isEmpty add addAll remove removeAll
    iterator
  • Other interfaces add methods List adds get set
    indexOf
  • Interfaces are generic
  • public interface CollectionltEgt extends
    IterableltEgt
  • public interface SetltEgt extends CollectionltEgt

10
Implementations
  • A Java collection class
  • implements one of those interfaces
  • uses data structures like arrays, linked
    structures, binary search trees, hash tables
  • Java's collection framework has many
  • ArrayList LinkedList
  • TreeMap HashMap
  • TreeSet HashSet
  • ArrayBlockingQueue LinkedBlockingQueue

11
Set and SortedSet
  • The Set interface (needed in Boggle)
  • 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)
  • SortedSetltEgt headSet(E toElement)
  • SortedSetltEgt subSet(E fromElement, E toElement)
  • E last() E first()

12
TreeSetltEgt implements OrderedSetltEgt plus 6 other
interfaces
  • SetltStringgt set new TreeSetltStringgt()
  • set.add("pear")
  • set.add("banana")
  • set.add("apple")
  • assertEquals("apple, banana, pear",set.toString(
    ))
  • Demonstrate a few other methods
  • isEmpty size remove

13
Iteration 2 of Boggle requires these
  • From the Boggle Spec, bottom of page 3
  • // All words the user entered that count for
    score public SetltStringgt getWordsFound()
  • // All words the user entered that do not count
  • public SetltStringgt getWordsIncorrect()
  • // All words user could have guessed but didn't
    public SetltStringgt getWordsNotGuessed()

14
Set Elements Sorted or Not?
  • TreeSet keySet returns a sorted set
  • HashSet keySet returns keys in chaotic order
  • Change TreeSet to HashSet and the assertion
    likely will not pass

15
Traversing Collections
  • Iterators provide a general way to traverse all
    elements in a class that implements 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
16
Enhanced for Loop
  • The for loop has been enhanced to iterate over
    collections (class must implement Iterable)
  • General form
  • for (Type element collection)
  • element is the next thing visited each
    iteration
  • for (String key s)
  • System.out.println(key " ")

17
Collection 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)

18
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