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

Using Guice, how can I inject a bounded-wildcard class?

Using Guice, I want to inject a bounded-wildcard class. To be clear, I don't want to inject an object, but inject a class type. The would read: class A { Class a; @Inject A(Class a) { this.a…
Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246
1
vote
1 answer

Confusion about generic bounded wildcard types

Pretty trivial Java question. This code has an error: public abstract class SubTypeDependentEditor implements Editor { protected abstract Editor getEditorFor(T obj); public void edit(T obj) { Editor
Andrey Agibalov
  • 7,624
  • 8
  • 66
  • 111
1
vote
2 answers

Cast generic return type into lambda expression

I use this lambda expression to find a nested object Optional onlineResourceOptional = metadata.getDistributionInfo().stream() .filter(Objects::nonNull) .flatMap(distribution ->…
charlycou
  • 1,778
  • 13
  • 37
1
vote
0 answers

Eclipse: Bound mismatch error with Generics and wildcards

Eclipse is currently triggering this error: Bound mismatch: The type V is not a valid substitute for the bounded parameter >> of the type Controller however compilation and running work fine (no errors with…
maicol07
  • 199
  • 1
  • 6
  • 16
1
vote
0 answers

Java Method with Generics and Wildcard

I have an issue with a task I have to solve. I have to create a static method 'copy' which gets two list-like generalised arguments and has to copy one in the other. I don't understand how to use generics in this specific case. Right now I get an…
Rocket
  • 31
  • 6
1
vote
2 answers

Bad return type in lambda expression when using Java's Optional.or() with subclasses

I am trying to use Optional.or to get an object of subclass A or, if empty, an object of subclass B: interface Node {} class InnerNode implements Node {} class LeafNode implements Node {} Optional createInnerNode() {} Optional
r0estir0bbe
  • 699
  • 2
  • 7
  • 23
1
vote
1 answer

List> and List> and how to use it correctly?

Consider the following snippet: List doubleList = null; List integerList = null; List numberList = null; //expression:1 List> superDoubleList = Arrays.asList(doubleList,…
1
vote
0 answers

#HyperSkill Type Erasure(JAVA) Violator question, why my solution isn't working?

Question Link Problem Statment: You are asked to perform a security audit in a baking company. Their products are represented by Bakery class and its different subclasses like Cake and LemonTart. All the pastries are sold in nice boxes. Prior to…
1
vote
3 answers

Compiler errors with java bounded wildcard generics

Having the below code: Stack integers = new Stack(); Stack numbers = integers; Number n = numbers.pop(); numbers.push(3); numbers.push(n); I get compilation errors on the last two lines, but…
yas4891
  • 4,774
  • 3
  • 34
  • 55
1
vote
1 answer

Subtyping and Generics Function in Java

I have an interface A and class B implementing A. I have a method in A, called doX(Function f). When I tried to implement it in B, by writing doX(Function f), it doesn't compile. I tried a variant…
1
vote
1 answer

Compilation error with bounded wildcards using Java classes in Scala

In Java, we have defined an ObservableCollection.java like this: public class ObservableCollection implements Collection { public SubscriptionHandle onElementAdded(Consumer onAdded) { // ... } } And an AgentService.java that…
1
vote
1 answer

Generics in Java - Wildcard Usecases

We use wildcard in the method args when we want to pass list containing objects of child class. But as shown below we can achieve the same functionality using Type parameter. So why we need wildcards ? Scenario Lets say we have base-class named…
Harsh Kanakhara
  • 909
  • 4
  • 13
  • 38
1
vote
1 answer

Bounded wildcards and adding new elements to a queue

The other day I wad trouble understand a particular textbook example related to bounded wildcards and how they're used in combination with a Queue. The example starts by setting up a trivial inheritance hierarchy: class X { int i; X(int i) {…
MrBr
  • 1,884
  • 2
  • 24
  • 38
1
vote
1 answer

Bound Parameter Type to only accept Context Object not Activity Object

public static APIInterface getServiceInstance(T context) { if (apiInterface == null) { // do Something } return apiInterface; } This method is currently accepting all the objects .. Activity.this ..…
Paranoid
  • 214
  • 2
  • 14
1
vote
1 answer

What is the benefit of an upper bounded wildcard?

The recurring explanation I find is that an upper bounded wildcard relaxes the restrictions of types that a type parameter can accept. This concept applies to bounded generics as well, for example: static void gMethod (ArrayList…
Matthew S.
  • 464
  • 2
  • 12