将HashMap值添加到TreeSet时出错

时间:2011-08-05 11:09:50

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

    }

2 个答案:

答案 0 :(得分:2)

为了被添加到TreeSet(具有自然顺序),一个类应该与它自己相比:

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

而在您的情况下BaseT相当:

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

答案 1 :(得分:0)

TreeSet使用对象compareTo方法进行排序。因此,当将第二个A实例添加到TreeSet时,将以另一个A实例作为参数调用A#compareTo,但由于此方法期望BaseType(或BaseType的子类)作为参数,因此抛出ClassCastException。