HashMap in Java

HOME

A HashMap store items in “key/value” pairs, and you can access them by an index of another type (e.g. a String).

One object is used as a key (index) to another object (value). It can store different types: String keys and Integer values, or the same type, like: String keys and String values. We need to import java.util.HashMap or its super class in order to use the HashMap class and methods.

It is not an ordered collection which means it does not return the keys and values in the same order in which they have been inserted into the HashMap.

Create a HashMap object as shown below from:-

import java.util.HashMap;

Syntax of HashMap

HashMap<String, String> employeeDetail = new HashMap<String, String>();

HashMap<String, String> employeeDetail = new HashMap<>();

HashMap<String, Integer> employeeDetail = new HashMap<String, Integer>();

Add Items

Below is an example where we are adding items to HashMap by using put() method.

public class HashMapDemo {

	public static void main(String[] args) {

		/* This is how to declare HashMap of key and Value as String  */
		HashMap<String, String> employeeDetail = new HashMap<String, String>();

		// Add keys and values (Name, Designation)
		employeeDetail.put("Tim", "DBA");
		employeeDetail.put("Dong", "SDET");
		employeeDetail.put("Nisha", "BA");
		employeeDetail.put("Ulana", "Dev");

		System.out.println("Detail of Employees");
		System.out.println(employeeDetail);
	}
}

Output
Detail of Employees
{Dong=SDET, Tim=DBA, Nisha=BA, Ulana=Dev}

Below is an example where we are adding items to HashMap by using put() method. Here, key is String and Value as Integer.

public class HashMapDemo {

	public static void main(String[] args) {

		// Combination of String(key) and Integer(value)
		HashMap<String, Integer> employeeAge = new HashMap<String, Integer>();

		// Add keys and values (Name, Age)
		employeeAge.put("Tim", 25);
		employeeAge.put("Dong", 26);
		employeeAge.put("Nisha", 29);
		employeeAge.put("Ulana", 34);

		System.out.println("Age of Employees");
		System.out.println(employeeAge);
    }
}

Output
Age of Employees
{Dong=26, Tim=25, Nisha=29, Ulana=34}

Access an Item

To access a value in the HashMap, use the get() method and refer to its key:-

public class HashMapDemo {

	public static void main(String[] args) {

		// Combination of String(key) and Integer(value)
		HashMap<String, Integer> employeeAge = new HashMap<String, Integer>();

		// Add keys and values (Name, Age)
		employeeAge.put("Tim", 25);
		employeeAge.put("Dong", 26);
		employeeAge.put("Nisha", 29);
		employeeAge.put("Ulana", 34);

		System.out.println("Age of Employees");
		System.out.println(employeeAge);

		// Access a value
		System.out.println("Access Value of Key Nisha");
		System.out.println(employeeAge.get("Nisha"));
   }
}

Output
Access Value of Key Nisha
29

Remove an Item

To remove an item, use the remove() method and refer to the key:

public class HashMapDemo {

	public static void main(String[] args) {

		// Combination of String(key) and Integer(value)
		HashMap<String, Integer> employeeAge = new HashMap<String, Integer>();

		// Add keys and values (Name, Age)
		employeeAge.put("Tim", 25);
		employeeAge.put("Dong", 26);
		employeeAge.put("Nisha", 29);
		employeeAge.put("Ulana", 34);

		System.out.println("Age of Employees");
		System.out.println(employeeAge);

		// To remove an item, use the remove() method and refer to the key
		employeeAge.remove("Ulana");
		System.out.println("List of Employee after removing Ulana :" + employeeAge);
   }
}

Output
Age of Employees
{Dong=26, Tim=25, Nisha=29, Ulana=34}
List of Employee after removing Ulana :{Dong=26, Tim=25, Nisha=29}

To remove all items, use the clear() method:-

public class HashMapDemo {

	public static void main(String[] args) {

		// Combination of String(key) and Integer(value)
		HashMap<String, Integer> employeeAge = new HashMap<String, Integer>();

		// Add keys and values (Name, Age)
		employeeAge.put("Tim", 25);
		employeeAge.put("Dong", 26);
		employeeAge.put("Nisha", 29);
		employeeAge.put("Ulana", 34);

		System.out.println("Age of Employees");
		System.out.println(employeeAge);

		// To remove all items, use the clear() method:
		employeeAge.clear();
		System.out.println("List of Employes after clear:" + employeeAge);
   }
}

Output
Age of Employees
{Dong=26, Tim=25, Nisha=29, Ulana=34}
List of Employes after clear:{}

Checking whether HashMap is empty or not

We are using isEmpty() method of HashMap class to perform this check.

public class HashMapDemo1 {

	public static void main(String[] args) {

		/* This is how to declare HashMap */
		HashMap<String, String> employeeDetail = new HashMap<String, String>();

		// Add keys and values (Name, Designation)
		employeeDetail.put("Tim", "DBA");
		employeeDetail.put("Dong", "SDET");
		employeeDetail.put("Nisha", "BA");
		employeeDetail.put("Ulana", "Dev");

		System.out.println("Detail of Employees");
		System.out.println(employeeDetail);

		// Checking whether HashMap is empty or not
		System.out.println("Is HashMap Empty? " + employeeDetail.isEmpty());

		// Delete all items from HasMap
		employeeDetail.clear();

		System.out.println("Is HashMap Empty now? " + employeeDetail.isEmpty());
    }
}

Output
Detail of Employees
{Dong=SDET, Tim=DBA, Nisha=BA, Ulana=Dev}
Is HashMap Empty? false
Is HashMap Empty now? true

Check if a particular key exists in HashMap

We will be using containsKey() method of HashMap class to perform this check.  This method returns a boolean value.

public class HashMapDemo1 {

	public static void main(String[] args) {

		/* This is how to declare HashMap */
		HashMap<String, String> employeeDetail = new HashMap<String, String>();

		// Add keys and values (Name, Designation)
		employeeDetail.put("Tim", "DBA");
		employeeDetail.put("Dong", "SDET");
		employeeDetail.put("Nisha", "BA");
		employeeDetail.put("Ulana", "Dev");

		System.out.println("Detail of Employees");
		System.out.println(employeeDetail);

		// Check if a particular key exists in HashMap
		boolean flag = employeeDetail.containsKey("Nisha");
		System.out.println("Key Nisha exists in HashMap? : " + flag);

		boolean flag1 = employeeDetail.containsKey("Shawn");
		System.out.println("Key Shawn exists in HashMap? : " + flag1);
    }
}

Output
Detail of Employees
{Dong=SDET, Tim=DBA, Nisha=BA, Ulana=Dev}
Key Nisha exists in HashMap? : true
Key Shawn exists in HashMap? : false

Check if a particular value exists in HashMap

We will be using containsValue() method of HashMap class to perform this check.

public class HashMapDemo1 {

	public static void main(String[] args) {

		/* This is how to declare HashMap */
		HashMap<String, String> employeeDetail = new HashMap<String, String>();

		// Add keys and values (Name, Designation)
		employeeDetail.put("Tim", "DBA");
		employeeDetail.put("Dong", "SDET");
		employeeDetail.put("Nisha", "BA");
		employeeDetail.put("Ulana", "Dev");

		System.out.println("Detail of Employees");
		System.out.println(employeeDetail);

		// Check if a particular value exists in HashMap
		boolean valueExists = employeeDetail.containsValue("Dev");
		System.out.println("String Dev exists in HashMap? : " + valueExists);

		boolean valueNotExists = employeeDetail.containsValue("Test");
		System.out.println("String Test exists in HashMap? : " + valueNotExists)
    }
}

Output
Detail of Employees
{Dong=SDET, Tim=DBA, Nisha=BA, Ulana=Dev}
String Dev exists in HashMap? : true
String Test exists in HashMap? : false

We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s