MultiSet 类中添加/删除/等于/字符串方法的问题

发布于 2024-12-20 08:42:03 字数 2347 浏览 1 评论 0原文

这是我的课程:

public class MultiSet<E> extends AbstractCollection<E>
{
    private int size = 0;
    private Map<E, Integer> values = new HashMap<E, Integer>();

    public MultiSet()
    {

    }

    public MultiSet(Collection<E> c)
    {
        addAll(c);
    }

    @Override
    public boolean add(E o)
    {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean remove(Object o)
    {
        throw new UnsupportedOperationException();
    }

    public Iterator<E> iterator()
    {
        return new Iterator<E>()
        {
            private Iterator<E> iterator = values.keySet().iterator();
            private int remaining = 0;
            private E current = null;

            public boolean hasNext()
            {
            return remaining > 0 || iterator.hasNext();
            }

            public E next()
            {
                if (remaining == 0)
                {
                    remaining = values.get(current);
                }
                remaining--;
                return current;
            }
            public void remove()
            {
                throw new UnsupportedOperationException();
            }
        };
    }

        public boolean equals(Object object)
        {
            if (this == object) return true;
            if (this == null) return false;
            if (this.getClass() != object.getClass()) return false;
            MultiSet<E> o = (MultiSet<E>) object;
            return o.values.equals(values);
        }


        public int hashCode()
        {
            return values.hashCode()*163 + new Integer(size).hashCode()*389;
        }

        public String toString()
        {
            String res = "";
            for (E e : values.keySet());
                    //res = ???;
            return getClass().getName() + res;
        }

        public int size()
        {
            return size;
        }
    }

所以基本上,我需要正确实现我的添加/删除方法,以向 Set 添加或删除元素。

对我来说,似乎我的 equals 是正确的,但 Eclipse 说:

MultiSet<E> o = (MultiSet<E>) object;

有一个从对象到 Multiset的未经检查的转换 有什么想法吗?

另外,在我的 toString 方法中,我不是 100% 确定如何定义“res”?

谢谢, // 克里斯

This is my class:

public class MultiSet<E> extends AbstractCollection<E>
{
    private int size = 0;
    private Map<E, Integer> values = new HashMap<E, Integer>();

    public MultiSet()
    {

    }

    public MultiSet(Collection<E> c)
    {
        addAll(c);
    }

    @Override
    public boolean add(E o)
    {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean remove(Object o)
    {
        throw new UnsupportedOperationException();
    }

    public Iterator<E> iterator()
    {
        return new Iterator<E>()
        {
            private Iterator<E> iterator = values.keySet().iterator();
            private int remaining = 0;
            private E current = null;

            public boolean hasNext()
            {
            return remaining > 0 || iterator.hasNext();
            }

            public E next()
            {
                if (remaining == 0)
                {
                    remaining = values.get(current);
                }
                remaining--;
                return current;
            }
            public void remove()
            {
                throw new UnsupportedOperationException();
            }
        };
    }

        public boolean equals(Object object)
        {
            if (this == object) return true;
            if (this == null) return false;
            if (this.getClass() != object.getClass()) return false;
            MultiSet<E> o = (MultiSet<E>) object;
            return o.values.equals(values);
        }


        public int hashCode()
        {
            return values.hashCode()*163 + new Integer(size).hashCode()*389;
        }

        public String toString()
        {
            String res = "";
            for (E e : values.keySet());
                    //res = ???;
            return getClass().getName() + res;
        }

        public int size()
        {
            return size;
        }
    }

So basically, i need to implement my add/remove-methods correctly, to add or remove elements to/from the Set.

To me, it seems like my equals is correct, but Eclipse says that in the line:

MultiSet<E> o = (MultiSet<E>) object;

there is an unchecked cast from object to Multiset<E>
Any thoughts?

Also, in my toString method, i'm not 100% sure how to define "res"?

Thanks,
// Chris

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

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

发布评论

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

评论(1

∝单色的世界 2024-12-27 08:42:03

请使用它:

MultiSet<?> o = (MultiSet<?>) object;

由于 java 中泛型的实现方式,这是必要的。

use this instead:

MultiSet<?> o = (MultiSet<?>) object;

this is necessary due to how generics are implemented in java.

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