unbounded-wildcard

AssertJ `containsExactly` assertion on list with wildcard

给你一囗甜甜゛ 提交于 2020-05-15 10:17:05
问题 I have a getter returning a List with a wildcard: import java.util.List; public interface Foo { List<? extends Bar> getList(); } Where Bar is an other interface. When I write an assertion with AssertJ like this: assertThat(foo.getList()).containsExactly(bar1, bar3); EDIT: my complete usage is to chain a usingElementComparator and to provide a Comparator<Bar> to compare the expected Bar instances. Comparator<Bar> comparator = createBarComparator() assertThat(foo.getList())

Wildcard and type pameter bounds in java

一个人想着一个人 提交于 2020-01-03 10:59:12
问题 Consider this case: class A {} class B<T extends A, E extends T> { B<?, A> b; B<?, ? extends A> b2; } As I understand type bounds, in this case effective upper bounds of both T and E is class A . So the question: why javac doesn't accept class A as argument in declaration of field b , but accepts wildcard ? extends A in declaration of field b2 ? 回答1: With the following classes: class A {} class C extends A {} class B<T extends A, E extends T> {} Think of it like this: E extends T extends A

difference between List and List<?> [duplicate]

主宰稳场 提交于 2019-12-22 18:27:44
问题 This question already has answers here : What's the difference between unbounded wildcard type List<?> and raw type List? (4 answers) Closed 10 months ago . I've read alot about this, and I know that: List<Object> listOfObject = new ArrayList<TYPE>(); // (0) //can only work for TYPE == Object. //if TYPE extends Object (and thus objects of type TYPE are Objects), //this is not the same with Lists: List<Type> is not a List<Object> Now I've read that the following is ok: List undefinedList = new

Cannot convert from List<List> to List<List<?>>

寵の児 提交于 2019-12-17 06:14:10
问题 A raw list converts to List<?> just fine. Why can't a list of raw lists convert to a list of List<?> ? { // works List raw = null; List<?> wild = raw; } { // Type mismatch: cannot convert from List<List> to List<List<?>> List<List> raw = null; List<List<?>> wild = raw; } Backstory (to mitigate the xy problem): An API I'm using returns List<JAXBElement> . I happen to know that it is always List<JAXBElement<String>> . I plan to loop and build my own List<String> , but I was trying to fix (but

Incompatible wildcard types that should be compatible [duplicate]

寵の児 提交于 2019-12-10 19:49:03
问题 This question already has an answer here : Bounded-wildcard related compiler error (1 answer) Closed 5 years ago . Following on from this question, which provides a solution but doesn't explain it (unfortunately, the links in the answers are now dead): Take the following method: void method(Map<?, ?> myMap) { Set<Map.Entry<?, ?>> set = myMap.entrySet(); ... } Simple, no? However, this fails to compile on jdk1.7.0_25: incompatible types required: java.util.Set<java.util.Map.Entry<?,?>> found:

Java language specification on wildcards

流过昼夜 提交于 2019-12-10 19:31:22
问题 I am going through this link (Chapter 4. Types, Values, and Variables) and did not understand below point: The relationship of wildcards to established type theory is an interesting one, which we briefly allude to here. Wildcards are a restricted form of existential types. Given a generic type declaration G<T extends B>, G<?> is roughly analogous to Some X <: B. G<X> . I appreciate if you provide good example to understand above point clearly. Thanks in advance. 回答1: The wording and

Java - Is new Stack<?>[N] equivalent to new Stack[N] for generic data type Stack<Item>?

柔情痞子 提交于 2019-12-10 08:57:51
问题 Is new Stack<?>[N] equivalent to new Stack[N] for a generic data type Stack<Item> ? EDIT: While I understand that mixing generic types and arrays should best be avoided and that more robust solutions exist, my query still stands: widely recognized textbooks such as Algorithms, 4th Edition by Kevin Wayne and Robert Sedgewick (pg. 158) suggest using constructs like the following: Stack<String>[] a = (Stack<String>[]) new Stack[N]; 回答1: Yes, they are equivalent, except for the return type (one

Java - Is new Stack<?>[N] equivalent to new Stack[N] for generic data type Stack<Item>?

 ̄綄美尐妖づ 提交于 2019-12-05 19:04:26
Is new Stack<?>[N] equivalent to new Stack[N] for a generic data type Stack<Item> ? EDIT: While I understand that mixing generic types and arrays should best be avoided and that more robust solutions exist, my query still stands: widely recognized textbooks such as Algorithms, 4th Edition by Kevin Wayne and Robert Sedgewick (pg. 158) suggest using constructs like the following: Stack<String>[] a = (Stack<String>[]) new Stack[N]; Yes, they are equivalent, except for the return type (one is Stack[] , the other is Stack<?>[] ). Note that Stack[] can be assigned to Stack<String>[] without an

Generics Java, unbounded wildcard

落爺英雄遲暮 提交于 2019-12-04 04:33:52
问题 Hi directly from a java tutorial provided by Oracle http://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html static void filter(Collection<?> c) { for (Iterator<?> it = c.iterator(); it.hasNext(); ) if (!cond(it.next())) it.remove(); } I am aware of the type erasure at compilation time. And I am aware also of that a type (unbounded) is going to be substituted with Object. Being aware of that what is going to do the compiler with the unbounded wild card at compilation time

List<List<?>> and List<List> are incompatible types in java [duplicate]

☆樱花仙子☆ 提交于 2019-12-01 03:31:56
This question already has an answer here: Cannot convert from List<List> to List<List<?>> 3 answers I did not get this code to compile either way: List<List> a = new ArrayList(); List<List<?>> b = new ArrayList(); a = b; // incompatible types b = a; // incompatible types It seems that java does not consider List and List<?> to be the same type when it comes to generics. Why is that? And is there some nice way out? Context There is a library function with following signature: public <T> Set<Class<? extends T>> getSubTypesOf(final Class<T> type) . This works fine for simple types passed as