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
3 answers

Generic argument method ? super

Why can NOT be added a new Object into a List if with this type is supposed to be able to add any supertype of Apple? import java.util.List; import java.util.ArrayList; class Apple{} public class Macintosh extends Apple { public static void…
Joe
  • 7,749
  • 19
  • 60
  • 110
1
vote
2 answers

Why no bounded wildcard in input parameters of synchronizedCollection() static factory method?

I was going through the Java tutorial and stumbled on something which I did not understand. In the Collections trail, they talk about Wrapper implementations, there I notice two static factory methods - public static Collection
Rohit Agarwal
  • 420
  • 1
  • 3
  • 18
1
vote
1 answer

Adding an element inside a wildcard type ArrayList

I am trying to add an element in a list where the list type parameter is a wildcard that extends Question ArrayList id = new ArrayList(); id.add(new Identification("What is my…
user962206
  • 15,637
  • 61
  • 177
  • 270
1
vote
2 answers

Java Generics - wildcards

I'm new to Java and have gotten myself into a situation where it's evident that I'm misunderstanding something about how it handles Generics, but reading tutorials and searching stackoverflow hasn't (at least so far) given me clarity beyond that I…
Khanmots
  • 181
  • 1
  • 10
1
vote
1 answer

Generics Wildcards Proper Use

I have an object defined as follow: protected Map> geoms=new HashMap>(); I try to insert in an object that looks conform to the wildcard ArrayList points=new…
giscope
  • 77
  • 1
  • 8
0
votes
3 answers

Why Wild Cards can't be used in generic class & method declaration?

Declaration like this : class A { } is allowed.Whereas declaration like this is not allowed. class A { } Is there any logical explanation about why Java restricts us to do that? & what's the…
Debadyuti Maiti
  • 1,099
  • 4
  • 18
  • 30
0
votes
2 answers

JAVA Wildcard Capture Error with an array of generic stacks

Stack[] stacks = { new Stack(), new Stack(), new Stack(), new Stack(), new Stack() }; That's the code making the array of stacks. The reason I'm putting them in an array is because it's…
prunes4u
  • 43
  • 1
  • 1
  • 8
0
votes
3 answers

Type Safety warning

In the book Java Generics and Collections by Maurice Naftalin, Philip Wadler, I was going through Generics limitations and came up with doubt. May be that is answered in the book, but I think I am confused a loy. In the following code: …
Dipesh Gupta
  • 787
  • 1
  • 7
  • 20
0
votes
5 answers

Can't refer to generic type from bounded wildcard reference

What is wrong with Class A below that won't allow it to compile? public class GenericsHell { interface Shape{} interface Circle extends Shape {} interface ShapeHelper { void draw(T shape); } class A
Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246
0
votes
0 answers

how can is distinghush wildclass (upper bound wildcard class)

private static void printNumbers(List numbers) { Number number = numbers.get(0); } or List names = new ArrayList<>(); List tmp = names; i learn the wildcard, use '?' symbol. and i learn this is also wildcard. in my…
chobodark
  • 25
  • 4
0
votes
2 answers

Not able to create generic bounded class objects with interface

I am trying to use bounded types with generics to create generic objects of subclasses (these implement an interface). But I am getting type mismatch errors when initializing objects with the subclasses. Here is the interface: public interface…
0
votes
2 answers

Java Function interface with bounded wildcard

I'm trying to use java.util.Function object as an input to a library class. Function mapper; public MyLibraryClass(Function mapper) { ... } public void aMethod(ClassExtendsMyEntity e) { …
Zizou
  • 831
  • 1
  • 10
  • 19
0
votes
1 answer

Generalizing the insertion of different subclasses to respective HashMaps

I have two classes, Stock and Crypto. Both are subclasses of Security, which is where they get the methods invoked below. I want a general method that accepts either one of these and adds or updates to the respective HashMap. Something like: 1 …
nqodonnell17
  • 15
  • 2
  • 5
0
votes
0 answers

Custom logic for specific bounded generic types in Java

I'm having the following hierarchical structure for A, B and C: interface A { } class B implements A { } class C implements A { } I want to design a generic method that takes in Set of any subtype of A and perform some custom logic if the…
Lavish Kothari
  • 2,211
  • 21
  • 29
0
votes
1 answer

Bounded type as method parameter?

I am trying to enforce a particular subclass type on an abstract method, like so: public abstract class Searcher { public abstract SearchResult search( searchInput); } This is to ensure that subclasses of Searcher have…
IVR Avenger
  • 15,090
  • 13
  • 46
  • 57