Java Map Interface
Java Map Interface
import java.util.Map;
import java.util.HashMap;
class Main {
Output:
Map: {One=1, Two=2}
Keys: [One, Two]
Values: [1, 2]
Entries: [One=1, Two=2]
Removed Value: 2
class Main {
Output:
Map using TreeMap: {First=1, Second=2}
New Map: {First=11, Second=22}
Removed Value: 11
// create a hashmap
HashMap<String, Integer> numbers = new HashMap<>();
Output:
Initial HashMap: {}
HashMap after put(): {One=1, Two=2, Three=3}
Output:
HashMap: {1=Java, 2=Python, 3=JavaScript}
Value at index 1: Java
We can also access the keys, values, and key/value pairs of the hashmap
as set views using keySet(), values(), and entrySet() methods respectively.
For example,
import java.util.HashMap;
class Main {
public static void main(String[] args) {
HashMap<Integer, String> languages = new HashMap<>();
languages.put(1, "Java");
languages.put(2, "Python");
languages.put(3, "JavaScript");
System.out.println("HashMap: " + languages);
Output:
HashMap: {1=Java, 2=Python, 3=JavaScript}
Keys: [1, 2, 3]
Values: [Java, Python, JavaScript]
Key/Value mappings: [1=Java, 2=Python, 3=JavaScript]
class Main {
public static void main(String[] args) {
Output:
Original HashMap: {1=Java, 2=Python, 3=JavaScript}
HashMap using replace(): {1=Java, 2=C++, 3=JavaScript}
6. Remove HashMap Elements
To remove elements from a hashmap, we can use the remove() method.
For example,
import java.util.HashMap;
class Main {
public static void main(String[] args) {
Output:
HashMap: {1=Java, 2=Python, 3=JavaScript}
Removed value: Python
Updated HashMap: {1=Java, 3=JavaScript}
7. Iterate through a HashMap
To iterate through each entry of the hashmap, we can use Java for-each
loop. We can iterate through keys only, vales only, and key/value
mapping. For example,
import java.util.HashMap;
import java.util.Map.Entry;
class Main {
public static void main(String[] args) {
// create a HashMap
HashMap<Integer, String> languages = new HashMap<>();
languages.put(1, "Java");
languages.put(2, "Python");
languages.put(3, "JavaScript");
System.out.println("HashMap: " + languages);
Output:
HashMap: {1=Java, 2=Python, 3=JavaScript}
Keys: 1, 2, 3,
Values: Java, Python, JavaScript,
Entries: 1=Java, 2=Python, 3=JavaScript,
import java.util.HashMap;
import java.util.TreeMap;
class Main {
public static void main(String[] args) {
// create a treemap
TreeMap<String, Integer> evenNumbers = new TreeMap<>();
evenNumbers.put("Two", 2);
evenNumbers.put("Four", 4);
System.out.println("TreeMap: " + evenNumbers);
Output:
TreeMap: {Four=4, Two=2}
HashMap: {Two=2, Three=3, Four=4}
Output:
100=Amit
101=Vijay
102=Rahul
Output:
102=Rahul
101=Vijay
100=Amit
Output:
100=Amit
102=Rahul
101=Vijay
12. Java Map Example: comparingByValue() in Descending Order
import java.util.*;
class MapExample6{
public static void main(String args[]){
Map<Integer,String> map=new HashMap<Integer,String>();
map.put(100,"Amit");
map.put(101,"Vijay");
map.put(102,"Rahul");
//Returns a Set view of the mappings contained in this map
map.entrySet()
//Returns a sequential Stream with this collection as its source
.stream()
//Sorted according to the provided Comparator
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
//Performs an action for each element of this stream
.forEach(System.out::println);
}
}
Output:
101=Vijay
102=Rahul
100=Amit
13. Synchronized HashMap
Using Collections.synchronizedMap() method.
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;