entrySet() method

entrySet(): This entrySet method in Java is used to fetch the Set view of all keys and Values. It returns all the key and values in form of Set. This Set worked on backed of TreeMap, if any change makes in TreeMap also reflects in this Set and vice versa. It returns type is Set.

Set<Map.Entry<K,V>> entrySet(); 

Where, K represents the type Keys in TreeMap .
V represents the type Values in TreeMap.

import java.util.TreeMap;

public class ExampleOfTreeMap 
{
	public static void main(String[] args) 
	{
		TreeMap<Integer,String> listOfNames = new TreeMap<Integer, String>();
		
		listOfNames.put(1, "JAVA");
		listOfNames.put(2, "GOAL");
		listOfNames.put(3, "RAVI");
		
		System.out.println("Keys and values set of TreeMap = "+listOfNames.entrySet());
		
	}
}

Output: Keys and values set of TreeMap = [1=JAVA, 2=GOAL, 3=RAVI]

Leave a Comment