Java.util.TreeMap.put() and putAll() in Java
Last Updated :
18 Sep, 2023
The insertion in TreeMap are handled using the variants of the put functions. There are two variants of put() in Java.util.TreeMap, both are discussed in this article.
1. put() : It associates the specified value with the specified key in the map. If a key is already present, then updating it results in updation of that key.
Parameters:
key : The key with which the value is to be associated.
value : The value to be associated with the given key.
Return Value:
It returns the previously associated value with this key,
or null if there was no mapping for key.
Exception:
Not Available.
import java.io.*;
import java.util.*;
public class Put {
public static void main(String[] args)
{
TreeMap<String, Integer> tmp = new TreeMap<String, Integer>();
tmp.put("one", 1);
tmp.put("two", 3);
System.out.println("The initial TreeMap is : " + tmp);
int z = tmp.put("two", 2);
System.out.println("The previous value with two is : " + z);
System.out.println("Updated TreeMap is : " + tmp);
}
}
|
Output:
The initial TreeMap is : {one=1, two=3}
The previous value with two is : 3
Updated TreeMap is : {one=1, two=2}
2. putAll() : It copies all of the mappings from the specified map to the given map and overwrites in case of repeated key.
Parameters:
map : The mappings to be stored.
Return Value:
Not Available.
Exception:
NullPointerException : Thrown if specified map is null,
specified map contains a null key.
import java.io.*;
import java.util.*;
public class putAll {
public static void main(String[] args)
{
TreeMap<String, Integer> tmp1 = new TreeMap<String, Integer>();
TreeMap<String, Integer> tmp2 = new TreeMap<String, Integer>();
tmp1.put("two", 3);
tmp1.put("one", 1);
tmp2.put("three", 3);
tmp2.put("two", 2);
System.out.println("First treemap values are : " + tmp1);
tmp1.putAll(tmp2);
System.out.println("Values after modifying 1st treemap : " + tmp1);
}
}
|
Output:
First treemap values are : {one=1, two=3}
Values after modifying 1st treemap : {one=1, three=3, two=2}
Exception
Null Pointer Exception : Occurs if the map contains a null key or it is null in putAll().
import java.io.*;
import java.util.*;
public class putAll {
public static void main(String[] args)
{
TreeMap<String, Integer> tmp1 = new TreeMap<String, Integer>();
TreeMap<String, Integer> tmp2 = new TreeMap<String, Integer>();
tmp1.put("two", 3);
tmp1.put("one", 1);
tmp2.put("three", 3);
tmp2.put(null, 2);
System.out.println("First treemap values are : " + tmp1);
tmp1.putAll(tmp2);
System.out.println("Values after modifying 1st treemap : " + tmp1);
}
}
|
Runtime Error :
Exception in thread "main" java.lang.NullPointerException
at java.util.TreeMap.put(TreeMap.java:563)
at putAll.main(putAll.java:21)
Practical Application : This function has the to store values and update them if we need. This kind of functionality is mainly useful in the directories and dictionaries or records. A small code below explains the combining databases to get updated score in a game.
import java.io.*;
import java.util.*;
public class putAllAppli {
public static void main(String[] args)
{
TreeMap<String, Integer> Score1 = new TreeMap<String, Integer>();
TreeMap<String, Integer> Score2 = new TreeMap<String, Integer>();
Score1.put("Sachin", 40);
Score1.put("Rahul", 32);
Score1.put("Dhoni", 68);
System.out.println("The scores till Drinks break are : " + Score1);
Score2.put("Rahul", 67);
Score2.put("Dhoni", 102);
Score2.put("Raina", 10);
System.out.println("The scores at Lunch : " + Score2);
Score1.putAll(Score2);
System.out.println("Net scorecard is : " + Score1);
}
}
|
Output:
The scores till Drinks break are : {Dhoni=68, Rahul=32, Sachin=40}
The scores at Lunch : {Dhoni=102, Rahul=67, Raina=10}
Net scorecard is : {Dhoni=102, Rahul=67, Raina=10, Sachin=40}
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...