bounded-wildcard

List<? extends MyType>

非 Y 不嫁゛ 提交于 2019-11-26 20:33:57
I have a Java question about generics. I declared a generic list: List<? extends MyType> listOfMyType; Then in some method I try instantiate and add items to that list: listOfMyType = new ArrayList<MyType>(); listOfMyType.add(myTypeInstance); Where myTypeInstance is just an object of type MyType ; it won't compile. It says: The method add(capture#3-of ? extends MyType) in the type List<capture#3-of ? extends MyType> is not applicable for the arguments (MyType) Any idea? Surya You cannot do a "put" with extends . Look at Generics - Get and Put rule . Consider: class MySubType extends MyType { }

What is the difference between bounded wildcard and type parameters?

孤街浪徒 提交于 2019-11-26 20:01:47
Is there a difference between <N extends Number> Collection<N> getThatCollection(Class<N> type) and Collection<? extends Number> getThatCollection(Class<? extends Number>) notnoop They expose different interfaces and contract for the method. The first declaration should return a collection whose elements type is the same of the argument class. The compiler infers the type of N (if not specified). So the following two statements are valid when using the first declaration: Collection<Integer> c1 = getThatCollection(Integer.class); Collection<Double> c2 = getThatCollection(Double.class); The

Why can't you have multiple interfaces in a bounded wildcard generic?

ぃ、小莉子 提交于 2019-11-26 19:44:42
I know there's all sorts of counter-intuitive properties of Java's generic types. Here's one in particular that I don't understand, and which I'm hoping someone can explain to me. When specifying a type parameter for a class or interface, you can bound it so that it must implement multiple interfaces with public class Foo<T extends InterfaceA & InterfaceB> . However, if you're instantiating an actual object, this doesn't work anymore. List<? extends InterfaceA> is fine, but List<? extends InterfaceA & InterfaceB> fails to compile. Consider the following complete snippet: import java.util.List;

generics error: not applicable for the arguments

匆匆过客 提交于 2019-11-26 14:52:51
问题 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

Java: bounded wildcards or bounded type parameter?

[亡魂溺海] 提交于 2019-11-26 10:09:42
问题 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? 回答1: It depends on what you need to do

What does List<?> mean in java generics?

∥☆過路亽.° 提交于 2019-11-26 09:36:14
问题 What does List<?> mean, does it mean simply a list of objects of unspecified type? Googling for the string <?> returns nothing useful (: 回答1: The keyword you need to get more information is Wildcards 回答2: As Tom said, the ? , or unbounded wildcard, means that the type of the object is not specified. It could be unknown, could be meant for multiple possible values or might be just plain irrelevant. Your example, List<?> , is pronounced "List of unknown." It's convenient because it's flexible,

Creating new generic object with wildcard

夙愿已清 提交于 2019-11-26 09:03:30
问题 Please explain this generic code wildcard compile time error: //no compile time error. List<? extends Number> x = new ArrayList<>(); //compile time error. List<? extends Number> x = new ArrayList<? extends Number>(); 回答1: It's invalid syntax to instantiate a generic type with wildcards. The type List<? extends Number> means a List of some type that is or extends Number . To create an instance of this type doesn't make sense, because with instantiation you're creating something specific: new

Java nested generic type

大憨熊 提交于 2019-11-26 08:06:26
问题 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

Java Generics Wildcarding With Multiple Classes

旧街凉风 提交于 2019-11-26 00:12:59
问题 I want to have a Class object, but I want to force whatever class it represents to extend class A and implement interface B. I can do: Class<? extends ClassA> Or: Class<? extends InterfaceB> but I can\'t do both. Is there a way to do this? 回答1: Actually, you can do what you want. If you want to provide multiple interfaces or a class plus interfaces, you have to have your wildcard look something like this: <T extends ClassA & InterfaceB> See the Generics Tutorial at sun.com, specifically the

What is PECS (Producer Extends Consumer Super)?

旧城冷巷雨未停 提交于 2019-11-25 23:55:32
问题 I came across PECS (short for Producer extends and Consumer super ) while reading up on generics. Can someone explain to me how to use PECS to resolve confusion between extends and super ? 回答1: tl;dr: "PECS" is from the collection's point of view. If you are only pulling items from a generic collection, it is a producer and you should use extends ; if you are only stuffing items in, it is a consumer and you should use super . If you do both with the same collection, you shouldn't use either