Java 向树集添加HashMap值时出错
这是一些示例代码…我似乎总是得到ClassCastException…有人指出我做错了什么吗Java 向树集添加HashMap值时出错,java,generics,hashmap,classcastexception,treeset,Java,Generics,Hashmap,Classcastexception,Treeset,这是一些示例代码…我似乎总是得到ClassCastException…有人指出我做错了什么吗 package com.query; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.TreeSet;
package com.query;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.junit.Test;
import com.google.common.collect.Sets;
public class ClassCastExceptionTest {
@Test
public void test() {
A<SomeType> a1 = new A<SomeType>(1);
A<SomeType> a2 = new A<SomeType>(2);
A<SomeType> a3 = new A<SomeType>(3);
Map<String, A<SomeType>> map = new HashMap<String, A<SomeType>>();
map.put("A1", a1);
map.put("A2", a2);
map.put("A3", a3);
Collection<A<SomeType>> coll = map.values();
Set<A<SomeType>> set = Sets.newTreeSet(coll);
System.out.println("Done.");
//EXCEPTION...
//com.query.ClassCastExceptionTest$A cannot be cast to
//com.query.ClassCastExceptionTest$BaseType
}
private class A<T extends BaseType> extends Base<T> {
public A(int i) {
super(i);
}
}
private class Base<T extends BaseType> implements Comparable<T> {
private Integer id;
public Base(int id) {
this.id = id;
}
/**
* @return the id
*/
public Integer getId() {
return id;
}
/**
* @param id the id to set
*/
@SuppressWarnings("unused")
public void setId(int id) {
this.id = id;
}
@Override
public int compareTo(T o) {
return getId().compareTo(o.getId());
}
}
private class SomeType extends BaseType {
@Override
public Integer getId() {
return 0;
}
@Override
public int compareTo(BaseType o) {
return this.getId().compareTo(o.getId());
}
}
private abstract class BaseType implements Comparable<BaseType> {
public abstract Integer getId();
}
}
package.com.query;
导入java.util.ArrayList;
导入java.util.Collection;
导入java.util.HashMap;
导入java.util.Map;
导入java.util.Set;
导入java.util.TreeSet;
导入org.junit.Test;
导入com.google.common.collect.set;
公共类ClassCastExceptionTest{
@试验
公开无效测试(){
a1=新的A(1);
A a2=新的A(2);
a3=新的A(3);
Map Map=newhashmap();
地图放置(“A1”,A1);
地图放置(“A2”,A2);
地图放置(“A3”,A3);
Collection coll=map.values();
Set Set=Sets.newTreeSet(coll);
System.out.println(“完成”);
//例外。。。
//无法将com.query.ClassCastExceptionTest$A强制转换为
//ClassCastExceptionTest$BaseType
}
私有类A扩展了基本类{
公共A(国际一){
超级(i);
}
}
私有类基类实现了可比较的{
私有整数id;
公共基地(int id){
this.id=id;
}
/**
*@返回id
*/
公共整数getId(){
返回id;
}
/**
*@param id要设置的id
*/
@抑制警告(“未使用”)
公共无效集合id(内部id){
this.id=id;
}
@凌驾
公共内部比较(TO){
返回getId().compareTo(o.getId());
}
}
私有类SomeType扩展了BaseType{
@凌驾
公共整数getId(){
返回0;
}
@凌驾
公共整数比较(基类型o){
将这个.getId().compareTo(o.getId())返回;
}
}
私有抽象类BaseType实现了可比较的{
公共抽象整数getId();
}
}
为了添加到树集
(自然排序),类应该与其自身具有可比性:
private class Base<T extends BaseType> implements Comparable<Base> { ... }
树集使用对象比较方法进行排序。因此,当将第二个A实例添加到树集时,将调用一个#compareTo,另一个A实例作为参数,但由于此方法期望BaseType(或BaseType的子类)作为参数,因此会抛出一个ClassCastException。感谢axtavt…将其更改为
private类基类实现了可比较的{…}
,解决了这个问题。
private class Base<T extends BaseType> implements Comparable<T> { ... }