bounded-wildcard

Why Wild Cards can't be used in generic class & method declaration?

一个人想着一个人 提交于 2019-12-13 04:41:48
问题 Declaration like this : class A<X extends Number & List> { } is allowed.Whereas declaration like this is not allowed. class A<? extends Number & List> { } Is there any logical explanation about why Java restricts us to do that? & what's the actual difference between <T extends Number> & <? extends Number>? 回答1: The whole point of a type parameter like T is so that you can use it as a type inside the class. What would a wildcard there even mean? If you can't use it anywhere, why have a type

Generic upper bounded wildcard instantiation known at run time

烂漫一生 提交于 2019-12-11 20:22:11
问题 class Aliphatic<F> extends Organic<F>{} class Hexane<G> extends Aliphatic<G>{} public class Organic<E>{ void react(E e){} static void main(String[] args){ Organic<? extends Organic> compound = new Aliphatic<Organic>(); compound.react(new Organic()); } } Why can't I call react method with Organic argument? The generic type ot the reference <? extends Organic> says that the generic type of the instantiation canb either a Organic, or a subtype of Organic. Is because the compiler doesn't know

How to say “A Map of Class<?> to List<The class>” in Java?

左心房为你撑大大i 提交于 2019-12-11 03:47:59
问题 Say I have a HashMap<?, List<?>> map = new HashMap<>(); map.put(String.class, new ArrayList<Long>()); The following code will compile. However, I want to fail compilation because the ArrayList is not for String type. Also, my wildcard is limited to some specific interface (e.g. Exception), so I suppose I should put that <? extends Exception> somewhere. How can I achieve the above? Example Test: map.put(String.class, new ArrayList<String>()); //Fail because String is not an Exception map.put

How do I use Java generic wildcards with methods taking more than one generic parameter?

二次信任 提交于 2019-12-11 01:19:18
问题 So we have a generic method like this, which is part of dependency injection initialisation: public static <TS, TI extends TS> void registerTransient( Class<TS> serviceClass, Class<TI> implementationClass) { // } At some point we found a case where a class might not necessarily be present. And it's an implementation class which we would be injecting multiple off (so the service class is the same as the implementation class.) Naturally you would write this like this: Class<?> clazz = Class

Java 'reduceLeft' signature / Lower-bounded Type Arguments

僤鯓⒐⒋嵵緔 提交于 2019-12-10 13:43:30
问题 The following signature is valid and commonly used in Scala: trait Collection[A] { def reduceLeft [B >: A] (f: (B, A) => B): B } However, since >: is the Scala equivalent of super in Java, my first idea to convert this signature (replacing the function type with BiFunction and making use of Use-Site variance annotations aka Bounded Wildcards) would be interface Collection<A> { <B super A> B reduceLeft(BiFunction<? super B, ? super A, ? extends B> mapper) } But oh no ! The compiler complains

Wild card in java Generic and <? super T> meaning, lower or upper bound

邮差的信 提交于 2019-12-10 13:27:23
问题 So I am reading about generic method and I am get confused. Let me state the problem here first: In this example: Suppose that I need a version of selectionSort that works for any type T, by using an external comparable supplied by the caller. First attempt: public static <T> void selectionSort(T[] arr, Comparator<T> myComparator){....} Suppose that I have: Defined vehicle class created VehicleComparator implementing Comparator while compare vehicles by their price. created Truck extends

Raw types, unbounded wilcard and bounded wildcard

泄露秘密 提交于 2019-12-10 10:29:33
问题 I have a quick question as below: Here's a simple examples about this whole issues: List a = new ArrayList(); List <?> b; List <? extends Object> c; According to Java SCJP by khalid mughal (a very good book!): a = b; // ok. Widening conversion. b = a; // ok too. No unchecked warning. b = c; // ok c = b; // ok c=a; // ok but now will issue a unchecked warning. // clause 1 I do understand that any raw types (example a) when assigned to any bounded wilcard references, a unchecked warning is

A bad interaction between self-referential types and bounded wildcards

帅比萌擦擦* 提交于 2019-12-10 10:12:45
问题 This case seems to be another one where Eclipse's Java compiler crushes javac. The only question for me is whether it's a bug in JLS or javac. interface EndoFunctor< C, FC extends EndoFunctor< C, FC > > { /*...*/ } interface Algebra< C, FC extends EndoFunctor< ? extends C, FC > > { /*...*/ } The second line compiles in Eclipse, but fails to compile in javac with the message that "type parameter FC is not within its bound". FC is declared to extend EndoFunctor< ? extends C, FC >, and the bound

In Java, how can I avoid raw types when calling getClass on an instance of a generic type?

南楼画角 提交于 2019-12-09 16:23:39
问题 Suppose I have this in Java: List<String> list = new ArrayList<String>(); list.getClass(); The type of the last expression is Class<? extends List> . I understand why, due to erasure, it cannot be Class<? extends List<String>> . But why can't it be Class<? extends List<?>> ? Is there no way for me to avoid both unchecked cast warnings and raw type warnings if I want to assign the result of this expression to a variable that somehow keeps the information that this class is actually some kind

Is there a need to use bounded wildcard generics in a passthrough method?

[亡魂溺海] 提交于 2019-12-08 08:39:24
问题 I know that in the following method in Collection<E> : public void addAll(Collection<? extends E> subcollection); We use Collection<? super E> there to allow a collection that solely exists of sub-elements, example: List<Drivable> drivables = new ArrayList<>(); List<Car> cars = new ArrayList<>(); //we need the wildcard here, because of the following line: drivables.addAll(cars); However, are such bounded wildcards needed in my following method? public static <E> Collection<E> requireNonEmpty