What is a Hashmap?
HashMap class is one of the popular classes among all collection classes, and it stores elements in the form of key and value pairs, and it is declared as HashMap <Key, Value>. If the key is provided later, its corresponding value can be easily retrieved from the Hashmap, but the key should be unique. This means we can not use duplicate data for keys in the hashmap. However, Hashmap is not synchronized, and hence, while using multiple threads on the HashMap object, we get unreliable results.
Key Points about HashMap
- It is a part of java.util package.
- It Stores elements in the form of key-value pairs, and the keys should be unique.
- It does not allow duplicate keys.
- A class can have one null key and multiple null values.
- HashMap class extends the abstract class AbstractMap class, which has not complete implements of the Map interface.
- It’s not an ordered collection that means it does not return the key and values in the same order on which order they have inserted.
- If you want to retrieve the value, then you need to use the associated key.
- It implements Cloneable and Serializable interfaces.
- It is not threaded safe, but we can make it thread-safe by using
Map m = Collections.synchronizedMap(new HashMap(“XXXX”));
Constructors in HashMap Class
HashMap provides four constructors, and access modifier of each is public:
- HashMap(): It is the default constructor that creates an instance of HashMap with initial capacity 16 and load factor 0.75.
- HashMap(int initial capacity): It creates a HashMap instance with a specified initial size and load factor 0.75.
- HashMap(int initial capacity, float loadFactor): It creates a HashMap instance with a specified initial size and specified load factor.
- HashMap(Map map): It creates an instance of HashMap with the same mappings as a specified map.
HashMap Methods
How to Verify Hashmap Elements By Value Of In Java?
package com.java.Softwaretestingblog; import java.util.HashMap; public class VerifyHashmapElements { public static void main(String[] args) { // TODO Auto-generated method stub HashMap<String, String> hm = new HashMap<String, String>(); //add key-value pair to hashmap hm.put("first", "FIRST INSERTED"); hm.put("second", "SECOND INSERTED"); hm.put("third","THIRD INSERTED"); System.out.println(hm); if(hm.containsValue("SECOND INSERTED")) { System.out.println("The hashmap contains value SECOND INSERTED"); } else { System.out.println("The hashmap does not contains value SECOND INSERTED"); } if(hm.containsValue("first")) { System.out.println("The hashmap contains value first"); } else { System.out.println("The hashmap does not contains value first"); } } }
Output:
{third=THIRD INSERTED, first=FIRST INSERTED, second=SECOND INSERTED} The hashmap contains value SECOND INSERTED The hashmap does not contains value first
Write a Program to Add & Retrieve Elements In Hashmap Java?
package com.java.collection.hashmap; import java.util.HashMap; import java.util.Set; public class MyHashMapKeys { public static void main(String[] args) { HashMap<String, String> hm = new HashMap<String, String>(); //add key-value pair to hashmap hm.put("first", "FIRST INSERTED"); hm.put("second", "SECOND INSERTED"); hm.put("third","THIRD INSERTED"); System.out.println(hm); Set<String> keys = hm.keySet(); for(String key: keys) { System.out.println(key); } } }
Java Hashmap In Java: Write a Program to Add New Values In Java Hashmap With Example?
package com.java.collection.hashmap; import java.util.HashMap; import java.util.Map.Entry; import java.util.Set; public class MyHashMapEntrySet { public static void main(String[] args) { HashMap<String, String> hm = new HashMap<String, String>(); //add key-value pair to hashmap hm.put("first", "FIRST INSERTED"); hm.put("second", "SECOND INSERTED"); hm.put("third","THIRD INSERTED"); System.out.println(hm); //getting value for the given key from hashmap Set<Entry<String, String>> entires = hm.entrySet(); for(Entry<String,String> ent:entires) { System.out.println(ent.getKey()+" ==> "+ent.getValue()); } } }
Write a Program to Find Basic Hashmap Operation Of In Java?
package com.java.Softwaretestingblog; import java.util.HashMap; public class BasicHashmapOperation { public static void main(String[] args) { // TODO Auto-generated method stub HashMap<String, String> hm = new HashMap<String, String>(); //add key-value pair to hashmap hm.put("first", "FIRST INSERTED"); hm.put("second", "SECOND INSERTED"); hm.put("third","THIRD INSERTED"); System.out.println(hm); //getting value for the given key from hashmap System.out.println("Value of second: "+hm.get("second")); System.out.println("Is HashMap empty? "+hm.isEmpty()); hm.remove("third"); System.out.println(hm); System.out.println("Size of the HashMap: "+hm.size()); } }
Output:
{third=THIRD INSERTED, first=FIRST INSERTED, second=SECOND INSERTED} Value of second: SECOND INSERTED Is HashMap empty? false {first=FIRST INSERTED, second=SECOND INSERTED} Size of the HashMap: 2
Reference: Article
Leave a Reply