A SortedMap method put() used to associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced by the specified value. It returns the previous value associated with key, or null if there was no mapping for key.
key : It is a key with which the specified value is to be associated.
value : It is a value to be associated with the specified key.
return : It returns the previous value associated with key, or null if there was no mapping for key.
Exceptions:
1. | UnsupportedOperationException : if the put operation is not supported by this map |
2. | ClassCastException : if the class of the specified key or value prevents it from being stored in this map |
3. | NullPointerException : if the specified key or value is null and this map does not permit null keys or values |
4. | IllegalArgumentException : if some property of the specified key or value prevents it from being stored in this map |
1 package iogyan.example;
2
3 import java.util.SortedMap;
4 import java.util.TreeMap;
5
6 public class Main {
7
8 public static void main(String[] args) {
9
10
11 SortedMap<Integer, Integer> sortedMap = new TreeMap<>();
12 sortedMap.put(2, 120);
13 sortedMap.put(1, 210);
14 sortedMap.put(3, 0);
15 System.out.println("A sorted map1 elements : " + sortedMap);
16
17
18 int res = sortedMap.put(3, 300);
19 System.out.println("A resulting value : " + res);
20 System.out.println("A changed map mappings : " + sortedMap);
21 }
22 }
In the above example, a SortedMap of type integer key and value that sort keys in natural order. A put() method called by specifying an existing key and value. It replaces the value for the specified key 3 and returns old value mapped with the key.
1 A sorted map1 elements : {1=210, 2=120, 3=0}
2 A resulting value : 0
3 A changed map mappings : {1=210, 2=120, 3=300}
SortedMap method put() with non-existing mapping
SortedMap method put() with non-existing mapping
1 package iogyan.example;
2
3 import java.util.SortedMap;
4 import java.util.TreeMap;
5
6 public class Main {
7
8 public static void main(String[] args) {
9
10
11 SortedMap<Integer, Integer> sortedMap = new TreeMap<>();
12 sortedMap.put(2, 120);
13 sortedMap.put(1, 210);
14 System.out.println("A sorted map mappings : " + sortedMap);
15
16
17 int res = sortedMap.put(3, 300);
18 System.out.println("A resulting value : " + res);
19 System.out.println("A changed map mappings : " + sortedMap);
20 }
21 }
In the above example, a SortedMap of type integer key and value that sort keys in natural order. A put() method called by specifying an non existing key and value. It throws NullPointerException as specified key not present in this map.
1 A sorted map mappings : {1=210, 2=120}
2 Exception in thread "main" java.lang.NullPointerException:
3 Cannot invoke "java.lang.Integer.intValue()" because the return value of "java.util.SortedMap.put(Object, Object)" is null
4 at iogyan.example.Main.main(Main.java:17)
SortedMap method put() with String
SortedMap method put() with String
1 package iogyan.example;
2
3 import java.util.SortedMap;
4 import java.util.TreeMap;
5
6 public class StringMain {
7
8 public static void main(String[] args) {
9
10 SortedMap<String, String> sortedMap = new TreeMap<>();
11 sortedMap.put("K", "Welcome");
12 sortedMap.put("I", "IOGyan");
13 System.out.println("A sorted map elements : " + sortedMap);
14
15
16 String res = sortedMap.put("I", "IOGYAN");
17 System.out.println("A resulting value : " + res);
18 System.out.println("A changed map mappings : " + sortedMap);
19 }
20 }
In the above example, a SortedMap of type String key and value that sort keys in natural order. A put() method called by specifying an existing key and value in uppercase. It replaces the value for the specified key I and returns old value mapped with the key.
1 A sorted map elements : {I=IOGyan, K=Welcome}
2 A resulting value : IOGyan
3 A changed map mappings : {I=IOGYAN, K=Welcome}
SortedMap method put() with custom object
SortedMap method put() with custom object
1 package iogyan.example;
2
3 import iogyan.model.Employee;
4
5 import java.util.SortedMap;
6 import java.util.TreeMap;
7
8 public class EmployeeMain {
9
10 public static void main(String[] args) {
11
12 Employee emp1 = new Employee(1, "Emp1","Tech", 1000);
13 Employee emp2 = new Employee(2, "Emp2","Admin", 5000);
14 Employee emp3 = new Employee(3, "Emp3","Admin", 12000);
15
16 SortedMap<Employee, Integer> sortedMap = new TreeMap<>();
17 sortedMap.put(emp1, 100);
18 sortedMap.put(emp2, 200);
19 sortedMap.put(emp3, 300);
20 System.out.println("A sorted map mappings : " + sortedMap);
21
22
23 int res = sortedMap.put(emp2, 1000);
24 System.out.println("A resulting value : " + res);
25 System.out.println("A changed map mappings : " + sortedMap);
26 }
27 }
In the above example, a SortedMap of type Employee key and integer value that sort keys in natural order based on Employee ID using compareTo() method of Employee class as Employee class implement Comparable interface. A put() method called by specifying an existing key and value in uppercase. It replaces the value for the specified key I and returns old value mapped with the key.
1 A sorted map mappings : {Emp1 Tech 1000.0=100, Emp2 Admin 5000.0=200, Emp3 Admin 12000.0=300}
2 A resulting value : 200
3 A changed map mappings : {Emp1 Tech 1000.0=100, Emp2 Admin 5000.0=1000, Emp3 Admin 12000.0=300}
Employee class
1 package iogyan.model;
2
3 import java.util.Comparator;
4
5 public class Employee implements Comparable<Employee> {
6 private final int id;
7 private final String name;
8 private final String dept;
9 private final double salary;
10
11 public Employee(int id, String name, String dept, double salary) {
12 this.id = id;
13 this.name = name;
14 this.dept = dept;
15 this.salary = salary;
16 }
17
18 public int getId() {
19 return this.id;
20 }
21
22 public String getName() {
23 return this.name;
24 }
25
26 public String getDept() {
27 return this.dept;
28 }
29
30 public double getSalary() {
31 return this.salary;
32 }
33
34 @Override
35 public int hashCode() {
36 return super.hashCode();
37 }
38
39 @Override
40 public boolean equals(Object obj) {
41 return this.id == ((Employee)obj).id;
42 }
43
44 @Override
45 public String toString() {
46 return this.getName() + " " + this.getDept() + " " + this.getSalary();
47 }
48
49 @Override
50 public int compareTo(Employee o) {
51 return this.getId() - o.getId();
52 }
53 }
Related options for your search