-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSetDemo.java
More file actions
156 lines (107 loc) · 3.24 KB
/
SetDemo.java
File metadata and controls
156 lines (107 loc) · 3.24 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package SetDemo;
import java.util.*;
public class SetDemo {
public static void main(String[] args) {
Set<String> hashSet = new HashSet<>();
hashSet.add("a");
hashSet.add("a");
hashSet.remove("a");
System.out.println(hashSet.size());
System.out.println(hashSet);
hashSet.add("a");
hashSet.add("h");
hashSet.add("b");
hashSet.add("a");
hashSet.add("c");
hashSet.add("f");
hashSet.add("w");
hashSet.add("j");
System.out.println(hashSet);
Set<String> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add("y");
linkedHashSet.add("e");
linkedHashSet.add("f");
linkedHashSet.add("g");
linkedHashSet.add("h");
linkedHashSet.add("k");
linkedHashSet.add("a");
System.out.println(linkedHashSet);
Set<Integer> treeSet = new TreeSet<>();
treeSet.add(5);
treeSet.add(4);
treeSet.add(3);
treeSet.add(2);
treeSet.add(1);
System.out.println(treeSet);
Set<String> stringTreeSet = new TreeSet<>();
stringTreeSet.add("f");
stringTreeSet.add("e");
stringTreeSet.add("a");
stringTreeSet.add("b");
stringTreeSet.add("c");
System.out.println(stringTreeSet);
Set<Student> customTreeSet = new TreeSet(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o2.getScore() - o1.getScore();
}
});
Student a1 = new Student(33, "a", 18);
Student a2 = new Student(33, "b", 23);
System.out.println("------hashcode---------");
customTreeSet.add(a1);
customTreeSet.add(a2);
System.out.println(customTreeSet);
Iterator<Student> itr = customTreeSet.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
System.out.println("-----------------------");
customTreeSet.add(new Student(100));
customTreeSet.add(new Student(90));
customTreeSet.add(new Student(60));
customTreeSet.add(new Student(99));
System.out.println(customTreeSet);
treeSet.clear();
System.out.println(treeSet.size());
}
}
class Student {
private int score;
private String name;
private int age;
public Student(int score, String name, int age) {
this.score = score;
this.name = name;
this.age = age;
}
public Student(int score) {
this.score = score;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"score=" + score +
'}';
}
public Student() {
super();
}
@Override
public int hashCode() {
System.out.println("...hashcode()");
return 1;
}
@Override
public boolean equals(Object obj) {
System.out.println(this.name + "...equals()");
Student student = (Student) obj;
return this.name.equals(student.name) && this.age == student.age;
}
}