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
895
votes
16 answers

What is PECS (Producer Extends Consumer Super)?

I came across PECS (short for Producer extends and Consumer super) while reading up on generics. Can someone explain to me how to use PECS to resolve confusion between extends and super?
peakit
  • 28,597
  • 27
  • 63
  • 80
435
votes
2 answers

Java Generics With a Class & an Interface - Together

I want to have a Class object, but I want to force whatever class it represents to extend class A and implement interface B. I can do: Class Or: Class but I can't do both. Is there a way to do this?
Alex Beardsley
  • 20,988
  • 15
  • 52
  • 67
159
votes
5 answers

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

Consider this code: public class DummyClass { public List dummyMethod() { return new ArrayList(); } } public class DummyClassTest { public void testMockitoWithGenerics() { DummyClass dummyClass…
Shikhar Mishra
  • 1,965
  • 2
  • 13
  • 14
126
votes
7 answers

Java Generics (Wildcards)

I have a couple of questions about generic wildcards in Java: What is the difference between List and List? What is a bounded wildcard and what is an unbounded wildcard?
Pablo Fernandez
  • 103,170
  • 56
  • 192
  • 232
98
votes
5 answers

Java: bounded wildcards or bounded type parameter?

Recently, I read this article: http://download.oracle.com/javase/tutorial/extra/generics/wildcards.html My question is, instead of creating a method like this: public void drawAll(List shapes){ for (Shape s: shapes) { …
Tony Le
  • 983
  • 1
  • 7
  • 4
85
votes
5 answers

Why can't you have multiple interfaces in a bounded wildcard generic?

I know there's all sorts of counter-intuitive properties of Java's generic types. Here's one in particular that I don't understand, and which I'm hoping someone can explain to me. When specifying a type parameter for a class or interface, you can…
Adrian Petrescu
  • 16,629
  • 6
  • 56
  • 82
63
votes
11 answers

What does List mean in java generics?

What does List mean, does it mean simply a list of objects of unspecified type? Googling for the string returns nothing useful (:
corydoras
  • 7,130
  • 12
  • 55
  • 58
51
votes
6 answers

Java Generics Puzzler, extending a class and using wildcards

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 extends ArrayList {} class TbinList extends ArrayList> {} class…
user1677663
  • 1,431
  • 15
  • 21
42
votes
5 answers

List

I have a Java question about generics. I declared a generic list: List listOfMyType; Then in some method I try instantiate and add items to that list: listOfMyType = new ArrayList(); listOfMyType.add(myTypeInstance);…
Lancelot
  • 2,417
  • 12
  • 39
  • 46
37
votes
2 answers

What is the difference between bounded wildcard and type parameters?

Is there a difference between Collection getThatCollection(Class type) and Collection getThatCollection(Class)
rrejex
  • 371
  • 3
  • 3
31
votes
5 answers

Difference between Scala's existential types and Java's wildcard by example?

A bit more specific than Stack Overflow question What is an existential type?, what is the difference between Scala's existential types and Java's wildcard, prefereably with some illustrative example? In everything I've seen so far, they seem to be…
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
29
votes
2 answers

Java nested generic type

How come one must use the generic type Map> instead of a simpler Map> for the following test() method? public static void main(String[] args) { Map> mappy = new HashMap
Louis Bliss
  • 393
  • 1
  • 3
  • 7
22
votes
2 answers

Why doesn't the ternary operator like generic types with bounded wildcards?

The following class defines two methods, both of which intuitively have the same functionality. Each function is called with two lists of type List and a boolean value which specifies which of those lists should be assigned to a…
Feuermurmel
  • 9,490
  • 10
  • 60
  • 90
21
votes
5 answers

Difference between Bounded Type parameter (T extends) and Upper Bound Wildcard (? extends)

I know that there was a similar question already posted, although I think mine is somewhat different... Suppose you have two methods: // Bounded type parameter private static void processList(List someList) { } // Upper bound…
ThomasMX
  • 1,643
  • 2
  • 19
  • 35
20
votes
3 answers

In Java, what can a wild card do that regular generics cannot do?

I am new to Java. In this document they give this as a use case for using wildcard: static void printCollection(Collection c) { Iterator i = c.iterator(); for (int k = 0; k < c.size(); k++) { System.out.println(i.next()); …
developer747
  • 15,419
  • 26
  • 93
  • 147
1
2 3
16 17