📄 set2.java
字号:
//: c09:Set2.java
// From 'Thinking in Java, 2nd ed.' by Bruce Eckel
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
// Putting your own type in a Set.
import java.util.*;
class MyType implements Comparable {
private int i;
public MyType(int n) {
i = n; }
public boolean equals(Object o) {
System.out.println("equals() ");
return
(o instanceof MyType)
&& (i == ((MyType)o).i);
}
public int hashCode() {
System.out.println("hashCode()"+" ");
return i; }
public String toString() { return i + " "; }
public int compareTo(Object o) {
System.out.println("compareTo()"+" ");
int i2 = ((MyType)o).i;
return (i2 < i ? -1 : (i2 == i ? 0 : 1));
}
}
public class Set2 {
public static Set fill(Set a, int size) {
for(int i = 0; i < size; i++){
a.add(new MyType(i));
System.out.println(new MyType(i)+"\t");
}
return a;
}
public static void test(Set a) {
fill(a, 10);
fill(a, 10); // Try to add duplicates
fill(a, 10);
a.addAll(fill(new TreeSet(), 10));
System.out.println(a);
}
public static void main(String[] args) {
System.out.println("new HashSet()");
test(new HashSet());
/*System.out.println("new TreeSet()");
test(new TreeSet());*/
}
} ///:~
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -