简体   繁体   中英

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. 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 !

Both E and T extend Collection , but you want to treat a Collection as T in MyCollection in this line:

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

Which can be wrong because every Collection is not from type 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:

  • C for the kind of collection
  • E for the content of the collection

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM