Thursday, 1 June 2017

Java Collections Tutorial 4: Sorted Maps



In the last tutorial, we looked at the HashMap. Towards the end of the tutorial, I told you that HashMaps don’t put your keys and values in any kind of order. If you would, however, like to have order in your keys and values, there are two options, both of which implement the Map interface.

The first option is a LinkedHashMap. Now a LinkedHashMap is similar to a HashMap, only that it has a doubly linked list that connects the items in the list. This is just the same implementation as the LinkedList which we saw in a last tutorial, only that in a LinkedHashMap, each item has a key and a value. The hash code is used to store things in a HashMap and LinkedHashMap.

LinkedHashMap<Integer, String> numbers = new LinkedHashMap<Integer, String>();

Adding Items to LinkedHashMap

So to add items to the LinkedHashMap, we normally use the put() method like this:

numbers.put(3, “Lucy”);
numbers.put(5, “Joseph”);
numbers.put(10, “Anthony”);
numbers.put(4, “Wayne”);

If you want to access the values referred to by the keys, we use the get() method like this:

String name = numbers.get(10);

If we output the above operation using System.out.println, it will output “Anthony”.
Figure 1: Adding items to a LinkedHashMap


Removing Items from LinkedHashMap

If we want to remove an item from a LinkedHashMap, we use the remove() method, in which we pass the key of the value we want to remove. So, let’s say that in the above LinkedHashMap, we want to remove the key-value pair [5, “Joseph”]. Here is how we’d do it:

numbers.remove(5);

Iterating through a LinkedHashMap

There is a specific syntax for iterating through a LinkedHashMap. For most people, including myself, the easiest way is to Google the syntax for iterating through a LinkedHashMap and then copying and pasting it. However, over time, you will get the hang of it. So here is the syntax with the above example LinkedHashMap:

for(Map.Entry<Integer, String> entry: numbers.entrySet()) {
            int key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + “: ” + value);
}
 Figure 2: Removing and iterating through items in a LinkedHashMap


The second option is a TreeMap. A TreeMap sorts the values that you add to it using the natural order. The words “natural order” mean for integers, it’s 1, 2, 3, 4, 5… In String, it is a, b, c, d, e etc. Also, Java allows you to define a natural order for your classes, which we will look at in a later tutorial. Anyway, for a TreeMap, these are the operations:

TreeMap<Integer, String> numbers = new TreeMap<Integer, String>();

Adding Items to TreeMap


So to add items to the TreeMap, we normally use the put() method like this:

numbers.put(3, “Lucy”);
numbers.put(5, “Joseph”);
numbers.put(10, “Anthony”);
numbers.put(4, “Wayne”);

If you want to access the values referred to by the keys, we use the get() method like this:

String name = numbers.get(10);

As you can see from the outputs of the TreeMap, they are arranged in their natural order, i.e., ascending numerical order for numerals.
 Figure 3: Adding items to a TreeMap


If we output the above operation using System.out.println, it will output “Anthony”.

Removing Items from TreeMap

If we want to remove an item from a TreeMap, we use the remove() method, in which we pass the key of the value we want to remove. So, let’s say that in the above TreeMap, we want to remove the key-value pair [5, “Joseph”]. Here is how we’d do it:

numbers.remove(5);

Iterating through a TreeMap

There is a specific syntax for iterating through a TreeMap. For most people, including myself, the easiest way is to Google the syntax for iterating through a TreeMap and then copying and pasting it. However, over time, you will get the hang of it. So here is the syntax with the above example TreeMap:

for(Map.Entry<Integer, String> entry: numbers.entrySet()) {
            int key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + “: ” + value);
}
 Figure 4: Removing and iterating through items in a TreeMap


As you can see from the outputs of the TreeMap, they are arranged in their natural order, i.e., ascending numerical order for numerals.

Remember that HashMap doesn’t guarantee any type of order. If you want order in your key-value pairs, consider using a TreeMap or a LinkedHashMap.
That’s all for this tutorial and if you have any questions or comments, please drop them in the comments section below and I will make sure to address them. 

In the next tutorial, we are going to look at Sets, which is another member of the Java collections framework.

Until next time, stay safe.   

No comments:

Post a Comment