0

I have a Node class:

public class Node<T extends MySuperClass> {

   private T data;
   private Node<? extends MySuperClass> parent;
   private List<Node<? extends MySuperClass>> children;

   public Node(T data, Node<? extends MySuperClass> parent, List<Node<? extends MySuperClass>> children) {
     this.data = data;
     this.parent = parent;
     this.children = children;
   }

   public T getData() {
      return data;
   }

   public Node<? extends MySuperClass> getParent() {
          return parent;
   }

   public List<Node<? extends MySuperClass>> getChildren() {
          return children;
   }

   public void setData(T data) {
         this.data = data;
   }

   public void setParent(Node<? extends MySuperClass> parent) {
         this.parent = parent;
   }

   public void setChildren(List<Node<? extends MySuperClass>> children) {
          this.children = children;
   }
}

I need to create a map of Generic Node defined above. I have to write something like this

List<Map<Long, Node<? extends MySuperClass>>> tree = new ArrayList<Map<Long, Node< extends MySuperClass>>>();

When I try to add an instance of map to the list

public MyClass extends MySuperClass{

}

    Map<Long, Node<MyClass>> myMap = new HashMap<Long,Node<MyClass>>();

    tree.add(myMap);

The Compiler gives me the following message:

 The method add(Map<Long,Node<? extends MySuperClass>>) in the type List<Map<Long,Node<? extends MySuperClass>>> is not applicable for the arguments (Map<Long,Node<MyClass>>)

Syntactically it is correct. I Can't understand why it doesn't work.

6
  • my bad, answer was off, trying to think now.. Commented Nov 6, 2014 at 15:14
  • 1
    Can you please fix this line Map<Long, Node<MyClass>> myMap = new ArrayList<Long,Node<MyClass>>? Write what you really use there. Commented Nov 6, 2014 at 15:27
  • @Tom yea that was bothering me too, however what if he wants to use methods from Map (interface or class??) but he wants an array list? Commented Nov 6, 2014 at 15:28
  • @AbdulAhmad This line won't even compile. If he want something, then he should write it in his question and not duck tape some code lines. And btw: you can't mix data types (i.e. use methods of class A on the unrelated object B). Commented Nov 6, 2014 at 15:30
  • Yes, it doesn't look like this code is accurate. My stripped down version of this compiles fine. The line where tree is initialized is not syntactically correct. OP, can you change this to have a minimal example of something that does not compile? Commented Nov 6, 2014 at 15:35

1 Answer 1

2

Your Map should be defined the same type as your List.

This is because the List expects a type of Map<Long, Node<? extends MySuperClass>>. The type Node<? extends MySuperClass> and Node<MyClass> are not the same.

From http://docs.oracle.com/javase/tutorial/java/generics/inheritance.html

Note: Given two concrete types A and B (for example, Number and Integer), MyClass<A> has no relationship to MyClass<B>, regardless of whether or not A and B are related. The common parent of MyClass<A> and MyClass<B> is Object.

Note : I add to add a default constructor to your Node class to get the next four lines to compile since I didnt want to type in the params for the other constructor.

List<Map<Long, Node<? extends MySuperClass>>> tree = new ArrayList<Map<Long, Node<? extends MySuperClass>>>();
Map<Long, Node<? extends MySuperClass>> myMap = new HashMap<Long, Node<? extends MySuperClass>>();
myMap.put(1L, new Node<MyClass>());
tree.add(myMap);
Sign up to request clarification or add additional context in comments.

1 Comment

OP may be conofused by the fact that, in his case, the inheritance relationship between A and B is created by bounded wildcards rather than concrete types (see docs.oracle.com/javase/tutorial/java/generics/subtyping.html). Still, it does not matter how relationship came about - it's still not taken into account.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.