1 / 7

Map Collections and Custom Collection Classes

Chapter 14. Map Collections and Custom Collection Classes. Maps. An object that maps a key to a value Key maps to one value A map cannot have duplicate keys Often used to associate a short key with a longer value Example: Dictionary Example: Employee database using employee ID number.

pilar
Download Presentation

Map Collections and Custom Collection Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 14 Map Collections andCustom Collection Classes

  2. Maps • An object that maps a key to a value • Key maps to one value • A map cannot have duplicate keys • Often used to associate a short key with a longer value • Example: Dictionary • Example: Employee database using employee ID number

  3. Concrete Map Implementations • TreeMap orders the keys. • HashMap stores the keys in a hash table. • HashMap class is generally preferred for its efficiency (speed) unless sorted keys are needed.

  4. Retrieving Values by Key • Use the keys collection to retrieve the keys in a map. • Iterator keyIterator = myMap.keySet().iterator(); • Keys collection holds “key” objects. • Must cast key values to the appropriate type • Integer key = (Integer)keyIterator.next(); • Retrieves the next key from keyIterator (collection of keys) then casts it to an Integer and assigns it to the variable key

  5. Custom Collection Classes • Custom collections allow you to define the data storage method. • Good way to understand how other collection classes work • Think through issues involved in writing good generic classes

  6. Linked List • A linked list is a type of collection, similar to a train. • Each object or node is linked to the next object in the collection. • Singly linked list • Each node knows the node following it • Can only be iterated in one direction • Doubly linked list • Each node knows the node preceding it and the node following it • Can be iterated in both directions

  7. Linked List Hierarchy • Head node points to next node in list • Internal node “carries” an object (the value being stored) and points to next node in list • Tail node signals end of the list

More Related