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

Java : Generics, wildcards with multiple bounds

First of all, happy new year to everyone ! I would like to write a method with a multiple bounds parameter like that: public static void max(List> list) But unfortunately this code does not compile, could someone…
valoo86
  • 23
  • 2
0
votes
0 answers

Allocating generic type with bounded wildcard using diamond

The following code: Set set = new HashSet<>(); Compiles fine. However, the essence of using the diamond notation is just for not rewriting the LHS type parameter again. This means that the statement above should be equivalent with…
Some1
  • 103
  • 7
0
votes
1 answer

Why can I use bounded wildcards for parameter and not for return type in a method?

I couldn't find a better description of my question's topic, so I will try to better explain my problem. I noticed that if I use bounded wildcards I can use its "bound" as an argument, but not as a return value. This might sound confusing, so I will…
Stefan
  • 969
  • 6
  • 9
0
votes
1 answer

JAVA 8 - Implementing Comparable in a user-defined class that uses generics - Collections.sort() not working

I have a user-defined class that is supposed to be generic to accept Integers, Floats, Doubles, etc. but also needs to be able to be compared. It looks like: public class MyClass> implements Comparable>{ private T…
ckrug
  • 17
  • 4
0
votes
1 answer

Java Generics bounded wildcards

This is a rather contrived example that I'm using to learn Java streams but I'm stuck with a generic wild card issue, I believe. My code is below. I'm trying to read every Employee object from a list, apply a function to get the id and set it in a…
JavaNovice
  • 1,083
  • 1
  • 12
  • 20
0
votes
0 answers

Java generics-in-an-instance-field design

Let's say I have Node class that is part of a graph. Each Node handles generic types T and S, so that we have Node. Each Node can add inputs to itself from other Nodes via an addInput method. class Node { void…
0
votes
2 answers

Get-put principles in Java generics

I was learning Java generics lately and came across the so-called "get-put" principle, i.e. which kind of wildcards allow you to add or remove certain types of objects from a collection (reference, e.g. https://flylib.com/books/en/4.79.1.18/1/). My…
Daniel Pop
  • 456
  • 1
  • 6
  • 23
0
votes
1 answer

Why does not work for comparingByValue method?

In the Java JDK, I noticed that there is a static method in the Map class: public static > Comparator> comparingByValue() { return (c1, c2) -> c1.getValue().compareTo(c2.getValue()); } To my…
souverlai
  • 43
  • 4
0
votes
2 answers

How can I re-bound a wildcard?

I'm implementing (single-pivot) Quicksort in Java. I read there is a term of partitioning and I thought I can write a method with extensibility. public static void sort( final List list, final…
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
0
votes
1 answer

What is the more idiomatic use of super and extends in this case?

A library I am contributing to maintains a registry of Classes and handlers that can accept instances of these classes, think Consumer. There is a facility for registering a handler for a type *and all its subtypes`, e.g. void…
Clashsoft
  • 11,553
  • 5
  • 40
  • 79
0
votes
1 answer

Implementing an Upper Bounded Generic Method

I am having trouble building a somewhat circular generic configuration. I need a "state" interface that provides a getter to a generic "container" interface. The container interface on its methods needs to accept a generic "state" as one of the…
Vance T
  • 513
  • 2
  • 5
  • 15
0
votes
1 answer

Error putting superclass objects in lower bounded wildcard list

I understand that using a lower bounded wildcard on a list as method parameter should let us put elements of that lower bound and its super types but consider following code: public class WildcardError { void foo(List i) { …
Abhay Sharma
  • 309
  • 5
  • 16
0
votes
1 answer

Why List> is not working for List>

I am iterating through a map whose keys are charts and values are data sets which will be displayed on charts. Data sets are lists of maps because I have multiple XYSeries displayed on each of my Charts (one series - one map with x and y values). In…
Lucija
  • 5
  • 4
0
votes
1 answer

How to avoid (bounded) wildcard in return parameter

I have an interface with a method returning an immutable collection with a bounded wildcard. public interface Foo { Set getAllBar(); } public interface Bar { String getBar(); } An abstract class implementing that interface…
0
votes
1 answer

Making parameterized ScalaCache generic with runtime configuration

The git repo that contains the issue can be found here https://github.com/mdedetrich/scalacache-example The problem that I currently have is that I am trying to make my ScalaCache backend agnostic with it being configurable at runtime using typesafe…