When it is required to store data key, value pairs you can use Either java HashMap, LinkedHashMap or TreeMap. There are slight differences amongst the classes. But all of the classes implement Map interface.
# Syntax
HashSet map = new HashMap();
//Like:
HashSet map = new HashMap();
//Integer will be use as key and String will be used as values.
Note: The key in HashMap must be unique, but the values can be duplicate. Null values are allowed in key and values but there is only one null value supported as key. Also, remember HashMap doesn't maintain insertion order. And if you want to iterate HashMap or Map interface, you have to use Map.Entry and entrySet() method. Map.Entry contains getKey and getValue() method. For getting the instance of Map.Entry you have to use the entrySet() method overall.
# There are many useful methods to work with java HashMap class or Map interface.
Methods | Description |
---|---|
put(K key, V value) | Insert new element with the specified value with the specified key in this map. |
putAll(Map m) | Copies all of the mappings from the specified map to this map. |
remove(Object key) | Removes the mapping for the specified key from this map if present. |
size() type int | Returns the number of key-value mappings in this map. |
clear() void | Removes all of the mappings from this map. |
clone() | Returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned. |
containsKey(Object key) type boolean | Returns true if this map contains a mapping for the specified key. |
containsValue(Object value) boolean | Returns true if this map maps one or more keys to the specified value. |
isEmpty() boolean | Returns true if this map contains no key-value mappings. |
size() int | Returns the number of key-value mappings in this map. |
values() | Returns a Collection view of the values contained in this map. |
compute() | Attempts to compute a mapping for the specified key and its current mapped value |
keySet() | Returns a Set view of the keys contained in this map |
# Creating HashMap and add new elements based on key, value pairs
import java.util.HashMap;
public class Main{
public static void main (String[] args) {
//creating HashMap
HashMap map = new HashMap();
//add new elements using put() method
map.put(1, "Mark Smith");
map.put(2, "Bob Smith");
map.put(3, "Jekov Jenkov");
map.put(4, "Shakil Ahmed");
map.put(5, "Jhon Peterson");
//print the HashMap
System.out.println(map);
}
}
Output you will get in the console:
{1=Mark Smith, 2=Bob Smith, 3=Jekov Jenkov, 4=Shakil Ahmed, 5=Jhon Peterson}
Note: in square bracket we use
# Retrieve specific value based on key using the get(Key) method
import java.util.HashMap;
public class Main{
public static void main (String[] args) {
//creating HashMap
HashMap map = new HashMap();
//add new elements using put() method
map.put(1, "Mark Smith");
map.put(2, "Bob Smith");
map.put(3, "Jekov Jenkov");
map.put(4, "Shakil Ahmed");
map.put(5, "Jhon Peterson");
//print the HashMap
System.out.println(map);
//get the first value usin get(key)
System.out.println(map.get(1));
//get the second value
System.out.println(map.get(2));
//get the last value
System.out.println(map.get(5));
}
}
After running above example you will get the following output in the console
{1=Mark Smith, 2=Bob Smith, 3=Jekov Jenkov, 4=Shakil Ahmed, 5=Jhon Peterson}
Mark Smith
Bob Smith
Jhon Peterson
Note: it is also possible though the key is String. (But case sensitive)
# Iterate HashMap
import java.util.HashMap;
public class Main{
public static void main (String[] args) {
//creating HashMap
HashMap map = new
HashMap();
//add new elements using put() method
map.put(1, "Mark Smith");
map.put(2, "Bob Smith");
map.put(3, "Jekov Jenkov");
map.put(4, "Shakil Ahmed");
map.put(5, "John Peterson");
//iterate HashMap (keys and values)
for(Map.Entry value: map.entrySet()){
System.out.println(value.getKey +" "+value.getaValue);
}
}
}
/**
* Output: (together keys and values)
* 1 Mark Smith
* 2 Bob Smith
* 3 Jekov Jenkov
* 4 Shakil Ahmed
* 5 John Peterson
*/
# Inserting new elements using put, putAll and putIfAbsent method and then iterate
import java.util.HashMap;
public class Main{
public static void main (String[] args) {
//creating HashMap
HashMap map = new
HashMap();
//add new elements using put() method
map.put(1, "Mark Smith");
map.put(2, "Bob Smith");
map.put(3, "Jekov Jenkov");
map.put(4, "Shakil Ahmed");
map.put(5, "John Peterson");
//initial map
System.out.println("Initial List");
System.out.println(map);
//using putIfAbsent() method
map.putIfAbsent(6, "Abdul Hamid");
map.putIfAbsent(3, "Eugune Uzbek"); // will not be updated
//updated list
System.out.println("Updated List using putIfAbsent");
System.out.println(map);
//Creating new entry
HashMap newMap = new HashMap();
//add new elements
newMap.put(7, "Rebeka Ghom");
newMap.putAll(map); //using putAll()
//print newMap
System.out.println("Updated List using putAll()");
System.out.println(newMap);
//iterate map and newMap now
System.out.println("map iteration");
for(Map.Entry value: map.entrySet()){
System.out.println(value.getKey +" "+value.getaValue);
}
System.out.println("newMap iteration");
for(Map.Entry value: newMap.entrySet()){
System.out.println(value.getKey +" "+value.getaValue);
}
}
}
If you run the above example, you will get the following output
//output (together with keys and values)
Initial List
{1=Mark Smith, 2=Bob Smith, 3=Jekov
Jenkov, 4=Shakil Ahmed, 5=Jhon
Peterson}
Updated List using putIfAbsent()
{1=Mark Smith, 2=Bob Smith, 3=Jekov Jenkov, 4=Shakil Ahmed, 5=Jhon Peterson, 6=Abdul Hamid}
Updated List using putAll
{1=Mark Smith, 2=Bob Smith, 3=Jekov Jenkov, 4=Shakil Ahmed, 5=Jhon Peterson, 6=Abdul Hamid, 7=Rebeka Ghom}
map iteration
1 Mark Smith
2 Bob Smith
3 Jekov Jenkov
4 Shakil Ahmed
5 John Peterson
6 Abdul Hamid
newMap iteration
1 Mark Smith
2 Bob Smith
3 Jekov Jenkov
4 Shakil Ahmed
5 John Peterson
6 Abdul Hamid
7 Rebeka Ghom
# Remove any entry based on key, value, and both (keys abd values) from HashMap
import java.util.HashMap;
public class Main{
public static void main (String[] args) {
//creating HashMap
HashMap map = new
HashMap();
//add new elements using put() method
map.put(1, "Mark Smith");
map.put(2, "Bob Smith");
map.put(3, "Jekov Jenkov");
map.put(4, "Shakil Ahmed");
map.put(5, "John Peterson");
//print initial HashMap
System.out.println("Initial HashMap: "+map);
//remove based on key
map.remove(5)
//print updated list
System.out.println("Updated Entry "+map);
//remove based on value
map.remove("Shakil Ahmed")
//print updated list
System.out.println("Updated Entry "+map);
//remove based on keys and values
map.remove(3, "Jekov Jenkov");
//print update list
System.out.println("Update d Entry " +map);
//now iterate HashMap (after removal)
for(Map.Entry value: map.entrySet()){
System.out.println(value.getKey +" "+value.getaValue);
}
}
}
You get the following output after running the above example
//Output
Initial HashMap: {1=Mark Smith, 2=Bob Smith, 3=Jekov
Jenkov, 4=Shakil Ahmed, 5=Jhon
Peterson}
Updated Entry: {1=Mark Smith, 2=Bob Smith, 3=Jekov
Jenkov, 4=Shakil Ahmed}
Updated Entry: {1=Mark Smith, 2=Bob Smith, 3=Jekov
Jenkov,}
Updated Entry: {1=Mark Smith, 2=Bob Smith}
//iteration entry after removal
1 Mark Smith
2 Bob Smith