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

Instantiation of Generic object with wildcard

I have a class public class OrderedBox {} Compiler doesn't allow to create member/local variable like these. OrderedBox testItems1 = new OrderedBox(); List testItems2 = new ArrayList
arc
  • 113
  • 9
3
votes
1 answer

Java Bounded Type Parameters in HashMap

I am just learning how to use wildcards and bounded type parameters. I want to use (I think) bounded wildcards in a method that is passed a HashMap. I've seen examples of bounded type parameters and bounded wildcards but I haven't found anything…
Patricia
  • 5,019
  • 14
  • 72
  • 152
3
votes
1 answer

How to call wildcard java function from scala

I have a java library with some generic containers: public interface IColumnTable> { } public interface IColumn, M extends IMetaData> { } public interface IColumnValues { } public interface…
3
votes
3 answers

Private helper method to capture wildcard type for generic methods

The following code does not compile in Eclipse. It says "The method putHelper(List,int,E) in the type Abc is not applicable for the arguments (List <.capture#8-of extends E>",int,E)" private void putHelper(List list, int i, E value) { …
3
votes
3 answers

Going from Abstract Generic to Non-Abstract Non-Generic

I've been working on a project in Java lately that uses a Generic-Best Search Algorithm. To make the Algorithm itself Generic, I used a bunch of Generics on all of the Classes used in the Algorithm. Here's all of the Class/Interface…
Boom
  • 2,465
  • 4
  • 19
  • 21
3
votes
2 answers

Generics casting explanation needed

Here are two forms to work with generics extending base type: public abstract BlockingQueue getQueueA(); public abstract BlockingQueue getQueueB(); I don't understand. What is the difference? Both methods…
Andrey
  • 853
  • 9
  • 27
3
votes
2 answers

Why is passing a subclass to a bounded wildcard only allowed in certain places?

This following is from generics tutorials: Say class R extends S, public void addR(List s) { s.add(0, new R()); // Compile-time error! } You should be able to figure out why the code above is disallowed. The type of the second…
Murali
  • 1,495
  • 2
  • 15
  • 28
3
votes
5 answers

Generic lower unbound vs upper bounded wildcards

import java.util.List; import java.util.ArrayList; interface Canine {} class Dog implements Canine {} public class Collie extends Dog { public static void main(String[] args){ List d = new ArrayList(); List c =…
Joe
  • 7,749
  • 19
  • 60
  • 110
3
votes
1 answer

What's the correct usage of generic wildcards when defining functional Java APIs?

I'm writing functional-style static helper methods acting as operators for a generic abstraction (say Iterable), and I'm a bit confused about when I should use wildcards. What are the correct, most type-safe and simplest method signatures in the…
thSoft
  • 21,755
  • 5
  • 88
  • 103
2
votes
2 answers

(Java) How to implement an interface method with a bounded wildcard generic?

I maintain 2 projects with the same functionality and I am consolidating this functionality into a commons project. I defined an interface: public interface GraphData { public List getShapes(); public void setShapes(…
user1277290
  • 21
  • 1
  • 2
2
votes
2 answers

How do I use Java generic wildcards with methods taking more than one generic parameter?

So we have a generic method like this, which is part of dependency injection initialisation: public static void registerTransient( Class serviceClass, Class implementationClass) { // } At some point we found a…
Hakanai
  • 12,010
  • 10
  • 62
  • 132
2
votes
2 answers

Upper bounded wildcard as a Map Value - how to provide a default Value with getOrDefault()

I have the following map Map> map Sometimes there is a List and sometimes there is a Set as a value. Now I'd like to get value, but there is a problem, it does not compile. Collection value =…
2
votes
1 answer

Lower and upper bound generic constraints on same wildcard

Let's assume the following class hierarchy: class A{} class B extends A{} class C extends B{} class D extends C{} In Java, it is possible to define wildcards with generics like this: List aOrDown;//A or any subtype of A List
dan1st
  • 12,568
  • 8
  • 34
  • 67
2
votes
2 answers

Java generics and bounded types

I have a wrapper class for ConcurrentMap like the following: public MapWrapper implements ConcurrentMap { private final ConcurrentMap wrappedMap; ... @Override public void putAll(Map map)…
teto
  • 4,019
  • 4
  • 21
  • 18
2
votes
1 answer

Incompatible types when using nested upper-bounded wildcards

I'm working on a functional programming library for Java, and I've hit a frustrating issue. I've got the following functions in my Option class: /** * Returns an Option containing the provided value. */ public static Option some(V…
Zoey Hewll
  • 4,788
  • 2
  • 20
  • 33