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
9
votes
2 answers

difference between creation unbounded and bounded wild card type array?

Why is this code valid ArrayList[] arr = new ArrayList[2]; but the following two are not? ArrayList[] arr = new ArrayList[2]; ArrayList[] arr = new ArrayList[2]; The two…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
9
votes
2 answers

java.util.Comparator.naturalOrder takes a > and returns a Comparator - why?

(If this is a duplicate please point me to the right answer! I searched and read several (>5) related questions but none seemed on the mark. Also looked at the Generics FAQ and other sources...) It is apparently proper practice that when a…
davidbak
  • 5,775
  • 3
  • 34
  • 50
9
votes
2 answers

Difference of assignability with nested wildcards in Java 7/8 generics

The following compiles just fine in JDK8, but gives an incompatible types error with JDK7. List> xs = Arrays.asList(Arrays.asList(0)); According to this answer, List> doesn't have a supertype…
DaoWen
  • 32,589
  • 6
  • 74
  • 101
9
votes
4 answers

Recursive type parameters for an almost-cyclic type bound

I have the following two interfaces: /** * A marker interface to denote that an object implements a view on some other object. * * @param The type of object that is viewed */ public interface View>> { } /** *…
skiwi
  • 66,971
  • 31
  • 131
  • 216
9
votes
2 answers

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

Suppose I have this in Java: List list = new ArrayList(); list.getClass(); The type of the last expression is Class. I understand why, due to erasure, it cannot be Class>. But why can't it be…
Jean-Philippe Pellet
  • 59,296
  • 21
  • 173
  • 234
9
votes
1 answer

Wildcard with final upper bound

Class will compile fine, but Integer is a final type so it doesn't make sense to use it as an upper bound (nothing will ever extend it). If you try to use a final type as an upper bound for a type parameter, you will get a …
Jeffrey
  • 44,417
  • 8
  • 90
  • 141
8
votes
2 answers

Why does java require a cast for the instantiation of a bounded type parameter to its upper bound class?

Java requires the instantiation of a bounded type parameter to its upper bound class to have a cast, for example: void passVal (T t) { Integer number = 5; t = (T) number; // Without cast a compile error is issued } T is…
Matthew S.
  • 464
  • 2
  • 12
8
votes
3 answers

Using generic wildcard types in return parameters

Using generic wildcard types in return parameters in Java is generally discouraged. For example Effective Java, Item 28 states: Do not use wildcard types as return types. Rather than providing additional flexibility for your users, it would force…
elk
  • 442
  • 3
  • 7
8
votes
2 answers

In guava, why is just "T" used where "? super T" would be possible?

Why do the utility factory methods often use a specific generic parameter (like T) instead of a bounded wildcard parameter (like ? super T)? For instance, the signature of Functions#forPredicate is: public static Function
matts
  • 6,738
  • 1
  • 33
  • 50
8
votes
4 answers

Multiple wildcard bounds

Suppose that I have the following class: public class Either { public Object get(); } Either is a type that stores one object of either type A or B. get() retrieves that one object. The question is whether or not it is possible to use…
Kelvin Chung
  • 1,327
  • 1
  • 11
  • 23
8
votes
6 answers

Java Generics: adding wrong type in collection

Who could me explain this? I have these couple of classes: abstract class Animal { public void eat() { System.out.println("Animal is eating"); } } class Dog extends Animal { public void woof() { …
Kyrylo Zapylaiev
  • 682
  • 1
  • 8
  • 15
7
votes
4 answers

Why is this assignment involving wildcards legal in Java?

Most questions about wildcards want to know why something sensible is rejected by the compiler. My question is the opposite. Why is the following program accepted by the compiler? void test(List g1, List g2) { …
Alan Snyder
  • 408
  • 2
  • 13
7
votes
2 answers

rule changing in bounded wildcards in java-8?

I was following a tutorial about generics in Java defining this static method: public static > T min(T a) { ... } and saying that min(new GregorianCalendar()); couldn't compile because GregorianCalendar extends Calendar and…
7
votes
3 answers

Java Generic Collection of Generic Type with Bounded Wildcard

Please help me with this: If Lion IS-A Animal and given Cage: Cage c = new Cage(); // ok, but Set> cc = new HashSet>(); // not ok What I don't see here?
ar_
  • 73
  • 2
7
votes
2 answers

Why can't assign I to ?

The following statements: URLClassLoader ucl = (URLClassLoader) ClassLoader.getSystemClassLoader(); Class uclc = ucl.getClass(); fail with error: Type mismatch: cannot convert from Class to…
ZioByte
  • 2,690
  • 1
  • 32
  • 68
1 2
3
16 17