I'm studying Java generics and I was wondering what are the differences due to the use of the wildcard <?>
in these two codes:
First
public class Tree<T> {
private final T node;
private final Set<Tree<?>> children;
public Tree(T node, Set<Tree<?>> children) {
this.node = node;
this.children = children;
}
public static void main(String[] args) {
Set<Tree<?>> children = new HashSet<>();
children.add(new Tree<Integer>(new Integer(0), new HashSet<>()));
children.add(new Tree<String>("asdf", new HashSet<>()));
Tree<Integer> ti = new Tree<Integer>(new Integer(1), children);
}
}
Second
public class Tree<T> {
private final T node;
private final Set<Tree> children;
public Tree(T node, Set<Tree> children) {
this.node = node;
this.children = children;
}
public static void main(String[] args) {
Set<Tree> children = new HashSet<>();
children.add(new Tree<Integer>(new Integer(0), new HashSet<>()));
children.add(new Tree<String>("asdf", new HashSet<>()));
Tree<Integer> ti = new Tree<Integer>(new Integer(1), children);
}
}
I wrote a simple main
that uses the class, but I'm interested in the differences resulting from different uses of this class (I didn't see any difference with this main
).
EDIT: don't know if it's clear from the example, but I don't necessarily want nodes of tree of the same type.