-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMain.java
More file actions
114 lines (84 loc) · 2.85 KB
/
Main.java
File metadata and controls
114 lines (84 loc) · 2.85 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
104
105
106
107
108
109
110
111
112
113
114
import java.util.*;
public class Main {
public static void main(String[] args) {
Collection<User> persons = new TreeSet<>();
User ahmed = new User("Ahmed", 10);
persons.add(ahmed);
persons.add(new User("Ali", 8));
persons.add(new User("Mohammed", 5));
persons.add(new User("Ali", 8));
persons.add(new User("Osama", 5));
persons.add(ahmed);
for(User user : persons) {
System.out.println(user);
}
// Collections.sort(persons, new UserAgeDescComparator());
// System.out.println(":::::::::::: AFTER SORT");
//
// for(User user : persons) {
// System.out.println(user);
// }
LinkedList<String> list = new LinkedList<>();
list.offer("A");
list.offer("B");
list.offer("C");
list.offer("D");
list.offer("E");
System.out.println(list.pollFirst());
System.out.println(list.pollLast());
System.out.println("===========");
Map<String, User> phoneDirectory = new TreeMap<>();
phoneDirectory.put("010", new User("Ahmed", 10));
phoneDirectory.put("011", new User("Mohammed", 10));
phoneDirectory.put("012", new User("Ali", 10));
phoneDirectory.put("013", new User("Osama", 10));
System.out.println(phoneDirectory.get("011"));
for(String key : phoneDirectory.keySet()) {
System.out.println(key + " : " + phoneDirectory.get(key));
}
for(Map.Entry entry : phoneDirectory.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
static class UserAgeAscComparator implements Comparator<User> {
@Override
public int compare(User o1, User o2) {
return o1.age - o2.age;
}
}
static class UserAgeDescComparator implements Comparator<User> {
@Override
public int compare(User o1, User o2) {
return o2.age - o1.age;
}
}
static class User implements Comparable<User> {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public boolean equals(Object obj) {
User otherUser = (User) obj;
System.out.println("EQUALS " + this + " === " + otherUser);
return otherUser.getName().equals(this.name);
}
@Override
public String toString() {
return "User name: " + name + " and age: " + age;
}
@Override
public int compareTo(User other) {
return this.age - other.age;
}
}
}