Java

Understanding Map Collection in Java

This article on Understanding Map Collection in Java describes the java collection interface Map. Also, several examples are also included here.

Map is one of the collection interfaces available in java.util package. In order to store key-value pairs, the Map interface is used. Also, the keys that the Map contains are unique. Some of the methods available in the Map interface are: put(), get(), remove(), size(), values(), putAll(), and clear().

The following list provides some examples of using Map interface.

Examples for Understanding Map Collection in Java

The HashMap class implements the Map interface. The following program shows a HashMap object that represents a collection of an Integer type key and a Staff type value pairs. While the Staff is a user-defined class. Here we add six key-value pairs and display them using an iterator using the put() method. The following program also shows how to update the value for an existing key. For this purpose, first we obtain the value for a specific key using the get() method. After that, we can again call the put() method with the same key and the updated value. The size() method returns the total number of key-value pairs in the HashMap. In order to remove a key-value pair, we can use the remove() method with a specific key value.

package com.myhashsets;
import java.util.*;
public class MapsDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		HashMap<Integer, Staff> hsh=new HashMap<Integer, Staff>();
		hsh.put(11, new Staff(101, "Jim"));
		hsh.put(12, new Staff(102, "Jill"));
		hsh.put(13, new Staff(103, "Joe"));
		hsh.put(14, new Staff(104, "Joe"));
		hsh.put(15, new Staff(102, "Jill"));
		hsh.put(16, new Staff(101, "Jim"));

		System.out.println("HashMap Values: ");
		Set entrySet=hsh.entrySet();
		Iterator it=entrySet.iterator();
		while(it.hasNext())
		{
			Map.Entry<Integer, Staff> me=(Map.Entry<Integer, Staff>)(it.next());
			System.out.println("Key: "+me.getKey()+", Value: "+me.getValue());
		}
		
		//updating value for a key
		String s=hsh.get(13).getSname();
		int sid=hsh.get(13).getStaff_id();
		s=s.toUpperCase();
		hsh.put(13, new Staff(sid, s));
		System.out.println("HashMap Values After Updating: ");	
		it=entrySet.iterator();
		while(it.hasNext())
		{
			Map.Entry<Integer, Staff> me=(Map.Entry<Integer, Staff>)(it.next());
			System.out.println("Key: "+me.getKey()+", Value: "+me.getValue());
		}
		
		System.out.println("Size of HashMap: "+hsh.size());
		
		//Removing an element using the key
		hsh.remove(11);
		//Hashmap after removing
		it=entrySet.iterator();
		while(it.hasNext())
		{
			Map.Entry<Integer, Staff> me=(Map.Entry<Integer, Staff>)(it.next());
			System.out.println("Key: "+me.getKey()+", Value: "+me.getValue());
		}
		System.out.println("Size of HashMap after removing: "+hsh.size());
	}

}
class Staff
{
	int staff_id;
	String sname;
	public Staff(int staff_id, String sname) {
		super();
		this.staff_id = staff_id;
		this.sname = sname;
	}
	public int getStaff_id() {
		return staff_id;
	}
	public void setStaff_id(int staff_id) {
		this.staff_id = staff_id;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	@Override
	public String toString() {
		return "Staff Details: [staff_id=" + staff_id + ", sname=" + sname + "]";
	}	
}

Output

Understanding Map Collection in Java - Implementing HashMap
Understanding Map Collection in Java – Implementing HashMap

Hashtable Class

The Hashtable class also implements the Map interface. Also, it extends from the Dictionary class. Its elements are all unique. So, it can’t take null values. Furthermore, this class is synchronized. The following link contains an example of the Hashtable class.

https://www.programmingempire.com/an-example-of-hashtable-class-in-java/

TreeMap Class

The TreeMap class maintains a sorted collection of key-value pairs. Likewise, the key can’tbe null. However, null values are allowed. The collection arranges its elements in ascending order of keys. The following link contains an example of TreeMap class.

https://www.programmingempire.com/an-example-of-treemap-in-java/

Properties Class

Like other Map classes, the Properties class also contains key-value pairs. However, both keys and values are of the String type. The following link contains an example of the Properties class.

https://www.programmingempire.com/an-example-of-the-properties-class-in-java/


Further Reading

Understanding Enterprise Java Beans

Java Practice Exercise

Princites

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *