bounded-wildcard

Generics in Java, using wildcards

我们两清 提交于 2019-11-30 17:51:25
问题 I have a question about Generics in Java, namely using wildcards. I have an example class GenClass like this: public class GenClass<E> { private E var; public void setVar(E x) { var = x; } public E getVar() { return var; } } I have another simple class: public class ExampleClass { } I have written the following test class: public class TestGenClass { public static void main(String[] str) { ExampleClass ec = new ExampleClass(); GenClass<ExampleClass> c = new GenClass<ExampleClass>(); c.setVar

Java unbound wildcard generics

馋奶兔 提交于 2019-11-30 10:28:55
Are there any advantages of using wildcard-type generics in the Bar class over completely skipping them? public class Foo<T> {} public interface Bar { public void addFoo(Foo<?> foo); public Foo<?> getFoo(String name); } There are multiple advantages. They won't produce compiler warnings like using the raw type would They give more type safety. For example, consider if Foo was List instead. If you used List instead of List<?> , you could do this: myBar.getFoo("numbers").add("some string"); even if the list was only supposed to contain Number s. If you returned a List<?> , then you would not be

Nested Bounded Wildcard

谁都会走 提交于 2019-11-30 09:21:54
When I try to compile the following code: LinkedList<List<? extends Number>> numList = new LinkedList<List<Integer>>(); I get an incompatible type error: Required: LinkedList <java.util.list<? extends java.lang.Number>> Found: LinkedList <java.util.list<Integer>> How can I achieve having a LinkedList which contains elements that are List s with elements that extend Number ? To be clear, I'm looking to add lists to numList in the following fashion: numList.add(new LinkedList<Integer>()); Wildcard capture does not go more than one generic level deep. So while this works: LinkedList<? extends

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

可紊 提交于 2019-11-30 04:44:49
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 choice, List<? super Integer> list1, List<? super Integer> list2) { List<? super Integer> list = choice ?

Why is passing a subclass to a bounded wildcard only allowed in certain places?

有些话、适合烂在心里 提交于 2019-11-29 22:56:50
问题 This following is from generics tutorials: Say class R extends S, public void addR(List<? extends S> s) { s.add(0, new R()); // Compile-time error! } You should be able to figure out why the code above is disallowed. The type of the second parameter to s.add() is ? extends S -- an unknown subtype of S. Since we don't know what type it is, we don't know if it is a supertype of R; it might or might not be such a supertype, so it isn't safe to pass a R there. I have read it a few times but still

Java unbound wildcard generics

笑着哭i 提交于 2019-11-29 15:52:22
问题 Are there any advantages of using wildcard-type generics in the Bar class over completely skipping them? public class Foo<T> {} public interface Bar { public void addFoo(Foo<?> foo); public Foo<?> getFoo(String name); } 回答1: There are multiple advantages. They won't produce compiler warnings like using the raw type would They give more type safety. For example, consider if Foo was List instead. If you used List instead of List<?> , you could do this: myBar.getFoo("numbers").add("some string")

Nested Bounded Wildcard

隐身守侯 提交于 2019-11-29 13:46:19
问题 When I try to compile the following code: LinkedList<List<? extends Number>> numList = new LinkedList<List<Integer>>(); I get an incompatible type error: Required: LinkedList <java.util.list<? extends java.lang.Number>> Found: LinkedList <java.util.list<Integer>> How can I achieve having a LinkedList which contains elements that are List s with elements that extend Number ? To be clear, I'm looking to add lists to numList in the following fashion: numList.add(new LinkedList<Integer>()); 回答1:

Difference between Bounded Type parameter and Upper Bound Wildcard

微笑、不失礼 提交于 2019-11-29 09:18:45
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 after all? CKing There are several differences between the two syntaxes during compile time : With the

Java bounded wildcard in return type

给你一囗甜甜゛ 提交于 2019-11-29 09:15:15
I've read in various places including here that having a bounded wildcard in a method return type is a bad idea. However, I can't find a way to avoid it with my class. Am I missing something? The situation looks something like this: class EnglishReaderOfPublications { private final Publication<? extends English> publication; EnglishReaderOfPublications(Publication<? extends English> publication) { this.publication = publication; } void readPublication() { publication.omNomNom(); } Publication<? extends English> getPublication() { return publication; } } In summary, a class that I want to be

java.lang.Class generics and wildcards

断了今生、忘了曾经 提交于 2019-11-29 07:14:17
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. Radiodef The subtyping relationship here is: Class<? extends Iface> ╱ ╲ Class<? extends Iface<?>> Class<Impl> (Which I explained in my answer to 'Cannot convert from List<List> to List<List<?>> ' .) So essentially it doesn't compile because it's