bounded-wildcard

generics error: not applicable for the arguments

安稳与你 提交于 2019-11-27 09:42:52
Can someone explain to me why the following code does not work? public class Test { interface Strategy<T> { void execute(T t); } public static class DefaultStrategy<T> implements Strategy<T> { @Override public void execute(T t) {} } public static class Client { private Strategy<?> a; public void setStrategy(Strategy<?> a) { this.a = a; } private void run() { a.execute("hello world"); } } public static void main(String[] args) { Client client = new Client(); client.setStrategy(new DefaultStrategy<String>()); client.run(); } } I'm getting the following error: The method execute(capture#3-of ?)

Why can't assign I <? extends Type> to <Type>?

风流意气都作罢 提交于 2019-11-27 07:48:15
问题 The following statements: URLClassLoader ucl = (URLClassLoader) ClassLoader.getSystemClassLoader(); Class<URLClassLoader> uclc = ucl.getClass(); fail with error: Type mismatch: cannot convert from Class<capture#2-of ? extends URLClassLoader> to Class<URLClassLoader> Why do I need a cast, here? I found several posts explaining why You can't do the reverse (assign T to a ), but that's (kind of) obvious and understood. NOTE: I am coding this under eclipse Luna, so I don't know if it's a Luna

Cannot Instantiate Type in generics

半腔热情 提交于 2019-11-27 07:47:47
问题 I have this class public class Tree<T> { //List of branches for this tree private List<Tree<? super T>> branch = new ArrayList<Tree<? super T>>(); public Tree(T t){ this.t = t; } public void addBranch(Tree< ? super T> src){ branch.add(src); } public Tree<? extends T> getBranch(int branchNum){ return (Tree<? extends T>) branch.get(branchNum); } private T t; } And I am trying to create a variable out of this class using this public static void main(String[] args){ Tree<? super Number> num2 =

Inferred wildcard generics in return type

本小妞迷上赌 提交于 2019-11-27 02:39:29
问题 Java can often infer generics based on the arguments (and even on the return type, in contrast to e.g. C#). Case in point: I've got a generic class Pair<T1, T2> which just stores a pair of values and can be used in the following way: Pair<String, String> pair = Pair.of("Hello", "World"); The method of looks just like this: public static <T1, T2> Pair<T1, T2> of(T1 first, T2 second) { return new Pair<T1, T2>(first, second); } Very nice. However, this no longer works for the following use-case,

Java: bounded wildcards or bounded type parameter?

心已入冬 提交于 2019-11-27 02:32:22
Recently, I read this article: http://download.oracle.com/javase/tutorial/extra/generics/wildcards.html My question is, instead of creating a method like this: public void drawAll(List<? extends Shape> shapes){ for (Shape s: shapes) { s.draw(this); } } I can create a method like this, and it works fine: public <T extends Shape> void drawAll(List<T> shapes){ for (Shape s: shapes) { s.draw(this); } } Which way should I use? Is wildcard useful in this case? polygenelubricants It depends on what you need to do. You need to use the bounded type parameter if you wanted to do something like this:

Difference between Scala's existential types and Java's wildcard by example?

坚强是说给别人听的谎言 提交于 2019-11-27 01:03:53
问题 A bit more specific than Stack Overflow question What is an existential type? , what is the difference between Scala's existential types and Java's wildcard , prefereably with some illustrative example? In everything I've seen so far, they seem to be pretty equivalent. A few references. Martin Odersky mentions them; Google's top hit for my question : MO: The original wildcard design ... was inspired by existential types. In fact the original paper had an encoding in existential types. But

Adding an element inside a wildcard type ArrayList

江枫思渺然 提交于 2019-11-26 23:42:45
问题 I am trying to add an element in a list where the list type parameter is a wildcard that extends Question ArrayList<? extends Question> id = new ArrayList<? extends Question>(); id.add(new Identification("What is my name?","some",Difficulty.EASY)); map.put("Personal", id); Where identification is a subclass of Question. QUestion is an abstract class. it is giving me this error On Line #1 Cannot instantiate the type ArrayList<? extends Question> And on Line #2 The method add(capture#2-of ?

Bounded-wildcard related compiler error

白昼怎懂夜的黑 提交于 2019-11-26 23:18:50
I am wondering what is wrong with this code: Map <? extends String, ? extends Integer> m = null; Set<Map.Entry<? extends String, ? extends Integer>> s = m.entrySet(); The compiler complains with the error message: Type mismatch: cannot convert from Set<Map.Entry<capture#1-of ? extends String,capture#2-of ? extends Integer>> to Set<Map.Entry<? extends String,? extends Integer>> What should the type of s be? Eclipse suggests Set<?> but I am trying to get more specific than that. Paul Bellora This issue is addressed in this old Apache thread : The problem is that the entrySet() method is

Java generic methods: super can't be used?

╄→尐↘猪︶ㄣ 提交于 2019-11-26 23:15:03
问题 So I have this method: protected void collectSelectedItems(ListSelectionModel lsm, Collection<? super MyItemClass> result) { for (int i : GUI.getSelectionIndices(lsm)) { result.add(getItemByDisplayIndex(i)); } } I'd like to return the collection instead of having a void method: protected <T super MyItemClass> Collection<T> collectSelectedItems(ListSelectionModel lsm, Collection<T> result) { for (int i : GUI.getSelectionIndices(lsm)) { result.add(getItemByDisplayIndex(i)); } return result; }

Java nested generic type

南笙酒味 提交于 2019-11-26 21:51:43
How come one must use the generic type Map<?, ? extends List<?>> instead of a simpler Map<?, List<?>> for the following test() method? public static void main(String[] args) { Map<Integer, List<String>> mappy = new HashMap<Integer, List<String>>(); test(mappy); } public static void test(Map<?, ? extends List<?>> m) {} // Doesn't compile // public static void test(Map<?, List<?>> m) {} Noting that the following works, and that the three methods have the same erased type anyways. public static <E> void test(Map<?, List<E>> m) {} Radiodef Fundamentally, List<List<?>> and List<? extends List<?>>