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
4
votes
4 answers

Why is the implicit cast of ArrayList> to Iterable> impossible?

I would like to know the reason why Java can't cast ArrayList> to Iterable> implicitly. My question is not about how to do it explicitly. Why does the following code: import java.util.Iterator; import…
lovasoa
  • 6,419
  • 1
  • 35
  • 45
4
votes
1 answer

Why does this method with wildcards work?

I came across this question: We're given a map interface: interface MyMap{ public void put (K key, V value); public V get (K key); public boolean containsKey (K key); } We want to implement a method addToMyMap whose signature is (we need…
matanc1
  • 6,525
  • 6
  • 37
  • 57
4
votes
1 answer

Bounded-wildcard related compiler error

I am wondering what is wrong with this code: Map m = null; Set> s = m.entrySet(); The compiler complains with the error message: Type mismatch: cannot convert…
JRR
  • 6,014
  • 6
  • 39
  • 59
4
votes
4 answers

Generic 0 cannot be cast to java.lang.Short

I have two maps in my class (I am new to generics) private Map aMap = new ConcurrentHashMap(); private Map bMap = new HashMap(); If key does not exist in map I want to get a zero…
Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101
4
votes
2 answers

Class in getSuperclass() Does it make sense?

Class getSuperclass() The getSuperclass() in Class class return a Class whose type is, which mean that the type parameter of Class of the Super could be T or any of its Superclasses, now how come a SuperClass's Class type…
skystar7
  • 4,419
  • 11
  • 38
  • 41
4
votes
4 answers

Covariant structure fails with capture-of error in Java

Consider the following Java class definitions: class Animal {} class Lion extends Animal {} When defining a covariant Cage for Animals I use this code in Java: class Cage { void add(T animal) { System.out.println("Adding…
soc
  • 27,983
  • 20
  • 111
  • 215
3
votes
1 answer

A bad interaction between self-referential types and bounded wildcards

This case seems to be another one where Eclipse's Java compiler crushes javac. The only question for me is whether it's a bug in JLS or javac. interface EndoFunctor< C, FC extends EndoFunctor< C, FC > > { /*...*/ } interface Algebra< C, FC extends…
Judge Mental
  • 5,209
  • 17
  • 22
3
votes
3 answers

generic methods and wildcards

What are the differences between the following three signatures? static void foo(List, Comparator); static void bar(List, Comparator ); static void baz(List, Comparator
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
3
votes
3 answers

Sharing a wildcard in Java generics

Suppose I have an interface interface Foo { void foo(T x); T bar() } and an object of this type with unknown parameter: Foo baz. Then I can call baz.foo(baz.bar()). However, now I need to put the value baz.bar() into a collection…
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
3
votes
6 answers

Generic: ArrayList of ? Extends ISomeInterface in Java

I'm having some trouble in the following code. public ArrayList getEventsByDateRange(DateTime minStartTime, DateTime minEndTime) { ArrayList returnedEvents = new ArrayList(); …
Mortalus
  • 10,574
  • 11
  • 67
  • 117
3
votes
3 answers

Can you restrict a type parameter to multiple specific classes

I am writing a generic class Bla with type parameter T. Can I restrict T, so that only classes I want to support can be used? public class Bla { private T foo; private Class fooClazz; } I want Bla to support most primitive classes…
SunFlow
  • 43
  • 5
3
votes
2 answers

How do I fix this Java generics wildcard error?

In this question, TofuBeer was having problems creating a genericized IterableEnumeration. The answer came from jcrossley3 pointing to this link http://www.javaspecialists.eu/archive/Issue107.html which pretty much solved the problem. There is…
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
3
votes
2 answers

Bounded wildcards in reference types

I have trouble understanding following code about two Predicate objects. The first one uses a lower bounded wildcard, the second a upper bounded. Predicate p1 = s -> s.startsWith("a"); // why can I call startsWith()? Predicate
Huntro
  • 322
  • 1
  • 3
  • 16
3
votes
2 answers

Type parameter bound not considered when generic type is used with unbounded wildcard

In my project, I have a constellation like this: trait F trait X[A <: F] def test(x: X[_]): X[_ <: F] = x Trait X has a type parameter with an upper bound of F. From my understanding, the types X[_] and X[_ <: F] should be equivalent. But scalac…
Feuermurmel
  • 9,490
  • 10
  • 60
  • 90
3
votes
2 answers

Java wildcards defaulted to java.lang.Object instead of upper bound

Given the code abstract class Base { public void addOnSomethingListener(Consumer action) {} public void foo() { System.out.println("foo"); } } class Simple
Wietlol
  • 1,001
  • 1
  • 10
  • 25