Computer Science
Java Map Interface
Java Map Interface is a part of the Java Collections Framework that provides a way to store and manipulate key-value pairs. It allows the user to access, insert, and remove elements based on their keys. The Map interface is implemented by several classes, including HashMap, TreeMap, and LinkedHashMap.
Written by Perlego with AI-assistance
Related key terms
1 of 5
5 Key excerpts on "Java Map Interface"
- eBook - PDF
Data Structures
Abstraction and Design Using Java
- Elliot B. Koffman, Paul A. T. Wolfgang(Authors)
- 2021(Publication Date)
- Wiley(Publisher)
A second, related interface is the Map. Map objects provide efficient search and retrieval of entries that consist of pairs of objects. The first object in each pair is the key (a unique value), and the second object is the information associated with that key. You retrieve an object from a Map by specifying its key. 7 C h a p t e r O b j e c t i v e s ◆ To understand the Java Map and Set interfaces and how to use them ◆ To learn about hash coding and its use to facilitate efficient search and retrieval ◆ To study two forms of hash tables—open addressing and chaining—and to understand their relative benefits and performance tradeoffs ◆ To learn how to implement both hash table forms ◆ To be introduced to the implementation of Maps and Sets ◆ To see how two earlier applications can be implemented more easily using Map objects for data storage ◆ To introduce NavigableMaps and NavigableSets and a data structure called the skip-list that can be used to implement them. 7.1 Sets and the Set Interface 323 We also study the hash table data structure. The hash table is a very important data structure that has been used very effectively in compilers and in building dictionaries. It can be used as the underlying data structure for a Map or Set implementation. It stores objects at arbitrary locations and offers an average constant time for insertion, removal, and searching. We will see two ways to implement a hash table and how to use it as the basis for a class that implements Map or Set. We will not show you the complete implementation of an object that implements Map or Set because we expect that you will use the ones provided by the Java API. However, we will certainly give you a head start on what you need to know to implement these interfaces. Finally, we look at the NavigableSet and NavigableMap interfaces, which provide great flexibility in the order in which we can access elements of a Map or Set. - eBook - PDF
- Todd Greanier(Author)
- 2006(Publication Date)
- Sybex(Publisher)
However, each key can refer to a single value at most. Different implementations of a map may allow or deny null keys and values. A map implementation can enforce ordering of its elements, but this is not required. A map can also have its contents extracted into a different collection type such as a set or a generic collection. The java.util.Map Interface All maps in the collections framework are implementations of the java .util.Map interface. This interface does not extend the Collection interface, however. This does not mean it is not part of the collection framework, of course. The Map interface actually provides a few methods that exactly match methods found in the Collection interface, but no others. For this reason, it does not extend Collection ; it would have been messy to handle all those extra-neous methods. The following list does not contain every method defined in Map , but it con-tains most of them. The first three methods are duplicates of methods in the Collection interface, so their descriptions will seem somewhat familiar. public void clear() Removes all the elements in a collection. The size of the collection after this method is called will be zero. The Collections Framework 295 public boolean isEmpty() Returns true if a particular collection con-tains no elements. public int size() Returns a count of the number of elements contained in a collection. public boolean containsKey(Object key) Returns true if the specified key is found in the Map . This is determined by calling the equals() method to compare it against each element until a match is found or until there are no more keys to check. There can be at most one key that matches. public boolean containsValue(Object value) Returns true if the specified value is found in the Map by using the equals() method for comparison. public Object get(Object key) Returns the element that maps to the specified key. - 15.4 Maps A map allows you to associate elements from a key set with elements from a value collection. You use a map when you want to look up objects by using a key. For example, Figure 10 shows a map from the names of people to their favorite colors. Just as there are two kinds of set imple- mentations, the Java library has two implementations for the Map interface: HashMap and TreeMap. After constructing a HashMap or TreeMap, you can store the reference to the map object in a Map reference: Map favoriteColors = new HashMap<>(); Use the put method to add an association: favoriteColors.put("Juliet", Color.RED); You can change the value of an existing association, simply by calling put again: favoriteColors.put("Juliet", Color.BLUE); The get method returns the value associated with a key. Color julietsFavoriteColor = favoriteColors.get("Juliet"); If you ask for a key that isn’t associated with any values, the get method returns null. To remove an association, call the remove method with the key: favoriteColors.remove("Juliet"); Sometimes you want to enumerate all keys in a map. The keySet method yields the set of keys. You can then ask the key set for an iterator and get all keys. From each key, you can find the associated value with the get method. Thus, the following instruc- tions print all key/value pairs in a map m whose keys have type String: Set keySet = m.keySet(); for (String key : keySet) { Color value = m.get(key); System.out.println(key + "->" + value); } Romeo Adam Eve Juliet Values Keys Figure 10 A Map The HashMap and TreeMap classes both implement the Map interface. To find all keys and values in a map, iterate through the key set and find the values that correspond to the keys. 526 Chapter 15 The Java Collections Framework Table 5 Working with Maps Map scores; Keys are strings, values are Integer wrappers.
- You can’t write efficient code if you don’t know whether the methods that you are calling are efficient or not. This is plainly a serious design error in the standard library, and it makes the List interface somewhat unattractive. S E L F C H E C K Programming Tip 15.1 706 Chapter 15 The Java Collections Framework 15.4 Maps A map allows you to associate elements from a key set with elements from a value collection . You use a map when you want to look up objects by using a key. For example, Figure 10 shows a map from the names of people to their favorite colors. Just as there are two kinds of set imple-mentations, the Java library has two implementations for the Map interface: HashMap and TreeMap . After constructing a HashMap or TreeMap , you can store the reference to the map object in a Map reference: Map favoriteColors = new HashMap <>(); Use the put method to add an association: favoriteColors.put(Juliet, Color.RED); You can change the value of an existing association, simply by calling put again: favoriteColors.put(Juliet, Color.BLUE); The get method returns the value associated with a key. Color julietsFavoriteColor = favoriteColors.get(Juliet); If you ask for a key that isn’t associated with any values, the get method returns null . To remove an association, call the remove method with the key: favoriteColors.remove(Juliet); Table 5 Working with Maps Map scores; Keys are strings, values are Integer wrappers. Use the interface type for variable declarations. scores = new TreeMap<>(); Use a HashMap if you don’t need to visit the keys in sorted order. scores.put(Harry, 90); scores.put(Sally, 95); Adds keys and values to the map. scores.put(Sally, 100); Modifies the value of an existing key. int n = scores.get(Sally); Integer n2 = scores.get(Diana); Gets the value associated with a key, or null if the key is not present.
- eBook - PDF
Java Concepts
Late Objects
- Cay S. Horstmann(Author)
- 2016(Publication Date)
- Wiley(Publisher)
However, the List interface has get and set methods for random access, even though these methods are very inefficient for linked lists. You can’t write efficient code if you don’t know whether the methods that you are calling are efficient or not. This is plainly a serious design error in the standard library, and it makes the List interface somewhat unattractive. S E L F C H E C K Programming Tip 15.1 706 Chapter 15 The Java Collections Framework 15.4 Maps A map allows you to associate elements from a key set with elements from a value collection. You use a map when you want to look up objects by using a key. For example, Figure 10 shows a map from the names of people to their favorite colors. Just as there are two kinds of set imple- mentations, the Java library has two implementations for the Map interface: HashMap and TreeMap. After constructing a HashMap or TreeMap, you can store the reference to the map object in a Map reference: Map favoriteColors = new HashMap<>(); Use the put method to add an association: favoriteColors.put("Juliet", Color.RED); You can change the value of an existing association, simply by calling put again: favoriteColors.put("Juliet", Color.BLUE); The get method returns the value associated with a key. Color julietsFavoriteColor = favoriteColors.get("Juliet"); If you ask for a key that isn’t associated with any values, the get method returns null. To remove an association, call the remove method with the key: favoriteColors.remove("Juliet"); Table 5 Working with Maps Map scores; Keys are strings, values are Integer wrappers. Use the interface type for variable declarations. scores = new TreeMap<>(); Use a HashMap if you don’t need to visit the keys in sorted order. scores.put("Harry", 90); scores.put("Sally", 95); Adds keys and values to the map. scores.put("Sally", 100); Modifies the value of an existing key.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.




