Map and Sets - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Map and Sets

Description:

Title: Java's Collection Framework Subject: Lecture notes for CmpSc 101, 201, 203 Author: Rick Mercer - Instructor of Engineering and Computer Science – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 16
Provided by: RickM48
Category:

less

Transcript and Presenter's Notes

Title: Map and Sets


1
Java's Collection Framework
  • Map and Sets

2
Collection Framework
  • A collections 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

3
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

4
Collection interfaces
Queuesince Java 5
5
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 implements interfaces with
  • List ArrayList, LinkedList, Vector
  • Map HashMap, TreeMap
  • Set TreeSet, HashSet

6
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)

7
Two Useful ADTs
  • List a collection with a first element, a last
    element, distinct predecessors and successors
  • duplicates that "equals" each other are allowed
  • Map maps keys to values
  • Maps cannot contain duplicate keys
  • Each key maps at most one value

8
Map and SortedMap
  • The Map interface defines methods
  • get, put, contains, keySet, values, entrySet
  • TreeMap implements Map
  • put, get, remove O(log n)
  • HashMap implements Map
  • put, get, remove O(1)

9
Set and SortedSet
  • Some Map methods return Set
  • 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)

10
Let's play
  • Set up a small collection of mappings
  • MapltString, BankAccountgt tm
  • new TreeMapltString,
    BankAccountgt()
  • tm.put("M", new BankAccount("Michel",
    111.11))
  • tm.put("G", new BankAccount("Georgie",
    222.22))
  • tm.put("S", new BankAccount("Sam", 333.33))
  • tm.put("P", new BankAccount("Pei", 444.44))
  • tm.put("Z", new BankAccount("Zac", 555.55))
  • // Coming up Some Map methods

11
keySet()
  • // Get the set of keys in the Map.
  • // With TreeMap, the keys are ordered.
  • SetltStringgt keys tm.keySet()
  • assertEquals("G, M, P, S, Z", keys.toString())

12
values()
  • // The values
  • CollectionltBankAccountgt accountCollection
  • tm.values()
  • IteratorltBankAccountgt itr
  • accountCollection.iterator()
  • double allTheMoney 0.0
  • while (itr.hasNext())
  • allTheMoney itr.next().getBalance()
  • assertEquals(1666.65, allTheMoney, 0.01)

13
entrySet()
  • // The mappings
  • CollectionltEntryltString, BankAccountgtgt mapping
  • tm.entrySet()
  • IteratorltEntryltString, BankAccountgtgt itr2
  • mapping.iterator()
  • while (itr2.hasNext())
  • System.out.print("lt" itr2.next().toString()
  • "gt ")
  • ltGGeorgie 222.22gt ltMMichel 111.11gt
  • ltPPei 444.44gt ltSSam 333.33gt ltZZac 555.55gt

14
Maps do not have an iterator
  • Code Demo Sum up all balances in tm
  • There is no iterator method in TreeMap

15
Thank You
Write a Comment
User Comments (0)
About PowerShow.com