-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashSetTest.java
More file actions
49 lines (46 loc) · 1.13 KB
/
HashSetTest.java
File metadata and controls
49 lines (46 loc) · 1.13 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
package code;
import java.util.HashSet;
import java.util.Iterator;
class R{
int count;
public R(int count){
this.count = count;
}
public String toString(){
return "R["+count+"]";
}
public boolean equals(Object obj){
if(this == obj){
return true;
}
else{
if(obj!=null&&obj.getClass()==R.class){
R r = (R)obj;
return r==this;
}
return false;
}
}
public int hashCode(){
return count;
}
}
public class HashSetTest{
public static void main(String[] args) {
HashSet<R> hs = new HashSet<>();
hs.add(new R(1));
hs.add(new R(2));
hs.add(new R(3));
hs.add(new R(4));
System.out.println(hs);
Iterator<R> it = hs.iterator();
R first = (R)it.next();
first.count = 2;
System.out.println(hs);
hs.remove(new R(2));
System.out.println(hs);
System.out.println(hs.contains(new R(1)));
System.out.println(hs.contains(new R(2)));
System.out.println(hs.contains(new R(3)));
}
}