putAll(Map map) method

putAll(Map map): This Java Map putAll method is used to copy all elements of specified map in current TreeMap. It copies all the mapping(Key and value pairs) of specified Map to current TreeMap

putAll(Map map)

Where, map is specified Map which you want to copy.
return Void, it doesn’t return anything.

import java.util.HashMap;
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, "SITE");
		
		TreeMap<Integer, String> listOfNames2 = new TreeMap<Integer, String>(listOfNames);
		
		for(Integer key : listOfNames2.keySet())
			System.out.println("Names from list = "+listOfNames2.get(key));
	}
}

Output: Names from list = JAVA
Names from list = GOAL
Names from list = SITE

Leave a Comment