Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a1e3fd5

Browse files
Challenge 51.
Comparator, Collections
1 parent 29900f9 commit a1e3fd5

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package challenge51_60;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.Comparator;
6+
7+
/**
8+
* Comparator.comparing method
9+
* Collections.sort(list, comparator)
10+
*/
11+
public class Challenge_51 {
12+
13+
public static void main( String[] args ) {
14+
ArrayList<Jedi> arrayList = new ArrayList<>();
15+
arrayList.add(new Jedi("Anakin",10));
16+
arrayList.add(new Jedi("Luke",5));
17+
arrayList.add(new Jedi("Luke",6));
18+
arrayList.add(new Jedi("Obi Wan",7));
19+
20+
Comparator<Jedi> comparator = Comparator.comparing(Jedi::getName)
21+
.thenComparing(( o1, o2 ) -> o2.age.compareTo(o1.getAge()));
22+
Collections.sort(arrayList, comparator);
23+
arrayList.forEach(jedi -> System.out.println(jedi.name+":"+jedi.age));
24+
}
25+
26+
27+
28+
static class Jedi{
29+
String name;
30+
Integer age;
31+
32+
public Jedi( String name, Integer age ) {
33+
this.name = name;
34+
this.age = age;
35+
}
36+
37+
public String getName() {
38+
return name;
39+
}
40+
41+
public Integer getAge() {
42+
return age;
43+
}
44+
}
45+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /