File tree Expand file tree Collapse file tree 4 files changed +115
-0
lines changed Expand file tree Collapse file tree 4 files changed +115
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .util .Comparator ;
2+ import java .util .SortedMap ;
3+ import java .util .TreeMap ;
4+ class SortedMap10 {
5+ int a ;
6+ int b ;
7+ public SortedMap10 (int a , int b ) {
8+ this .a = a ;
9+ this .b = b ;
10+ }
11+ public String toString () {
12+ return "a:" + a + " b:" + b ;
13+ }
14+
15+
16+
17+
18+ public static void main (String [] args ) throws Exception {
19+ SortedMap <SortedMap10 , Integer > map = new TreeMap <>(Comparator .comparingInt (o -> o .a ));
20+ map .put (new SortedMap10 (1 , 2 ), 1 );
21+ map .put (new SortedMap10 (2 , 3 ), 2 );
22+ map .put (new SortedMap10 (3 , 4 ), 3 );
23+ map .put (new SortedMap10 (4 , 5 ), 4 );
24+ map .put (new SortedMap10 (5 , 6 ), 5 );
25+ map .put (new SortedMap10 (6 , 7 ), 6 );
26+ System .out .println ("Map:" + map );
27+
28+ }
29+ }
30+
31+
Original file line number Diff line number Diff line change 1+ import java .util .Comparator ;
2+ import java .util .SortedMap ;
3+ import java .util .TreeMap ;
4+ class SortedMap11 {
5+ int a ;
6+ int b ;
7+
8+ public SortedMap11 (int a , int b ) {
9+ this .a = a ;
10+ this .b = b ;
11+ }
12+
13+ public String toString () {
14+ return "a:" + a + " b:" + b ;
15+ }
16+
17+
18+ }
19+
20+ class srtMp implements Comparator <SortedMap11 > {
21+
22+ @ Override
23+ public int compare (SortedMap11 o1 , SortedMap11 o2 ) {
24+ return o1 .a - o2 .a ;
25+ }
26+
27+ }
28+
29+ class Main {
30+ public static void main (String [] args ) throws Exception {
31+ SortedMap <SortedMap11 , Integer > map = new TreeMap <>(new srtMp ());
32+ map .put (new SortedMap11 (1 , 2 ), 1 );
33+ map .put (new SortedMap11 (2 , 3 ), 2 );
34+ map .put (new SortedMap11 (3 , 4 ), 3 );
35+ map .put (new SortedMap11 (4 , 5 ), 4 );
36+ map .put (new SortedMap11 (5 , 6 ), 5 );
37+ map .put (new SortedMap11 (6 , 7 ), 6 );
38+ System .out .println ("Map:" + map );
39+
40+ }
41+ }
Original file line number Diff line number Diff line change 1+ import java .util .SortedMap ;
2+ import java .util .TreeMap ;
3+ public class SortedMap8 {
4+ public static void main (String [] args ) throws Exception {
5+ // headMap
6+ SortedMap <String , Integer > map = new TreeMap <>((a ,b ) -> b .compareTo (a ));
7+ map .put ("a" , 1 );
8+ map .put ("b" , 2 );
9+ map .put ("c" , 3 );
10+ map .put ("d" , 4 );
11+ map .put ("e" , 5 );
12+
13+ System .out .println ("Map:" + map );
14+
15+ }
16+
17+ }
Original file line number Diff line number Diff line change 1+ import java .util .Comparator ;
2+ import java .util .SortedMap ;
3+ import java .util .TreeMap ;
4+ public class SortedMap9 {
5+
6+ public static void main (String [] args ) throws Exception {
7+ SortedMap <String , Integer > map = new TreeMap <>(Comparator .reverseOrder ());
8+ map .put ("a" , 1 );
9+ map .put ("b" , 2 );
10+ map .put ("c" , 3 );
11+ map .put ("d" , 4 );
12+ map .put ("e" , 5 );
13+ map .put ("f" , 6 );
14+ System .out .println ("Map:" + map );
15+
16+ SortedMap <Integer , Integer > map2 = new TreeMap <>(Comparator .comparingInt (o -> o ));
17+ map2 .put (1 , 1 );
18+ map2 .put (2 , 2 );
19+ map2 .put (3 , 3 );
20+ map2 .put (4 , 4 );
21+
22+ System .out .println ("Map2:" + map2 );
23+
24+ }
25+
26+ }
You can’t perform that action at this time.
0 commit comments