开发者

Error when adding HashMap values to TreeSet

开发者 https://www.devze.com 2023-03-25 21:57 出处:网络
Here is some sample code...I always seem to get a ClassCastException...anybody point out what I am doing wrong?

Here is some sample code...I always seem to get a ClassCastException...anybody point out what I am doing wrong?

    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$开发者_C百科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();
        }

    }


In order to be added to TreeSet (with natural ordering) a class should be comparable with itself:

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

whereas in your case Base is comparable with T:

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


A TreeSet uses the objects compareTo method for sorting. So when adding the second A instance to the TreeSet, A#compareTo is invoked with the other A instance as argument, but as this method is expecting BaseType (or a subclass of BaseType) as argument, a ClassCastException is thrown.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号