Learn

Previously, we saw the collection framework hierarchy and how Collection is the root of it. There exists another interface that didn’t quite fit into that hierarchy but is an extremely important part of the collection framework, the Map.

Map defines a generic interface for an object that holds key-value pairs as elements. The key is used to retrieve (like the index in an array or List) some value. A key must be unique and map to exactly one value. A Map can be thought of as a Java interface for a Hash Table data structure. We are given many implementations of a Map but we’ll focus on the HashMap, LinkedHashMap, and TreeMap implementations.

The HashMap defines no specific ordering for the keys and is the most optimized implementation for retrieving values.

The LinkedHashMap keeps the keys in insertion order and is slightly less optimal than the HashMap.

The TreeMap keeps keys in their natural order (or some custom order defined using a Comparator). This implementation has a significant performance decrease compared to HashMap and LinkedHashMap but is great when needing to keep keys sorted.

A Map has the following methods for accessing its elements:

put(): Sets the value that key maps to. Note that this method replaces the value key mapped to if the key was already present in the Map.

get(): Gets, but does not remove, the value the provided key argument points to if it exists. This method returns null if the key is not in the Map. Let’s see how we can use them:

Map<String, String> myMap = new HashMap<>(); myMap.put("Hello", "World") // { "Hello" -> "World" } myMap.put("Name", "John") // { "Hello" -> "World" }, { "Name" -> "John" } String result = myMap.get("Hello") // returns "World" String noResult = myMap.get("Jack") // return `null`

In the example above we:

  • Created a Map reference that maps a String key to a String value using the HashMap implementation. Note that a Map needs a type argument for the key and value.
  • Added key-value pairs to myMap using put() where the first argument is the key and the second is the value the key maps to.
  • Retrieved the value the key “Hello” maps to using get(). The String “World” is returned since the key “Hello” exists. get() returns null because the key “Jack” does not exist.

We can iterate over a Map with an enhanced for-loop like:

// Given a map, `myMap`, with the following key-value pairs { "Hello" -> "World" }, { "Name" -> "John"} for (Map.Entry<String, String> pair: myMap.entrySet()){ System.out.println("key: "+pair.getKey()+", value: "+pair.getValue()); } // OUTPUT TERMINAL: // key: Name, value: John // key: Hello, value: World

In the example above we:

  • Called entrySet() that returns a Set of type Map.Entry which represents a key-value pair.
  • We define our iterating variable of type Map.Entry<String, String> to specify what type of key-value pair we are iterating over.
  • Called getKey() of Map.Entry to get the key of the pair.
  • Called getValue() of Map.Entry to get the value of the pair.

Let’s practice working with Map.

Instructions

1.

In Main.java we’ve defined a method named countNumbers() that should take an Integer List named list as a parameter and returns a Map of the Integer as a key and the number of times it appears in the list as its value.

Let’s complete the body of countNumbers by first defining a Map reference where the keys and values will be Integers and name it intCount. Use a Map implementation that will keep keys stored in sorted order.

Note: It’s okay to see an error in the console about a missing return statement.

2.

Let’s iterate through list and keep count of the Integers we come across.

In countNumbers() define an enhanced for-loop that iterates over list using an Integer named i. Complete the following tasks:

  • In the body of the for-loop define an Integer reference named currentCount and set it to the value returned from getting i from intCount.
  • Create and if block that checks if currentCount is not null.
  • In the body of the if block create an int type named newCount and set it equal to currentCount incremented by 1.
  • In the body of the if block update the key i in intCount with newCount.
  • Create an else block that will add the key i to intCount with an initial value of 1.

Note: It’s okay to see an error in the console about a missing return statement.

3.

Let’s return are completed count of Integers.

In countNumbers() return intCount and run the program. You should see the integers and the number of times they’ve appeared in myInts.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?