javalistgenericsuncheckedunchecked-conversion

Java generic list gives me warning


I don't understand why Java compiler gives me 'unchecked conversion' warning in the following situation:

I have this class:

public class NodeTree<T> {
   T value;
   NodeTree parent;
   List<NodeTree<T>> childs;

   NodeTree(T value, NodeTree parent) {
       this.value = value;
       this.parent = parent;
       this.childs = null;
   }

   public T getValue() { return value; }
   public void setValue(T value) { this.value = value; }

   public NodeTree getParent() { return parent; }
   public void setParent(NodeTree parent) { this.parent = parent; }

   public List<NodeTree<T>> getChilds() {
       if (this.childs == null) {
           this.childs = new LinkedList<NodeTree<T>>();
       }
       return this.childs;
   }
}

and in the main class I have the following instructions:

NodeTree node = new NodeTree<Integer>(10, null);

NodeTree<Integer> child = new NodeTree<Integer>(20, node);      
List<NodeTree<Integer>> childs = node.getChilds();

childs.add(child);

I can't explain why the hell I get warning on the getChilds() line of this type:

warning: [unchecked] unchecked conversion
List<NodeTree<Integer>> childs = node.getChilds();
                                               ^
required: List<NodeTree<Integer>>
found:    List
1 warning

getChilds() function does not return List type, it returns List < NodeTree < T > > type.

Please help me to understand.


Solution

  • Wouldn't it be better to code NodeTree<Integer> node = new NodeTree<>(10, null); instead of NodeTree node = new NodeTree<Integer>(10, null); ? Then the compiler would know the node's type parameter.