Hello coders, I am back with a new article in my Java Collections Framework article series. Today, I am going to discuss the HashMap class in Java. HashMap extends the Map interface in Java. Let’s learn about HashMaps.
What is HashMap?
HashMap is a class that is being used for data mapping. We use HashMaps to store data in key & value pairs. HashMaps do not maintain order. So, you should not use HashMaps, if you expect your list to keep order. In addition, HashMaps allow null values and this implementation is not synchronized.
Let’s learn how you can use HashMaps to store data.
HashMap Class Methods
1. value put(key, value)
This will add a key and value pair to the map. The method returns the value that was paired with the given key earlier. If the given key was never used in the map before, it will return null.
//Creating HashMap
HashMap<Integer, String> myHashMap = new HashMap<>();
//Adding values
myHashMap.put(1, "Thisura");
System.out.println(myHashMap.put(2, "Mahela"));
System.out.println(myHashMap.put(2, "Sanga"));
Here is the console output.
null
Mahela
2. value get (Object key)
This method returns the values paired with the given key.
//Creating HashMap
HashMap<Integer, String> myHashMap = new HashMap<>();
//Adding values
myHashMap.put(1, "Thisura");
myHashMap.put(2, "Mahela");
myHashMap.put(3, "Sanga");
System.out.println(myHashMap.get(3));
Here is the console output
Sanga
3. boolean isEmpty()
This method returns true if the map is empty.
//Creating HashMap
HashMap<Integer, String> myHashMap = new HashMap<>();
//Adding values
myHashMap.put(1, "Thisura");
myHashMap.put(2, "Mahela");
myHashMap.put(3, "Sanga");
System.out.println(myHashMap.isEmpty());
Here is the console output.
false
4. boolean containsKey (Object key)
This returns true if the given key is there in the map.
//Creating HashMap
HashMap<Integer, String> myHashMap = new HashMap<>();
//Adding values
myHashMap.put(1, "Thisura");
myHashMap.put(2, "Mahela");
myHashMap.put(3, "Sanga");
System.out.println(myHashMap.containsKey(2));
Here is the console output.
true
5. boolean containsValue(Object value)
This returns true if the given value is there in the map.
//Creating HashMap
HashMap<Integer, String> myHashMap = new HashMap<>();
//Adding values
myHashMap.put(1, "Thisura");
myHashMap.put(2, "Mahela");
myHashMap.put(3, "Sanga");
System.out.println(myHashMap.containsValue("Sanga"));
Here is the console output.
true
6. Set keySet()
This method returns the set of keys in the map.
//Creating HashMap
HashMap<Integer, String> myHashMap = new HashMap<>();
//Adding values
myHashMap.put(1, "Thisura");
myHashMap.put(2, "Mahela");
myHashMap.put(3, "Sanga");
System.out.println(myHashMap.keySet());
Here is the console output.
[1, 2, 3]
7. int size()
This returns the size of the map.
//Creating HashMap
HashMap<Integer, String> myHashMap = new HashMap<>();
//Adding values
myHashMap.put(1, "Thisura");
myHashMap.put(2, "Mahela");
myHashMap.put(3, "Sanga");
System.out.println(myHashMap.size());
Here is the console output.
3
8. value remove(Object key)
This method removes the key and value pair according to the specified key and returns the values that key was paired to.
//Creating HashMap
HashMap<Integer, String> myHashMap = new HashMap<>();
//Adding values
myHashMap.put(1, "Thisura");
myHashMap.put(2, "Mahela");
myHashMap.put(3, "Sanga");
System.out.println(myHashMap.remove(2));
System.out.println(myHashMap);
Here is the console output.
Mahela
{1=Thisura, 3=Sanga}
I believe these methods are the important ones you should know to get started with HashMaps. If you are interested in learning more about HashMaps, go ahead and read the Java documentation here.
I am going to wrap up this article here. Thank you for reading my article. I hope you learned something valuable from it. If you did, drop a like and follow my blog to get notifications when I publish new articles. I write articles about coding, cricket and things that I find interesting. I try to publish articles every other day. Have a nice day ✌