Skip to content

Commit 6fe6665

Browse files
author
JavaProgramTo.com
committed
Java TreeMap Comparator
1 parent f79b120 commit 6fe6665

File tree

6 files changed

+193
-0
lines changed

6 files changed

+193
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.javaprogramto.collections.treemap.comparator;
2+
3+
import java.util.Comparator;
4+
import java.util.Map;
5+
import java.util.SortedSet;
6+
import java.util.TreeMap;
7+
import java.util.TreeSet;
8+
9+
public class TreeMapComparatorByValueExample {
10+
11+
public static void main(String[] args) {
12+
13+
SortedSet<Map.Entry<Customer, Integer>> sortedset = new TreeSet<>(
14+
new Comparator<Map.Entry<Customer, Integer>>() {
15+
@Override
16+
public int compare(Map.Entry<Customer, Integer> e1, Map.Entry<Customer, Integer> e2) {
17+
return e1.getValue().compareTo(e2.getValue());
18+
}
19+
});
20+
21+
Map<Customer, Integer> customerAgeMap = new TreeMap<>(Comparator.comparing(Customer::getId));
22+
23+
customerAgeMap.put(new Customer(123, "D"), 30);
24+
customerAgeMap.put(new Customer(102, "B"), 70);
25+
customerAgeMap.put(new Customer(135, "A"), 40);
26+
customerAgeMap.put(new Customer(130, "C"), 50);
27+
28+
sortedset.addAll(customerAgeMap.entrySet());
29+
30+
System.out.println("java 8 - treemap sort by value - " + sortedset);
31+
32+
}
33+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.javaprogramto.collections.treemap.comparator;
2+
3+
import java.util.Map;
4+
import java.util.TreeMap;
5+
6+
public class TreeMapComparatorCustomObjectsExample {
7+
8+
public static void main(String[] args) {
9+
10+
Map<Customer, Integer> customerAgeMap = new TreeMap<>();
11+
12+
customerAgeMap.put(new Customer(123, "D"), 30);
13+
customerAgeMap.put(new Customer(102, "B"), 70);
14+
customerAgeMap.put(new Customer(135, "A"), 40);
15+
customerAgeMap.put(new Customer(130, "C"), 50);
16+
17+
System.out.println("treemap - " + customerAgeMap);
18+
19+
20+
21+
}
22+
}
23+
24+
class Customer {
25+
private int id;
26+
private String name;
27+
28+
public Customer(int id, String name) {
29+
super();
30+
this.id = id;
31+
this.name = name;
32+
}
33+
34+
public int getId() {
35+
return id;
36+
}
37+
38+
public void setId(int id) {
39+
this.id = id;
40+
}
41+
42+
public String getName() {
43+
return name;
44+
}
45+
46+
public void setName(String name) {
47+
this.name = name;
48+
}
49+
50+
@Override
51+
public String toString() {
52+
return "Customer [id=" + id + ", name=" + name + "]";
53+
}
54+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.javaprogramto.collections.treemap.comparator;
2+
3+
import java.util.Comparator;
4+
import java.util.Map;
5+
import java.util.TreeMap;
6+
7+
public class TreeMapComparatorCustomObjectsExample2 {
8+
9+
public static void main(String[] args) {
10+
11+
Map<Customer, Integer> customerAgeMap = new TreeMap<>(new Comparator<Customer>() {
12+
@Override
13+
public int compare(Customer o1, Customer o2) {
14+
Integer id1 = o1.getId();
15+
Integer id2 = o2.getId();
16+
return id1.compareTo(id2);
17+
}
18+
});
19+
20+
customerAgeMap.put(new Customer(123, "D"), 30);
21+
customerAgeMap.put(new Customer(102, "B"), 70);
22+
customerAgeMap.put(new Customer(135, "A"), 40);
23+
customerAgeMap.put(new Customer(130, "C"), 50);
24+
25+
System.out.println("treemap with comparator - " + customerAgeMap);
26+
27+
}
28+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.javaprogramto.collections.treemap.comparator;
2+
3+
import java.util.Comparator;
4+
import java.util.Map;
5+
import java.util.TreeMap;
6+
7+
public class TreeMapComparatorCustomObjectsExample3 {
8+
9+
public static void main(String[] args) {
10+
11+
Map<Customer, Integer> customerAgeMap = new TreeMap<>(new Comparator<Customer>() {
12+
@Override
13+
public int compare(Customer o1, Customer o2) {
14+
String name1 = o1.getName();
15+
String name2 = o2.getName();
16+
17+
return name1.compareTo(name2);
18+
}
19+
});
20+
21+
customerAgeMap.put(new Customer(123, "D"), 30);
22+
customerAgeMap.put(new Customer(102, "B"), 70);
23+
customerAgeMap.put(new Customer(135, "A"), 40);
24+
customerAgeMap.put(new Customer(130, "C"), 50);
25+
26+
System.out.println("treemap - " + customerAgeMap);
27+
28+
}
29+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.javaprogramto.collections.treemap.comparator;
2+
3+
import java.util.Comparator;
4+
import java.util.Map;
5+
import java.util.TreeMap;
6+
7+
public class TreeMapComparatorCustomObjectsExample4 {
8+
9+
public static void main(String[] args) {
10+
11+
Map<Customer, Integer> customerAgeMap = new TreeMap<>(Comparator.comparing(Customer::getId));
12+
13+
customerAgeMap.put(new Customer(123, "D"), 30);
14+
customerAgeMap.put(new Customer(102, "B"), 70);
15+
customerAgeMap.put(new Customer(135, "A"), 40);
16+
customerAgeMap.put(new Customer(130, "C"), 50);
17+
18+
System.out.println("java 8 - treemap sort by id - " + customerAgeMap);
19+
20+
Map<Customer, Integer> customerAgeMap2 = new TreeMap<>(Comparator.comparing(Customer::getName));
21+
22+
customerAgeMap2.put(new Customer(123, "D"), 30);
23+
customerAgeMap2.put(new Customer(102, "B"), 70);
24+
customerAgeMap2.put(new Customer(135, "A"), 40);
25+
customerAgeMap2.put(new Customer(130, "C"), 50);
26+
27+
System.out.println("java 8 - treemap sort by name - " + customerAgeMap2);
28+
29+
}
30+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.javaprogramto.collections.treemap.comparator;
2+
3+
import java.util.Map;
4+
import java.util.TreeMap;
5+
6+
public class TreeMapComparatorExample {
7+
8+
public static void main(String[] args) {
9+
10+
Map<String, Integer> designationSalaryInUSD = new TreeMap<>();
11+
12+
designationSalaryInUSD.put("Software Engineer", 150_000);
13+
designationSalaryInUSD.put("Senior Software Engineer", 210_000);
14+
designationSalaryInUSD.put("Manger", 300_000);
15+
designationSalaryInUSD.put("Lead Engineer", 250_000);
16+
17+
System.out.println("treemap - " + designationSalaryInUSD);
18+
}
19+
}

0 commit comments

Comments
 (0)