Handout 3.5b - Collections- The Dictionary ADT.pdf

Handout 3.5b - Collections- The Dictionary ADT.pdf

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Kimani Njoroge. Collections: The Dictionary ADT Page 1 The Dictionary/Map ADT Sometimes we want to store data in an organized manner that allows us to quickly find what we are looking for. For example, consider a video store. Isn't it a nice idea to have the movies arranged by category so that you don't waste time looking over movies that are not of an interesting nature (such as musicals or perhaps drama)? You may also agree that it is easy to find a definition of a word in a dictionary because the definition is paired up with the word that it defines. Once we know the word that we want to look up, then we can find its definition. The word is therefore the unique key to finding the definition. We say that the definition of the word is the value associated with that key word. Likewise, a person's phone number is paired up with his/her name in a phonebook so that we can find numbers easily based the person's name as the key identifier. We say that the phone number is the value for the particular key person. This idea of a key-value pairing (or mapping) is the basis of the Dictionary ADT: A dictionary is an abstract data type that stores a collection of unique keys and their associated values. Each key is associated with a single value or a set of values. The key is always necessary in order to access a particular value in the dictionary. Like the Collection interface, the Map interface stores objects as well . So what is different? Well, a Map stores things in a particular way such that the objects can be easily located later on. A Map really means a "Mapping" of one object to another. A Map does not keep items in any particular index order, as with lists. In a way, however, instead of having integer indices, a Map has arbitrary objects as indices . So just as a unique index into a list or array identifies the object at that location, the key in the Map identifies the object associated with it. The basic methods for inserting , removing , accessing and modifying items from a Map are as follows: a) put(Object k, Object v) Place object v in the map with key k. If there is already a value at key k, it is replaced by v. e.g., aMap.put("Bob", "555-4444") will place 555-4444 into the location Bob represents. b) get(Object k) Return the value currently associated with key k. If k is not in the map yet, usually null is returned. e.g., v = aMap.get("Bob") will return 555-4444 c) remove(Object k) Remove the key/value pair associated with key k from the Map. Usually, the value associated with the key is returned. e.g., v = aMap.remove("Bob") will return 555-4444 d) size() Return the number of keys in the list. e.g., n = aMap.size() will return 4 if aMap contains four elements and values e) clear() Remove all elements from the list.
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Kimani Njoroge. Collections: The Dictionary ADT Page 2 There are additional methods often available for convenience sake. Here are some: i) containsKey(Object k) Return true if there is an entry in the map with key k, otherwise return false.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern