java - 将 HashMap 值添加到 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;

    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();
        }

    }

最佳答案

为了添加到TreeSet(具有自然排序),类应该与其自身进行比较:

private class Base<T extends BaseType> implements Comparable<Base> { ... }

而在您的情况下BaseT相当:

private class Base<T extends BaseType> implements Comparable<T> { ... }

关于java - 将 HashMap 值添加到 TreeSet 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6955320/

相关文章:

具有下界通配符的 Java 泛型

java - 如何在java中有效解析查询参数?

java - 使用多线程时如何深度复制 HashMap

java - Worklight 适配器 - Java 与 JavaScript

java - 无法使用导入的库。安卓工作室。谷歌云端点 jar

java - 保护 JBoss 上远程客户端的 JNDI 安全

java - Lambda 内部或Else获取有界通配符泛型

java - 如何使用来自图像的数据 URI 作为 InputStream?

reactjs - Typescript 编译器不强制执行泛型

java - SparseArray 上 putAll 的等效方法