bounded-wildcard

Difference of assignability with nested wildcards in Java 7/8 generics

大憨熊 提交于 2019-11-29 04:19:42
The following compiles just fine in JDK8, but gives an incompatible types error with JDK7. List<List<? extends Number>> xs = Arrays.asList(Arrays.asList(0)); According to this answer , List<List<? extends Number>> doesn't have a supertype relationship to List<List<Integer>> . What changed in Java 8 that made this assignment work? I'm also having a hard time understanding why it doesn't work in Java 7. Both of these statements compile without type error using JDK7: List<? extends Number> xs = Arrays.asList(0); List<? extends List<? extends Number>> ys = Arrays.asList(Arrays.asList(0)); It seems

Why doesn't the ternary operator like generic types with bounded wildcards?

不羁岁月 提交于 2019-11-29 02:37:48
问题 The following class defines two methods, both of which intuitively have the same functionality. Each function is called with two lists of type List<? super Integer> and a boolean value which specifies which of those lists should be assigned to a local variable. import java.util.List; class Example { void chooseList1(boolean choice, List<? super Integer> list1, List<? super Integer> list2) { List<? super Integer> list; if (choice) list = list1; else list = list2; } void chooseList2(boolean

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

只谈情不闲聊 提交于 2019-11-28 13:36:34
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 Quirk or if there's something I really dont understand in generics. Covariance vs contravariance vs

Inferred wildcard generics in return type

試著忘記壹切 提交于 2019-11-28 09:06:06
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, which requires wildcards: Pair<Class<?>, String> pair = Pair.of((Class<?>) List.class, "hello");

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

人盡茶涼 提交于 2019-11-28 05:48:14
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 then when the actual final design came out in Java, this connection got lost a little bit This is Martin

Difference between Bounded Type parameter (T extends) and Upper Bound Wildcard (? extends)

旧街凉风 提交于 2019-11-28 02:42:44
问题 I know that there was a similar question already posted, although I think mine is somewhat different... Suppose you have two methods: // Bounded type parameter private static <T extends Number> void processList(List<T> someList) { } // Upper bound wildcard private static void processList2(List<? extends Number> someList) { // ... } As far as I know, both methods accepts arguments, that are List of type Number or List of subtype of Number . But what's the difference between the two methods

Adding an element inside a wildcard type ArrayList

纵然是瞬间 提交于 2019-11-28 02:18:32
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 ? extends Question) in the type ArrayList<capture#2-of ? extends Question> is not applicable for the

java.lang.Class generics and wildcards

谁说胖子不能爱 提交于 2019-11-28 00:47:04
问题 Why is is that the following code does not compile? interface Iface<T> { } class Impl<T> implements Iface<T> { } class TestCase { static Class<? extends Iface<?>> clazz = Impl.class; } The error is java: incompatible types: java.lang.Class<Impl> cannot be converted to java.lang.Class<? extends Iface<?>> but I don't see why the wildcard doesn't capture. 回答1: The subtyping relationship here is: Class<? extends Iface> ╱ ╲ Class<? extends Iface<?>> Class<Impl> (Which I explained in my answer to

Java generic methods: super can't be used?

南楼画角 提交于 2019-11-27 23:04:08
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; } with the intent of doing something like this (where MyItemClass extends MyItemBaseClass ): List

Unbounded wildcards in Java

自闭症网瘾萝莉.ら 提交于 2019-11-27 12:26:40
Is there ever a difference between an unbounded wildcard e.g. <?> and a bounded wildcard whose bound is Object , e.g. <? extends Object> ? I recall reading somewhere that there was a difference in the early drafts of generics, but cannot find that source anymore. As a point of pedntry, there is a difference if the class/interface/constructor/method declares a bound (other than extends Object ). interface Donkey<T extends Thing> { } ... Donkey<? extends Object> foo; // FAIL notnoop From a practical point to most people, <? extends Object> is the same as <?> , like everyone have suggested here.