-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup-by-using-java.java
52 lines (50 loc) · 1.18 KB
/
group-by-using-java.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.*;
public class Runner {
public static void main(String args[]) {
int x=10;
int y=25;
int z=x+y;
LinkedList <Student> q = new LinkedList<Student>();
q.add(new Student("a",10));
q.add(new Student("b",60));
q.add(new Student("c",50));
q.add(new Student("d",10));
print(q);
Collections.sort(q, new ScoreComparator().thenComparing(new NameComparator() ));
System.out.println("after sort");
print(q);
}
public static void print(LinkedList <Student>q)
{
Iterator <Student>itr = q.iterator();
while(itr.hasNext())
{
Student temp = itr.next();
System.out.println(temp.score+" "+temp.name);
}
}
}
class Student
{
String name;
Integer score;
Student(String name, int score)
{
this.name = name;
this.score = score;
}
}
class NameComparator implements Comparator<Student>
{
public int compare(Student s1, Student s2)
{
return s1.name.compareTo(s2.name);
}
}
class ScoreComparator implements Comparator<Student>
{
public int compare(Student s1, Student s2)
{
return s2.score.compareTo(s1.score);
}
}