-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionInterface5.java
More file actions
103 lines (85 loc) · 2.12 KB
/
CollectionInterface5.java
File metadata and controls
103 lines (85 loc) · 2.12 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class Student{
int age;
String name;
public Student(int age, String name){
this.age = age;
this.name = name;
}
@Override
public String toString(){
return "Student [ age = "+age+" ,name = "+name+" ]";
}
}
public class CollectionInterface5{
public static void main(String args[]){
//Comparator
//Comparator is an interface!
//Comparator for custom integer sorting!
Comparator<Integer> com = new Comparator<Integer>(){
@Override
public int compare(Integer i, Integer j){ //Collections only works with wrapper classes! Not primitives.
if(i%10 > j%10){
return 1; //return 1 if numbers need to be swapped!
}
else{
return -1; //no need to swap!
}
}
};
//Comparator for custom string sorting based on string length
Comparator<String> com2 = new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
if(s1.length() > s2.length()){
return 1;
}
else{
return -1;
}
}
};
//Comparator for Student object
Comparator<Student> com3 = new Comparator<Student>(){
@Override
public int compare(Student s1,Student s2){
if(s1.age > s2.age){
return 1;
}
else{
return -1;
}
}
};
List<Integer> nums = new ArrayList<>();
nums.add(41);
nums.add(33);
nums.add(76);
nums.add(98);
List<String> strs = new ArrayList<>();
strs.add("Jayandhan");
strs.add("Rolex");
strs.add("Dhilli");
strs.add("Killer");
strs.add("Wolverine");
List<Student> students = new ArrayList<>();
students.add(new Student(22,"Killer"));
students.add(new Student(14,"JD"));
students.add(new Student(35,"Rolex"));
students.add(new Student(66,"Dhilli"));
students.add(new Student(10,"perry"));
//comparator is an interface
//using comparator, we can implement custom sorting techniques!
Collections.sort(nums, com);
Collections.sort(strs,com2);
Collections.sort(students,com3);
System.out.println(nums);
System.out.println(strs);
for(Student s: students){
System.out.println(s);
}
}
}