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
19
votes
6 answers

Unbounded wildcards in Java

Is there ever a difference between an unbounded wildcard e.g. and a bounded wildcard whose bound is Object, e.g. ? I recall reading somewhere that there was a difference in the early drafts of generics, but cannot find that…
notnoop
  • 58,763
  • 21
  • 123
  • 144
18
votes
3 answers

Java bounded wildcard in return type

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…
thehouse
  • 7,957
  • 7
  • 33
  • 32
16
votes
4 answers

Why can't I use the wildcard (?) as type of parameter, field, local variable, or as return type of a method?

The Oracle doc about Wildcards in generics says, The wildcard can be used in a variety of situations: as the type of a parameter, field, or local variable; sometimes as a return type (though it is better programming practice to be more…
Solace
  • 8,612
  • 22
  • 95
  • 183
15
votes
1 answer

Inferred wildcard generics in return type

Java can often infer generics based on the arguments (and even on the return type, in contrast to e.g. C#). Case in point: I've got a generic class Pair which just stores a pair of values and can be used in the following way: Pair
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
14
votes
4 answers

Assigning to multilevel wildcards

Simple class: class Pair { } And a few assignments: Collection> c1 = new ArrayList>(); Collection> c2 = c1; // ok Collection> c3 = c1; // this does not compile Collection
Łukasz
  • 1,980
  • 6
  • 32
  • 52
13
votes
4 answers

Java Generics: Multiple Inheritance in Bounded Type Parameters

I am about to create a factory which creates objects of a certain type T which extends a certain class A and another interface I. However, T must not be known. Here are the minimum declarations: public class A { } public interface I { } This is the…
scravy
  • 11,904
  • 14
  • 72
  • 127
13
votes
2 answers

Creating new generic object with wildcard

Please explain this generic code wildcard compile time error: //no compile time error. List x = new ArrayList<>(); //compile time error. List x = new ArrayList();
Sam Adamsh
  • 3,331
  • 8
  • 32
  • 53
13
votes
3 answers

Java generic methods: super can't be used?

So I have this method: protected void collectSelectedItems(ListSelectionModel lsm, Collection result) { for (int i : GUI.getSelectionIndices(lsm)) { result.add(getItemByDisplayIndex(i)); } } I'd…
Jason S
  • 184,598
  • 164
  • 608
  • 970
13
votes
2 answers

Why does java.lang.Class's getInterfaces() method return Class[] and not Class[]?

(To clear up the question, 'T' refers to a type parameter declared in Class) Just as an example, please review the following application: public class TestClass { interface InterfaceA{} interface InterfaceB{} interface InterfaceC{} …
sager
  • 218
  • 3
  • 9
13
votes
2 answers

Use generic to store common supertype in Java

Suppose I have a method "mix" that takes two Lists of possibly different types T and S and returns a single List containing the elements of both. For type-safety, I'd like to specify that the returned List is of a type R, where R is a supertype…
Matt G
  • 1,661
  • 19
  • 32
11
votes
4 answers

Lower bounded wildcard not checked against upper bounded type parameter

I wonder why does this piece of code compile successfully? Source code: abstract class A { public abstract A useMe(A k); } Compiled successfully How does it work and why does this compile? M is any…
Pawel
  • 1,457
  • 1
  • 11
  • 22
10
votes
1 answer

Why is this generic assignment illegal?

I have a class: class Generic { List> getList() { return null; } } When I declare a Generic with wildcard and call getList method, the following assignment is illegal. Generic tt = null; List
haoyu wang
  • 1,241
  • 4
  • 17
10
votes
1 answer

Nested Bounded Wildcard

When I try to compile the following code: LinkedList> numList = new LinkedList>(); I get an incompatible type error: Required: LinkedList > Found: LinkedList…
vpiTriumph
  • 3,116
  • 2
  • 27
  • 39
9
votes
2 answers

Java 8 Comparator comparing static function

For the comparing source code in Comparator class public static > Comparator comparing( Function keyExtractor) { Objects.requireNonNull(keyExtractor); return (Comparator &…
Jiaming Li
  • 207
  • 1
  • 5
1
2
3
16 17