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

Is it possible to write a generic +1 method for numeric box types in Java?

This is NOT homework. Part 1 Is it possible to write a generic method, something like this: T plusOne(T num) { return num + 1; // DOESN'T COMPILE! How to fix??? } Short of using a bunch of instanceof and casts, is this…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
5
votes
1 answer

How do I resolve this wildcard capture issue when using java generics?

I am having issues using java generics - specifically, using wildcard capture. Here is a simplified version of the code I have that exhibits the problem I am seeing. It is driving me crazy: public class Task { private Action
Simon Perkins
  • 139
  • 1
  • 7
5
votes
1 answer

"Unexpected token" using lower-bounded wildcard (Java)

I have something along the lines of: interface Foo { //... lines [0,45]... /*line 46*/ List weave(R value); //... } But IntelliJ is reporting: Error:(46, 18) java: > expected Error:(46, 19) java: illegal start of…
Matt G
  • 1,661
  • 19
  • 32
5
votes
3 answers

Use of "super" with "?" in Java

I am trying to read and understand some Java code. Here it is: protected LoadTarget createTarget(PopulationLoadContext context) { return createTransactionalTargetGroup(RiskScoresTables.All_Tables); } What does the…
user1899082
5
votes
2 answers

Java Generics Curiosity

I have an interface A, which class B implements. The following generic method works public static List listFactory(Collection source) { return new ArrayList(source); } but public static List
Carl
  • 7,538
  • 1
  • 40
  • 64
4
votes
1 answer

Is it possible to write a single method that accepts a generic parameter of varying abstraction?

As a followup to this question, is it possible to write a single method that adds a Dog to a suitable room? (In this example, it would accept either an Animal room or a Dog room.) Or am I forced to write two distinct methods as below? (I can't…
Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246
4
votes
2 answers

Bounded Wildcards in Java

This is not fine List> a; List> b; a = b; This is fine List c; List d; c = d; How can make it compile first one?
Cemo
  • 5,370
  • 10
  • 50
  • 82
4
votes
1 answer

Java unbound wildcard generics

Are there any advantages of using wildcard-type generics in the Bar class over completely skipping them? public class Foo {} public interface Bar { public void addFoo(Foo foo); public Foo getFoo(String name); }
Johan Sjöberg
  • 47,929
  • 21
  • 130
  • 148
4
votes
1 answer

Applying functions of various types to a value

Let's say I have a method that applies multiple functions to a value. Example usage: String value = "a string with numb3r5"; Function> fn1 = ... Function, String> fn2 = ... Function> fn3 =…
Bart
  • 162
  • 1
  • 5
  • 12
4
votes
3 answers

Upper Bounded wildcards in Java

I have two generic methods that calculate the sum of elements of a List. The signatures of the methods are double method1(List list) - Here I am using a wildcard. double sumOfList1(List list) - Here there is…
ryandam
  • 684
  • 5
  • 13
4
votes
2 answers

Generics in Java, using wildcards

I have a question about Generics in Java, namely using wildcards. I have an example class GenClass like this: public class GenClass { private E var; public void setVar(E x) { var = x; } public E getVar() { …
user42155
  • 48,965
  • 27
  • 59
  • 60
4
votes
3 answers

Is there something wrong with nested ArrayList Initialization?

Consider these following classes: class A{ } class B extends A{ } As we know this compiles fine: List xx = new ArrayList(); List> xy = new ArrayList>(); But this gives compile time…
mark42inbound
  • 364
  • 1
  • 4
  • 19
4
votes
3 answers

When use Bounded type parameter or type Interface directly

In case you need to pass an argument of an interface type to a method you could use two impl. Use a bounded type parameter: public static void isTrue(boolean expression, I interfaceobj) { if(!expression){ …
Pau
  • 14,917
  • 14
  • 67
  • 94
4
votes
2 answers

How can I use generics with exclusive bound using "extends"?

Suppose I have below code snippet with JDK 1.7. Using Generics, I am getting confused how to achieve a generic parameterized type with exclusive, not inclusive bounds with using extends. For example: abstract class BaseAbstract { } class…
Vishal Zanzrukia
  • 4,902
  • 4
  • 38
  • 82
4
votes
1 answer

Upper-bound wildcard (extends) not working; ArrayList doesn't allow instances of subtype

From what I know of bounded wildcards, a type parameter of would accept all types that are subtypes of Object. As the Java Tutorials states: The upper bounded wildcard, , where Foo is any type, matches Foo and any…
Vince
  • 14,470
  • 7
  • 39
  • 84