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
2
votes
1 answer

How to convert Scala List[String] to legacy Java List

I have legacy (no modification allowed) Java code: //File Foo.java package sof; public interface Foo {} //File Bar.java package sof; public class Bar implements Foo {} //File Holder.java package sof; import java.util.List; public…
Lopotun
  • 611
  • 7
  • 15
2
votes
1 answer

redundant declaration of methods

I want to know if there are any logical differences in declaration of these two methods: exemple 1 public static > T findMax(List list) exemple 2 public static > T…
2
votes
1 answer

Covariant return types while overriding methods and `List' vs 'List'

Intuitively (and wrongly) I think that List is not more specialized than List so below shall not compile (because return type covariance mandates that return type in Derived is same or subtype of that in Base) - but it compiles! I…
Code Complete
  • 3,146
  • 1
  • 15
  • 38
2
votes
1 answer

PECS For generics in Non Collections

Joshua Bloch came up with the PECS, which says the rule when to use ? extends T and ? super T. If you think about PECS in terms of Collections framework, then it is very straightforward. If you add values to the data structure, use ? super T. If you…
Fatih Arslan
  • 1,054
  • 1
  • 9
  • 10
2
votes
0 answers

Java nested generic casting (? super Type)

I tried to wrap my head around a problem I cannot seem to find an answer for. Consider the following 2 examples: { // this works. Consumer c = F -> {}; test(c); } static void test(Consumer c) { /* content…
n247s
  • 1,898
  • 1
  • 12
  • 30
2
votes
2 answers

capture#1-of ? super C interpreted as C in generic interface

Given the following classes: class A { public A a() {return new A();}; }; class B extends A { public B b() {return new B();} }; class C extends B { public C c() {return new C();} }; class D extends C { public D d() {return new D();} }; I want to…
2
votes
2 answers

Java8, bounded wildcard type not considered a functional interface

In my situation, I have a consumer that takes a supplier of ? extends String and executes some action on it, so the declaration goes like this : final Consumer> action = ... The problem is when I try to execute…
marsouf
  • 1,107
  • 8
  • 15
2
votes
3 answers

Incompatible types when using lower bound wildcard

I can't understand why I have a compilation error for this code: private static Consumer f3() { return t -> {}; } private static Consumer f4() { return t -> {}; } @Test public void test() { Consumer
Florin M
  • 179
  • 2
  • 9
2
votes
1 answer

Why lower bound is used in functional interfaces

The usage of lower bound in collections context is more or less clear to me (see PECS question). Not so with functional interfaces in Java. E.g. Why Optional.map method has the signature with lower type bound: Optional map(Function
g6380647
  • 267
  • 1
  • 2
  • 10
2
votes
1 answer

Why this Java generic code with bounded wildcard doesn't compile?

I was reading the superb Java Generics FAQ by Angelika Langer when I read this source code example which I can't understand: import java.util.List; import java.util.ArrayList; import java.lang.Number; import java.lang.String; import…
madtyn
  • 1,469
  • 27
  • 55
2
votes
4 answers

Java generic wildcard extend

Why can't I add an integer to this type of list, even though Integer extends Number> List numList = new ArrayList(); Integer f = 12; numList.add(f);
Sultan Zhumatayev
  • 535
  • 1
  • 9
  • 18
2
votes
0 answers

Java compiler lack of type inference when using generic interface

Why is the compiler not figuring out that i am using the wrong type in the snippet below? Here's an interface with a generic. interface Foo { Map getMap(); X addFoos(int foos); } Notice the getMap() method which does…
Marco
  • 588
  • 1
  • 4
  • 15
2
votes
1 answer

Where does the Type Annotation belong in a bounded wildcard?

I recently started updating my Java projects with Eclipse's nullability annotations. I have a JavaFX base project, containing some translation classes. Now, in my LocalizedList, I initialize it with an element in the document tree and it recursively…
Adowrath
  • 701
  • 11
  • 24
2
votes
2 answers

? super String Lower Bound Java

I am reading book for OCP of Author Jeanne Boyarsky and Scott Selikoff, Book saying : Page # 122 ? super String With a lower bound, we are telling Java that the list will be a list of String objects or a list of some objects that are a superclass…
Shahid Ghafoor
  • 2,991
  • 17
  • 68
  • 123
2
votes
1 answer

Java generics not applicable for the arguments issue

I've got an issue with a generic framework I'm writing. Can someone explain to me, why my code does not compile? I've tried to show it with this simple example. (UPDATED EXAMPLE) import java.lang.annotation.ElementType; import…
BenGe89
  • 141
  • 1
  • 7