posdef
posdef

Reputation: 6532

Problems with Java generics and collections

I have a class that represents a tree-like structure, the essential bits look like this:

public Node<T> placeAll(Collection<T> elements){    
    for (T e : elements)
        addElement(e);

    // LOG/DEBUG etc
    return root;
}

public void addElement(T el) {
    Node<T> node = new Node<T>(el);
    addElement(root, node);
}

private void addElement(Node<T> parent, Node<T> child) {
    // .... PLACE THE NODE
}

Now this works perfectly fine when I place the nodes one by one in a test case:

public void test() {

    List<Integer> s1 = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
    // 13 more lists
    List<Integer> s15 = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 221, 251); 

    Hypergraph<Object> hg = new Hypergraph<>(...);

        hg.addElement(s1);
        System.out.println(hg.getRoot().toStringTree());
        System.out.println();
              .
              .
              .
        hg.addElement(s15);
        System.out.println(hg.getRoot().toStringTree());
        System.out.println();
    }

If I add the following line hg.placeAll(Arrays.asList(s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13,s14,s15));

to my test case, I get an error regarding the use of generics:

The method placeAll(Collection<Object>) in the type Hypergraph<Object> is not applicable for the arguments (List<List<Integer>>)

I don't quite understand this... If addElement(T el) works fine when I call it with T resolved to List<Integer>, why does List<List<Integer>> comply to placeAll(Collection<T> c)? Considering that List<T> is a Collection<T> I can't make sense out of this..

Upvotes: 3

Views: 141

Answers (1)

tobias_k
tobias_k

Reputation: 82899

The problem is that the method expects a Collection<Object> (as T seems to be Object in your example), but you are passing a Collection<List<Integer>>. And while a List<Integer> is an Object, a Collection<List<Integer>> is not a subclass of a Collection<Object>.

Change the method signature to accept a Collection<? extends T>, then it should work.

public Node<T> placeAll(Collection<? extends T> elements) {   

Upvotes: 3

Related Questions