bounded-wildcard

Lower bounded wildcard not checked against upper bounded type parameter

扶醉桌前 提交于 2019-12-04 01:43:30
I wonder why does this piece of code compile successfully? Source code: abstract class A<K extends Number> { public abstract <M> A<? super M> useMe(A<? super M> k); } Compiled successfully How does it work and why does this compile? M is any type, so why it can be used?. Should it be: <M extends Number> ? This will not compile: abstract class A<K extends Number> { public abstract <M> A<? super M> useMe(A<M> k); } Error message: type argument M is not within bounds of type variable K where M, K are type variables: M extends Object declared in method useMe(A) K extends Number declared in class A

.NET equivalent for Java bounded wildcard (IInterf<?>)?

社会主义新天地 提交于 2019-12-03 07:39:05
I'm stuck trying to translate some Java code that uses (bounded) wildcard generics to C#. My problem is, Java seems to allow a generic type to be both covariant and contravariant when used with a wildcard. [This is a spin-off from a previous question dealing with a simpler case of bounded-wildcards] Java - works: class Impl { } interface IGeneric1<T extends Impl> { void method1(IGeneric2<?> val); T method1WithParam(T val); } interface IGeneric2<T extends Impl> { void method2(IGeneric1<?> val); } abstract class Generic2<T extends Impl> implements IGeneric2<T> { // !! field using wildcard

Java Generics Puzzler, extending a class and using wildcards

限于喜欢 提交于 2019-12-03 02:57:19
问题 I've been beating my head against this one for awhile and thought that maybe some fresh eyes will see the issue; thanks for your time. import java.util.*; class Tbin<T> extends ArrayList<T> {} class TbinList<T> extends ArrayList<Tbin<T>> {} class Base {} class Derived extends Base {} public class Test { public static void main(String[] args) { ArrayList<Tbin<? extends Base>> test = new ArrayList<>(); test.add(new Tbin<Derived>()); TbinList<? extends Base> test2 = new TbinList<>(); test2.add

Java Generics Puzzler, extending a class and using wildcards

帅比萌擦擦* 提交于 2019-12-02 16:31:56
I've been beating my head against this one for awhile and thought that maybe some fresh eyes will see the issue; thanks for your time. import java.util.*; class Tbin<T> extends ArrayList<T> {} class TbinList<T> extends ArrayList<Tbin<T>> {} class Base {} class Derived extends Base {} public class Test { public static void main(String[] args) { ArrayList<Tbin<? extends Base>> test = new ArrayList<>(); test.add(new Tbin<Derived>()); TbinList<? extends Base> test2 = new TbinList<>(); test2.add(new Tbin<Derived>()); } } Using Java 8. It looks to me like the direct creation of the container in test

Mockito: Stubbing Methods That Return Type With Bounded Wild-Cards

给你一囗甜甜゛ 提交于 2019-12-02 14:15:56
Consider this code: public class DummyClass { public List<? extends Number> dummyMethod() { return new ArrayList<Integer>(); } } public class DummyClassTest { public void testMockitoWithGenerics() { DummyClass dummyClass = Mockito.mock(DummyClass.class); List<? extends Number> someList = new ArrayList<Integer>(); Mockito.when(dummyClass.dummyMethod()).thenReturn(someList); //Compiler complains about this } } The compiler complains about the line that's trying to stub the behavior for dummyMethod() . Any pointers on how one goes about stubbing methods that return a type with bounded wild-cards?

Generic lower unbound vs upper bounded wildcards

我只是一个虾纸丫 提交于 2019-12-01 23:09:45
问题 import java.util.List; import java.util.ArrayList; interface Canine {} class Dog implements Canine {} public class Collie extends Dog { public static void main(String[] args){ List<Dog> d = new ArrayList<Dog>(); List<Collie> c = new ArrayList<Collie>(); d.add(new Collie()); c.add(new Collie()); do1(d); do1(c); do2(d); do2(c); } static void do1(List<? extends Dog> d2){ d2.add(new Collie()); System.out.print(d2.size()); } static void do2(List<? super Collie> c2){ c2.add(new Collie()); System

Generic lower unbound vs upper bounded wildcards

末鹿安然 提交于 2019-12-01 22:39:44
import java.util.List; import java.util.ArrayList; interface Canine {} class Dog implements Canine {} public class Collie extends Dog { public static void main(String[] args){ List<Dog> d = new ArrayList<Dog>(); List<Collie> c = new ArrayList<Collie>(); d.add(new Collie()); c.add(new Collie()); do1(d); do1(c); do2(d); do2(c); } static void do1(List<? extends Dog> d2){ d2.add(new Collie()); System.out.print(d2.size()); } static void do2(List<? super Collie> c2){ c2.add(new Collie()); System.out.print(c2.size()); } } The answer for this question tell that when a method takes a wildcard generic

.NET equivalent for Java wildcard generics <?> with co- and contra- variance?

▼魔方 西西 提交于 2019-12-01 07:35:21
I'm stuck trying to translate some Java code that uses (bounded) wildcard generics to C#. My problem is, Java seems to allow a generic type to be both covariant and contravariant when used with a wildcard. For instance: Java: interface IInterf { } class Impl implements IInterf { } interface IGeneric1<T extends Impl> { void method1(IGeneric2<?> val); void method1WithParam(T val); } interface IGeneric2<T extends Impl> { void method2(IGeneric1<?> val); } abstract class Generic<T extends Impl> implements IGeneric1<T>, IGeneric2<T> { public void method1(IGeneric2<?> val2) { val2.method2(this); } }

Java 8 Comparator comparing static function

岁酱吖の 提交于 2019-12-01 04:57:32
For the comparing source code in Comparator class public static <T, U extends Comparable<? super U>> Comparator<T> comparing( Function<? super T, ? extends U> keyExtractor) { Objects.requireNonNull(keyExtractor); return (Comparator<T> & Serializable) (c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2)); } I understand the difference between super and extends . What i dont understand is that why this method have them. Can someone give me an example on what cannot be achieved when the parameter look like this Function<T, U> keyExtractor ? For example : Comparator<Employee>

Generics in Java, using wildcards

南笙酒味 提交于 2019-11-30 21:53:39
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(ec); System.out.println(c.getVar()); // OUTPUT: ExampleClass@addbf1 } } Now, if I use a wildcard and