HashMap and TreeMap Processing
Objectives
In this lab, we will explore and demonstrate HashMap and TreeMap, and we will utilize the built-in methods.
Learning Objectives
By the end of this lab, learners will be able to use Hashmap and TreeMap in Java.
HashMap Examples
Example One: Remove HashMap Elements.
To remove elements from a hashmap, we can use the remove() method as shown below.
Create a new Java project and create a new Class named “ExampleOneHashMap ,” and then write the code below in the class.
import java.util.HashMap;
public class ExampleOneHashMap {
public static void main(String[] args) {
HashMap<Integer, String> languages = new HashMap<>();
languages.put(1, "Java");
languages.put(2, "Python");
languages.put(3, "JavaScript");
languages.put(4, "C Sharp");
System.out.println("HashMap: " + languages);
// remove element associated with key 2
String value = languages.remove(2);
System.out.println("Removed value: " + value);
System.out.println("Updated HashMap: " + languages);
}
}
Output
Here, the remove() method takes the key as its parameter. It then returns the value associated with the key and removes the entry. We can also remove the entry only under certain conditions. For example: remove(4, "C Sharp");
Here, the remove() method only removes the entry if the key 4 is associated with the value "C Sharp."
Example Two - Create Hashmap and Remove HashMap Elements
Here, we will do multiple things. We will first create a Hashmap. We will then get its values one by one. After that, we will copy all data of the HashMap to a brand-new HashMap. And finally, we will remove one item and get Hashmap sizes. If the size is lower by one, the decrease of size by removal is confirmed. Create a new Class named “ExampletwoHashMap,” and then write the code below in the class.
Output:
TreeMap Examples
Example One: Remove TreeMap Elements.
remove(key) - returns and removes the entry associated with the specified key from a TreeMap. remove(key, value) - removes the entry from the map only if the specified key is associated with the specified value, and returns a boolean value.
Create a new Class named “exampleTreemapOne,” and then write the code below in the class.
Output:
Example Two: Methods for Navigation.
TreeMap class also implements NavigableMap; it provides various methods to navigate over the elements of the treemap. firstKey() - returns the first key of the map. firstEntry() - returns the key/value mapping of the first key of the map. lastKey() - returns the last key of the map. lastEntry() - returns the key/value mapping of the last key of the map. Create a new Class named “exampleTreemapTwo,” and then write the code below in the class.
Output
Example Three: TreeMap Comparator.
TreeMap elements are sorted in ascending order. However, we can also customize the ordering of keys. For this, we need to create our comparator class based on which keys in a TreeMap are sorted.
Create a new Class named “CustomComparator,” and then write the below code in the class.
Create a new Class named “maincomparator,” and then write the code below in the class.
Output
Last updated