将HashMap值添加到TreeSet时出错 [英] Error when adding HashMap values to TreeSet
问题描述
Here is some sample code...I always seem to get a ClassCastException...anybody point out what I am doing wrong?
c $ c> 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;
导入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);
收集< A< SomeType>> coll = map.values();
Set< A< SomeType>> set = Sets.newTreeSet(coll);
System.out.println(完成。);
// EXCEPTION ...
//com.query.ClassCastExceptionTest$A不能转换为
//com.query.ClassCastExceptionTest$BaseType
}
私有类A< T扩展了BaseType>延伸Base< T> {
public A(int i){
super(i);
}
}
私有类Base< T扩展了BaseType>实现可比< T> {
私人整数ID;
public Base(int id){
this.id = id;
}
/ **
* @return the id
* /
public Integer getId(){
return id;
$ b $ **
* @param id设置
* /
的标识b @SuppressWarnings(未使用)
public void setId(int id){
this.id = id;
}
@Override
public int compareTo(T o){
return getId()。compareTo(o.getId());
$ b private class SomeType extends BaseType {
$ b $ @Override
public Integer getId(){
return 0 ;
}
@Override
public int compareTo(BaseType o){
return this.getId()。compareTo(o.getId());
}
}
私有抽象类BaseType实现了Comparable< BaseType> {
public abstract Integer getId();
}
}
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();
}
}
推荐答案
private class Base<T extends BaseType> implements Comparable<Base> { ... }
而在 T
:
private class Base<T extends BaseType> implements Comparable<T> { ... }
这篇关于将HashMap值添加到TreeSet时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!