Questions tagged [bounded-wildcard]

Bounded wildcard is a type argument of the form "? extends T" or "? super T". Bounded wildcards are a feature of generics in the Java language. These type arguments represent some unknown type, with either an upper or lower bound.

A bounded wildcard is a type argument of the form ? extends T or ? super T. Bounded wildcards are a feature of generics in the language. These type arguments represent some unknown type, with either an upper or lower bound.

  • A bounded wildcard with an upper bound ? extends T requires that the original type was T or a subtype of T:

    static <T> void fill(
            Collection<T> collection,
            int count,
            Supplier<? extends T> supplier) {
    
        for (int i = 0; i < count; ++i)
            collection.add( supplier.get() );
    }
    
    List<Number> list = new ArrayList<>();
    Supplier<Double> random = Math::random;
    
    fill( list, 10, random );
    
  • A bounded wildcard with a lower bound ? super T requires that the original type was T or a supertype of T:

    static <T> void forEach(
            Iterable<T> iterable,
            Consumer<? super T> consumer) {
    
        for (T element : iterable)
            consumer.accept( element );
    }
    
    List<Integer> list = Arrays.asList(1, 2, 3);
    Consumer<Object> printer = System.out::println;
    
    forEach( list, printer );
    

The bounded wildcard ? extends Object is equivalent to the .

See also:

243 questions
7
votes
3 answers

generics error: not applicable for the arguments

Can someone explain to me why the following code does not work? public class Test { interface Strategy { void execute(T t); } public static class DefaultStrategy implements Strategy { @Override public void execute(T t) {} } …
user191204
6
votes
3 answers

Java Generics: What is the benefit of using wildcards here?

The Collections.fill method has the following header: public static void fill(List list, T obj) Why is the wildcard necessary? The following header seems to work just as well: public static void fill(List list, T obj) I…
6
votes
2 answers

Can explicit type parameters redundant?

I have a class with a type parameter. class MyObject { @Setter @Getter private IdType id; } And I thought I can add some method for conveniency so I did. > void copyIdTo(T object) { …
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
6
votes
3 answers

Wildcard and type pameter bounds in java

Consider this case: class A {} class B { B b; B 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…
Sergey94
  • 78
  • 7
6
votes
3 answers

Java 'reduceLeft' signature / Lower-bounded Type Arguments

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…
Clashsoft
  • 11,553
  • 5
  • 40
  • 79
6
votes
2 answers

java.lang.Class generics and wildcards

Why is is that the following code does not compile? interface Iface { } class Impl implements Iface { } class TestCase { static Class> clazz = Impl.class; } The error is java: incompatible types:…
Tavian Barnes
  • 12,477
  • 4
  • 45
  • 118
6
votes
2 answers

Java type inference with lower bounded types

Why is it that Java can infer the common ancestor of multiple upper-bounded types, but not of lower-bounded types? More specifically, consider the following examples: static class Test { static T pick(T one, T two) { return two; …
shmosel
  • 49,289
  • 6
  • 73
  • 138
6
votes
4 answers

Wild card in java Generic and meaning, lower or upper bound

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…
Victoria J.
  • 323
  • 2
  • 12
6
votes
2 answers

.NET equivalent for Java bounded wildcard (IInterf)?

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…
Cristian Diaconescu
  • 34,633
  • 32
  • 143
  • 233
6
votes
1 answer

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

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 {…
Cristian Diaconescu
  • 34,633
  • 32
  • 143
  • 233
6
votes
2 answers

Cannot Instantiate Type in generics

I have this class public class Tree { //List of branches for this tree private List> branch = new ArrayList>(); public Tree(T t){ this.t = t; } public void addBranch(Tree< ? super T> src){…
KyelJmD
  • 4,682
  • 9
  • 54
  • 77
5
votes
3 answers

Lower bound generic in java does not compile even when passing super class

List means the list can contain IOException objects or any object that is super class of IOException. Then why the below Line 2 does not compile? Also in Line 3 FileNotFoundException is not a super class of IOException. Then…
Sumit
  • 856
  • 5
  • 18
  • 38
5
votes
1 answer

What method declaration accepts bounded and unbounded multi-level Generics?

Given: public class Testcase { public static List> transform(List> list) { return list; } public static List> transform2(List> list) { return list; } …
Gili
  • 86,244
  • 97
  • 390
  • 689
5
votes
0 answers

Strange behavior of wildcards in method call

I was going through wildcard topic in Java where i got stuck at this code below static void type(List list){ //... } static void type2(List list){ //... } If you call these methods by this code,…
abhi_awake
  • 186
  • 1
  • 8
5
votes
2 answers

Java 8 generic collections with optionals

I have a relatively simple looking problem that I am trying to solve. There doesn't seem to be an intuitive way to do this or, I am missing something here. Consider this method to find the main image and if none exists, return first image- public…
sanz
  • 1,069
  • 1
  • 10
  • 26