如何创建一个实现 java.util.collections 的类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4301163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:45:11  来源:igfitidea点击:

How to create a class that implements java.util.collections

javacollections

提问by Eternal Learner

I am trying to create a class say MyStackthat would implement a java.util.collections class. MyStackwill override some methods of the collections class like add(similar to push), remove(similar to pop) etc.. I intend to model the class on the same lines as a Setor other interfaces of the collection class except that MyStackwould not be an interface or abstract class and that we can create objects of type MyStack.

我正在尝试创建一个MyStack可以实现 java.util.collections 类的类。MyStack将覆盖像添加收藏类的一些方法(类似于推),删除(类似于POP)等。我打算类模型在同一行作为Set或集合类的其他接口不同的是MyStack不会是一个接口或抽象类,我们可以创建类型的对象MyStack

I have problems with the syntax as I am not sure if I am proceeding in the right direction.All I have so far is something like this - NOTE - None of the methods have been defined so far - I am trying to get teh skeleton right before proceeding to define the methods.

我的语法有问题,因为我不确定我是否朝着正确的方向前进。到目前为止,我所拥有的只是这样的 - 注意 - 到目前为止还没有定义任何方法 - 我正在努力使骨架正确在继续定义方法之前。

import java.util.*;


public class MyStak implements java.util.Collection<E>{

    public boolean add(E o){

               return false;        
        }

       public boolean addAll(Collection c){
        return false; 

        }

       public void clear() {

       }

        public boolean contains(Object o){
          return false;

        }

        public boolean containsAll(Collection o){
          return false;

        }

        public boolean equals(Object c){
          return false; 
        }

        public int hashcode(){
          return 0; 
        }

        public boolean isEmpty(){
          return false; 
        }

        public Iterator iterator(){
          return null;

        }

        public boolean remove(Object o){
          return false; 
        }

        public boolean removeAll(Collection o){
          return false; 
        }

        public boolean retainAll(Collection o){
          return false; 
        }

        public int size(){
          return 1; 
        }

         public Object[] toArray(){
           return null;

         }

         public Object[] toArray(Object[] a){
           return null; 
         }

    }

I have a couple of compile time errors like -

我有几个编译时错误,例如 -

    +public class MyStak implements java.util.Collection<E>{
Multiple markers at this line
    - The type MyStak must implement the inherited abstract method 
     Collection<E>.add(E)
    - E cannot be resolved to a type

    +public boolean add(E o){
Multiple markers at this line
    - E cannot be resolved to a type
    - implements 
     java.util.Collection<E>.add

Any code modifications, examples , corrections to my code , links to tutorials etc will we highly appreciated.

我们将不胜感激任何代码修改、示例、对我的代码的更正、教程链接等。

回答by John Kugelman

Make sure to throw on a <E>specification on your class as well:

确保<E>在你的班级上也有一个规范:

public class MyStak<E> implements java.util.Collection<E>
                   ^^^

If you want to make life easier on yourself try sub-classing AbstractCollectioninstead of implementing Collectiondirectly. It provides reasonable default implementations for most of the methods to minimize the amount of code you need to write.

如果您想让自己的生活更轻松,请尝试使用子类AbstractCollection而不是Collection直接实现。它为大多数方法提供了合理的默认实现,以最大限度地减少您需要编写的代码量。

java.util

Class AbstractCollection<E>

This class provides a skeletal implementation of the Collectioninterface, to minimize the effort required to implement this interface.

To implement an unmodifiable collection, the programmer needs only to extend this class and provide implementations for the iteratorand sizemethods. (The iterator returned by the iteratormethod must implement hasNextand next.)

To implement a modifiable collection, the programmer must additionally override this class's addmethod (which otherwise throws an UnsupportedOperationException), and the iterator returned by the iteratormethod must additionally implement its removemethod.

The programmer should generally provide a void(no argument) and Collectionconstructor, as per the recommendation in the Collectioninterface specification.

实用程序

班级 AbstractCollection<E>

此类提供了Collection接口的骨架实现,以最大限度地减少实现此接口所需的工作。

要实现一个不可修改的集合,程序员只需要扩展这个类并提供iteratorsize方法的实现。(iterator方法返回的迭代器必须实现hasNextnext。)

要实现可修改的集合,程序员必须额外覆盖此类的add方法(否则会抛出UnsupportedOperationException),并且该iterator方法返回的迭代器必须额外实现其remove方法。

根据接口规范中的建议,程序员通常应提供void(无参数)和Collection构造函数Collection

回答by EboMike

You were VERY close!

你非常接近!

You just need to define E in your subclass as well:

你只需要在你的子类中定义 E :

public class MyStak<E> implements java.util.Collection<E>

The idea is that you could have a subclass with, say, <E, F, G>, and you implement two different interfaces, one using E, one using F. That, or MyStak could be specialized and use a specific class for Collection, instead of a generic E.

这个想法是你可以有一个子类,比如说,<E, F, G>你实现两个不同的接口,一个使用 E,一个使用 F。那个,或者 MyStak 可以被特化并使用特定的类 for Collection,而不是泛型E

回答by Alexandr

And one recommendation, if you plan to implement your own collection interface consider extending corresponding abstract class, but not implementing interface itself, cause abstract classes implement methods general to the interface. Look at: AbstractCollection, AbstractSet, AbstractList

还有一个建议,如果你打算实现自己的集合接口,可以考虑扩展对应的抽象类,而不是实现接口本身,导致抽象类实现接口通用的方法。查看:AbstractCollection、AbstractSet、AbstractList

回答by Laurence Gonsalves

Instead of:

代替:

public class MyStak implements java.util.Collection<E>{

try:

尝试:

public class MyStak<E> implements java.util.Collection<E>{

回答by Sandy

When we implement Collection along with we must implement Iterator<E>also. Which is use to iterate over the item on which you want.

当我们实现 Collection with 时,我们Iterator<E>也必须实现。哪个用于迭代您想要的项目。

回答by Nowaker

Is defining a stack what you really want to achieve? If so, then go ahead and define it without even implementing Collection interface - it will be OK for simple cases. Or use an existing class - java.util.Stack.

定义堆栈是您真正想要实现的目标吗?如果是这样,那么在不实现 Collection 接口的情况下继续定义它 - 对于简单的情况就可以了。或者使用现有的类 - java.util.Stack。