将 HashMap 值添加到 TreeSet 时出错

发布于 2024-11-28 05:27:48 字数 2459 浏览 5 评论 0原文

这是一些示例代码...我似乎总是遇到 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();
        }

    }

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

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执手闯天涯 2024-12-05 05:27:48

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

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

而在您的情况下,BaseT 进行比较:

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

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> { ... }
笑叹一世浮沉 2024-12-05 05:27:48

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文