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
1
vote
2 answers

are these wildcards redundant?

I have two examples of methods that uses wildcard generics. First example: public static T findMax(List list) Second example: public static > T findMax(List list) I want to…
1
vote
0 answers

Using a bounded wildcard (? extends) with a list of Converters gives a type mismatch error

I have the following classes: import com.google.common.base.Converter; public class CustomConverter extends Converter { // Implemented overrides are here and working } and public class BaseModule { } and public class…
David
  • 4,744
  • 5
  • 33
  • 64
1
vote
1 answer

Collections.sort generic method signature

Inside java.util.Collections we have the below method signature public static void sort(List list, Comparator c) I don't understand why one would specify Comparator instead of Comparator Which use cases does it…
Bogdan T.
  • 668
  • 6
  • 13
1
vote
0 answers

java generics bounded wildcards `? super ? extends T`

I stumble upon this problem million of times and I can't wrap my head around it. Let's assume I have a stream seq of type Stream and I want to execute a forEach on its elements. The type of the consumer now becomes a Consumer
marsouf
  • 1,107
  • 8
  • 15
1
vote
2 answers

Upper bounded wildcards causing compilation error in Java

I cannot understand why I am getting these compilation errors: 1: The method add(capture#1-of ? extends Exec.Bird) in the type List is not applicable for the arguments (Exec.Sparrow) 2: The method add(capture#2-of ? extends Exec.Bird) in the type…
Junaid
  • 25
  • 4
1
vote
3 answers

Is it possible to do "Class foo"?

I'm having an hirarchy like this: class Foo { ... } class Bar extends Foo { ... } class Baz extends Foo { ... } And now I'm trying to declare an object like this in class foo: Class anyName; Then there is some method where I want to…
Hann3s
  • 19
  • 3
1
vote
1 answer

How does Java capture type variables in a generic method of a generic interface?

The file GenericInterface.java: import java.lang.Comparable; import java.util.function.Function; public class GenericInterface { } interface Comparator { int compare(T o1, T o2); static > //comment out this line …
Robert
  • 1,964
  • 1
  • 22
  • 22
1
vote
2 answers

Can we restrict particular string case while loading it to Arraylist object?

A file consisting of many words, where some words are purely in 'UPPERCASE', some words are purely in 'lowercase' and rest are in 'miXeDCasE'? I want to load these words to Arraylist object but it should not allow to load words in UPPERCASE. Can we…
1
vote
1 answer

Compiling with wildcard generics

I'm working on the Java OCP exam, and I came across this question Given the following code: Transaction t1 = new Transaction<>(1, 2); //1 Transaction t2 = new Transaction<>(1, "2"); //2 It is required that //1 must compile and //2 must NOT compile.…
Shoikana
  • 595
  • 4
  • 8
1
vote
1 answer

Creating a static field which is a nested list using generics and wildcards

This question is specifically about nested lists which is a static field of the generic class. If I am maintaining the logs in a static variable inside my class using the code: static List> myLog = new ArrayList
1
vote
1 answer

Why do I get bound mismatch error wen calling Enum.valueOf() using a wildcard

I am trying to call the Enum class's static method valueOf() but I received a compile error. Please look at the code snippet below. public void hello(Class> q){ Object o= Enum.valueOf(q,"hello"); } IntelliJ IDEA…
Raj
  • 203
  • 1
  • 2
  • 8
1
vote
2 answers

Lower bound wildcards

I am trying understand the following code snippet, searched links on stackoverflow with regards to lowerbound and upperbound Just trying to get over the confusion in the following line , si=s//OK why? arrays of objects can be implicitly casted to…
chebus
  • 762
  • 1
  • 8
  • 26
1
vote
2 answers

How do I make a list of several kind of object if I cannot use wildcards?

I want to make a List which can hold two kind of Object. What comes in my mind is to use wildcard. Below is my code. public class Parent { //code } public class ChildOne extends Parent { //code } public class ChildTwo extends Parent { …
Gustaf
  • 262
  • 1
  • 13
1
vote
1 answer

Problems with inferring types with bounded wildcards

What's the proper way of setting temp2.in in the below snippet? Why does the code not compile? public class WildCards { public static void main(String[] args) { TheBox temp1 = new TheBox(); temp1.set(10); …
hungry91
  • 133
  • 8
1
vote
1 answer

generics object creation showing error in eclipse, but compiles in maven

Hi I have some initialization as below.... it is showing error in eclipse as type mismatch. if i compile using maven through command prompt, it compiles without any error.. using eclipse luna, and compiler set to java 1.7. ReportData rdata…
Prasanna Ab
  • 116
  • 2
  • 6