-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStududent.java
More file actions
47 lines (38 loc) · 1.08 KB
/
Stududent.java
File metadata and controls
47 lines (38 loc) · 1.08 KB
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
package com.comparable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Stududent implements Comparable<Stududent>{
private int id;
private String name;
private String address;
public Stududent(int id, String name, String address) {
super();
this.id = id;
this.name = name;
this.address = address;
}
@Override
public String toString() {
return "Stududent [id=" + id + ", name=" + name + ", address=" + address + "]";
}
@Override
public int compareTo(Stududent o) {
if(this.id==o.id)
return 0;
else if(this.id>o.id) // > for ascending , < for descending order
return 1;
else
return -1;
}
public static void main(String[] args) {
List<Stududent> list = new ArrayList<>();
list.add(new Stududent(4, "dawn", "London1"));
list.add(new Stududent(1, "adawn", "London4"));
list.add(new Stududent(3, "cdawn", "London3"));
list.add(new Stududent(5, "bdawn", "London2"));
list.add(new Stududent(2, "dawn", "London5"));
Collections.sort(list);
list.stream().forEach((id)->System.out.println(id.id));
}
}