7

Following is my simplified graph implementation

import java.util.ArrayList;
import java.util.List;

public class TreeNode<E extends Comparable<E>> {
    private E data;
    private List<TreeNode<E>> children;

    public TreeNode(E value) {
        data = value;
        children = new ArrayList<>();
    }

    public E getData() {
        return data;
    }

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

    public List<TreeNode<E>> getChildren() {
        return children;
    }

    public void setChildren(List<TreeNode<E>> children) {
        this.children = children;
    }

}

And I am writing code to find if 2 nodes are connected in a directed graph. I am getting compilation error

public static boolean findIfPathExists(TreeNode<? extends Comparable<?>> start, TreeNode<? extends Comparable<?>> end) {
    Set<TreeNode<? extends Comparable<?>>> visitedNodes = new HashSet<TreeNode<? extends Comparable<?>>>();
    return findIfPathExists(start, end, visitedNodes);
}

private static boolean findIfPathExists(TreeNode<? extends Comparable<?>> start, TreeNode<? extends Comparable<?>> end,
        Set<TreeNode<? extends Comparable<?>>> visitedNodes) {
    if(start == end) return true;
    visitedNodes.add(start);
    List<TreeNode<? extends Comparable<?>>> children = start.getChildren();
    for (TreeNode<? extends Comparable<?>> child : children) {
        if(visitedNodes.contains(child)) continue;
        if(findIfPathExists(child, end, visitedNodes)) return true;
    }
    return false;
}

I am getting error at line start.getchildren

 Type mismatch: cannot convert from List<TreeNode<capture #11 -of? extends 
Comparable<?>>> to List<TreeNode<? extends Comparable<?>>>
2
  • The problem is that wildcards could be anything and hence the compiler won't allow writing operations like visitedNodes.add(start); (which I guess is the line you're getting the error at - hint: don't make us guess). Commented Nov 23, 2016 at 16:39
  • @Thomas presumably, the one containing List<TreeNode<? extends Comparable<?>>>. Commented Nov 23, 2016 at 16:55

2 Answers 2

6

Add a type variable to your method signatures:

public static <T extends Comparable<T>> boolean findIfPathExists(
    TreeNode<T> start, TreeNode<T> end) {

private static <T extends Comparable<T>> boolean findIfPathExists(
    TreeNode<T> start, TreeNode<T> end, Set<TreeNode<T>> visitedNodes) {

and then use T wherever you currently have ? extends Comparable<?>.

Sign up to request clarification or add additional context in comments.

Comments

0

If you wanted to keep the wildcards, change the line from

List<TreeNode<? extends Comparable<?>>> children = start.getChildren()

to

List<? extends TreeNode<? extends Comparable<?>>> children = start.getChildren();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.