Big Java - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Big Java

Description:

Set. HashSet. TreeSet. Example. instantiate with specific. implementation. Advantage: could easily ... For TreeMap, same requirement for keys ... – PowerPoint PPT presentation

Number of Views:67
Avg rating:3.0/5.0
Slides: 17
Provided by: cyndi8
Category:
Tags: java | keys

less

Transcript and Presenter's Notes

Title: Big Java


1
Big Java
  • Chapter 16

2
Advanced Data Structures
  • Often referred to as the Java Collections
    Framework.
  • Set and map data types
  • Hash tables
  • Binary trees
  • Heap
  • Priority queue

3
Sets
  • Unordered collection
  • Fundamental operations
  • Add an element
  • Remove an element
  • Containment test (is object in set?)
  • List elements (in arbitrary order)
  • Reject duplicates

4
Set Implementation
ltltinterfacegtgt Set
HashSet
TreeSet
5
Example
instantiate with specific implementation. Advantag
e could easily change implementation, rest of
program stays the same.
use methods from interface
could also do if (names.contains(Jane))

6
Example (continued)
can use for each loop with Sets Nodes are
not necessarily visited in order inserted!
7
Reading Assignment
  • Read Quality Tip 16.1 pages 704-705. How would
    you feel if you had designed the List interface
    and ArrayList/LinkedList classes?

8
Maps
  • A map data type keeps associations between keys
    and values
  • Cannot contain duplicate keys
  • Operations include
  • put
  • get
  • containsKey/containsValue
  • keySet/values return all keys/values
  • size
  • Java has two implementations HashMap and
    TreeMap.

9
Example
10
Hash Table
  • Hash table can be used to implement sets and
    maps
  • Hash function computes an integer value (hash
    code) from an object, goal is for different
    objects to yield different hash codes
  • int h obj.hashCode()
  • hashCode is inherited from Object
  • Collision when two or more distinct objects
    have the same hash code

11
HashSet
  • Author provides HashSet, an extension of
    AbstractSet
  • provides a hash table with buckets (linked lists)
    to hold collisions
  • possible hash code for string
  • final int HASH_MULTIPLER 31 //prime
  • int h0
  • for (int i0 ilts.length() i)
  • h HASH_MULTIPLIER h s.charAt(i)
  • Can use integer fields directly as hash codes
  • If hashCode not overridden, Object class computes
    based on memory location of object. Problem if
    define equals but not hashCode.

12
Binary Search Tree
  • Review on your own, pages 720-734

13
TreeSet or HashSet?
  • With a good hash function, hashing is usually
    faster
  • Balanced trees (remember those?) can guarantee
    reasonable performance, HashSet depends entirely
    on performance of hash function
  • To use TreeSet, objects being stored must
    implement Comparable interface
  • For TreeMap, same requirement for keys
  • Can supply a Comparator object to TreeSet/TreeMap
    constructor (takes two objects and returns
    comparison)
  • TreeSet is preferable if want to print list of
    items in order

14
Reading Assignment
  • Random Fact 16.2, Software Piracy, pages 738-739

15
Priority Queue
  • Collects elements which have a priority
  • Elements inserted in any order, but retrieved
    according to priority
  • Java includes PriorityQueue() class
  • Abstract data class
  • Often use heap to implement
  • Heaps covered in 406 (I think)
  • Heapsort is O(n log(n))

16
Chapter 16 Quick Exercise
  • Use the HashTable code from the textbook
  • Can you store objects with the same hash code but
    different data? Try modifying CoinHashCodePrinter
    to insert two quarters but initialize them
    differently (e.g., a quarter and quarter
    rather than both quarter)
  • Review the TreeSet and TreeSetTester code.
    Notice the use of CoinComparator. Modify to use
    with some other class, such as BankAccount
Write a Comment
User Comments (0)
About PowerShow.com