看Thinking in JAVA有问题,问问大家。class SetType {
int i; public SetType(int n) {
i = n;
} public boolean equals(Object o) {
return o instanceof SetType && (i == ((SetType) o).i);
} public String toString() {
return Integer.toString(i);
}
}class HashType extends SetType {
public HashType(int n) {
super(n);
} public int hashCode() {
return i;
}
}class TreeType extends SetType
implements Comparable<TreeType> {
public TreeType(int n) {
super(n);
} public int compareTo(TreeType arg) {
return (arg.i < i ? -1 : (arg.i == i ? 0 : 1));
}
}public class TypesForSets {
static <T> Set<T> fill(Set<T> set, Class<T> type) {
try {
for (int i = 0; i < 10; i++)
set.add(
type.getConstructor(int.class).newInstance(i));
} catch (Exception e) {
throw new RuntimeException(e);
}
return set;
} static <T> void test(Set<T> set, Class<T> type) {
fill(set, type);
fill(set, type); // Try to add duplicates
fill(set, type);
System.out.println(set);
} public static void main(String[] args) {
test(new HashSet<HashType>(), HashType.class);
test(new LinkedHashSet<HashType>(), HashType.class);
test(new TreeSet<TreeType>(), TreeType.class);
// Things that don't work:
test(new HashSet<SetType>(), SetType.class);
test(new HashSet<SetType>(), SetType.class);
test(new LinkedHashSet<SetType>(), SetType.class);
test(new LinkedHashSet<TreeType>(), TreeType.class);
try {
test(new TreeSet<SetType>(), SetType.class);
} catch (Exception e) {
System.out.println(e.getMessage());
}
try {
test(new TreeSet<HashType>(), HashType.class);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
} 输出结果是:
[2, 4, 9, 8, 6, 1, 3, 7, 5, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[9, 9, 7, 5, 1, 2, 6, 3, 0, 7, 2, 4, 4, 7, 9, 1, 3, 6, 2, 4, 3, 0, 5, 0, 8, 8, 8, 6, 5, 1]
[0, 5, 5, 6, 5, 0, 3, 1, 9, 8, 4, 2, 3, 9, 7, 3, 4, 4, 0, 7, 1, 9, 6, 2, 1, 8, 2, 8, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
我想问的是,为什么
行1:test(new HashSet<HashType>(), HashType.class);
行2:test(new LinkedHashSet<HashType>(), HashType.class);
行3:test(new TreeSet<TreeType>(), TreeType.class);
是不重复的,而下面的重复
行4:test(new HashSet<SetType>(), SetType.class);
行5:test(new HashSet<SetType>(), SetType.class);
行6:test(new LinkedHashSet<SetType>(), SetType.class);
行7:test(new LinkedHashSet<TreeType>(), TreeType.class);
问题1:为什么行3的TreeType没有实现hashCode(),他什么不重复?我的想法是(1)它实现了的comparaTo方法(2)如果是因为equals方法的话,那问题2就来了。
问题2:4、5、6、7行的SetType和TreeType实现了equals方法,但他们输出的结果还是重复的?我明白在比较之前会调用hashCode(),但hashCode()相等后还是会调用equals方法啊,那为什么还会重复?
int i; public SetType(int n) {
i = n;
} public boolean equals(Object o) {
return o instanceof SetType && (i == ((SetType) o).i);
} public String toString() {
return Integer.toString(i);
}
}class HashType extends SetType {
public HashType(int n) {
super(n);
} public int hashCode() {
return i;
}
}class TreeType extends SetType
implements Comparable<TreeType> {
public TreeType(int n) {
super(n);
} public int compareTo(TreeType arg) {
return (arg.i < i ? -1 : (arg.i == i ? 0 : 1));
}
}public class TypesForSets {
static <T> Set<T> fill(Set<T> set, Class<T> type) {
try {
for (int i = 0; i < 10; i++)
set.add(
type.getConstructor(int.class).newInstance(i));
} catch (Exception e) {
throw new RuntimeException(e);
}
return set;
} static <T> void test(Set<T> set, Class<T> type) {
fill(set, type);
fill(set, type); // Try to add duplicates
fill(set, type);
System.out.println(set);
} public static void main(String[] args) {
test(new HashSet<HashType>(), HashType.class);
test(new LinkedHashSet<HashType>(), HashType.class);
test(new TreeSet<TreeType>(), TreeType.class);
// Things that don't work:
test(new HashSet<SetType>(), SetType.class);
test(new HashSet<SetType>(), SetType.class);
test(new LinkedHashSet<SetType>(), SetType.class);
test(new LinkedHashSet<TreeType>(), TreeType.class);
try {
test(new TreeSet<SetType>(), SetType.class);
} catch (Exception e) {
System.out.println(e.getMessage());
}
try {
test(new TreeSet<HashType>(), HashType.class);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
} 输出结果是:
[2, 4, 9, 8, 6, 1, 3, 7, 5, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[9, 9, 7, 5, 1, 2, 6, 3, 0, 7, 2, 4, 4, 7, 9, 1, 3, 6, 2, 4, 3, 0, 5, 0, 8, 8, 8, 6, 5, 1]
[0, 5, 5, 6, 5, 0, 3, 1, 9, 8, 4, 2, 3, 9, 7, 3, 4, 4, 0, 7, 1, 9, 6, 2, 1, 8, 2, 8, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
我想问的是,为什么
行1:test(new HashSet<HashType>(), HashType.class);
行2:test(new LinkedHashSet<HashType>(), HashType.class);
行3:test(new TreeSet<TreeType>(), TreeType.class);
是不重复的,而下面的重复
行4:test(new HashSet<SetType>(), SetType.class);
行5:test(new HashSet<SetType>(), SetType.class);
行6:test(new LinkedHashSet<SetType>(), SetType.class);
行7:test(new LinkedHashSet<TreeType>(), TreeType.class);
问题1:为什么行3的TreeType没有实现hashCode(),他什么不重复?我的想法是(1)它实现了的comparaTo方法(2)如果是因为equals方法的话,那问题2就来了。
问题2:4、5、6、7行的SetType和TreeType实现了equals方法,但他们输出的结果还是重复的?我明白在比较之前会调用hashCode(),但hashCode()相等后还是会调用equals方法啊,那为什么还会重复?
而tree是只看compareable的比较方法 跟hashcode和equal无关