简体   繁体   English

如何在 Java 中实现我自己的泛型集合?

[英]How can I implement my own generic collection in java?

I have an inteface 'MyCollection' with just two methods : addAll and containsAll which take as a parameter a generic collection.我有一个只有两个方法的接口“MyCollection”:addAll 和 containsAll,它们将通用集合作为参数。 How can I effectively implement these methods in my class so that they would work on any type of collection.我怎样才能在我的类中有效地实现这些方法,以便它们可以处理任何类型的集合。 This is what I've done so far :这是我到目前为止所做的:

The interface:界面:

interface MyCollection<T extends Collection> {
    boolean containsAll(T c);
    boolean addAll(T c);
}

My class where I implement the methods:我在其中实现方法的班级:

  public class MyPersonalCollection<E extends Collection> implements MyCollection {
        private E myCollection;

        public MyPersonalCollection(E myCollection) {
            this.myCollection = myCollection;
        }

        public boolean containsAll(Collection c) {
            return myCollection != null && myCollection.containsAll(c);
        }

        public boolean addAll(Collection c) {
            return myCollection != null && myCollection.addAll(c);
        }
    }

And the tests:和测试:

   @Test
    public void testIfNewCollectionCanBeAdded() {
        ArrayList<String> input = new ArrayList<>();
        MyPersonalCollection<ArrayList<String>> myCollection = new MyPersonalCollection<>(input);

        input.add("first");
        input.add("secon");
        input.add("third");

        assertTrue(myCollection.addAll(input));
    }

    @Test
    public void testIfMyCollectionContainsAnotherCollection() {
        LinkedList<String> list = new LinkedList<>();
        MyPersonalCollection<LinkedList<String>> myCollection = new MyPersonalCollection<>(list);

        list.add("bacon");
        list.add("tuna");
        list.add("steak");
        assertTrue(myCollection.addAll(list));
    }

I also get a warning : Unchecked call to 'containsAll(Collection) as a member of raw type 'Java.Util.Collection" in my class when I call the methods containsAll() and addAll(). So how can I tackle this problem ? Many thanks in advance !我还收到一个警告:当我调用方法 containsAll() 和 addAll() 时,在我的班级中,未经检查地调用 'containsAll(Collection) 作为原始类型'Java.Util.Collection' 的成员。那么我该如何解决这个问题? 提前谢谢了 !

Both E and T extend Collection , but you want to treat a Collection as T in MyCollection in this line: ET扩展了Collection ,但您希望在此行MyCollection Collection 视为MyCollection中的T

return myCollection != null && myCollection.containsAll(c);

Which can be wrong because every Collection is not from type T .这可能是错误的,因为每个Collection都不是来自类型T

Anyway if you are sure that this type casting is safe, simply ignore it and use无论如何,如果您确定这种类型转换是安全的,只需忽略它并使用

@SuppressWarnings("unchecked")

to suppress that warning.抑制该警告。

The problem is that you have to define 2 generic:问题是你必须定义2个泛型:

  • C for the kind of collection C为收藏种类
  • E for the content of the collection E为馆藏内容

I fixed your code, now there is no warnings我修复了你的代码,现在没有警告

interface MyCollection<C> {
  boolean containsAll(C c);
  boolean addAll(C c);
}

public class MyPersonalCollection<E, C extends Collection<E>> 
   implements MyCollection<C> {
  private C collection;

   public MyPersonalCollection(C myCollection) {
     this.collection = myCollection;
   }

   public boolean containsAll(C c) {
     return collection != null && collection.containsAll(c);
   }

   public boolean addAll(C c) {
     return collection != null && collection.addAll(c);
   }
}

You can use your class in the test like this:您可以在测试中使用您的类,如下所示:

MyPersonalCollection<String, LinkedList<String>> myCollection = 
  new MyPersonalCollection<String, LinkedList<String>>(list);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该使用Java集合sort()还是自己实现? - Should I use java collection sort() or implement my own? Java Canvas-如何实现自己的字体并将其绘制到文本中 - Java Canvas - How can I implement my own fonts and draw them to my text 在Java中,如何使用自己的比较条件对未实现Comparable的对象进行排序? - In Java, how can I sort objects that do not implement Comparable with my own compare criteria? 实例化接口时如何实现自己的方法? - How can I implement my own method when instantiating an interface? 如何将自己的方法的操作实施到main方法中? - How can I implement the actions of my own method into the main method? 如何将Class作为参数传递并在Java中返回泛型集合? - How can I pass a Class as parameter and return a generic collection in Java? 我可以实施自己的JPA吗? - Can i implement my own JPA? 我可以在android上实现自己的文件系统吗? - Can I implement my own filesystem on android? 如何在我的backtracker(java)中实现呢? - How can I implement that in my backtracker (java)? 如何在Java中为AES算法创建自己的密钥? - How can I create my own keys for the AES algorithm in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM