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

Force Interface Method Return Value to be Wildcard with Multiple Classes

This question is a follow up from Java Generics Wildcarding With Multiple Classes. I'm writing an interface like this: public interface SomeInterface { public Class getClassForObject(Object…
ghopper
  • 100
  • 8
0
votes
1 answer

Unable to create annotation with multiple bound generic class arguments

I'm trying to create an annotation which can accept multiple classes as input. Typical usage would be @Prerequisites{FirstPrerequisite.class, SecondPrerequisite.class} For this I can create an annotation as shown…
Buddha
  • 4,339
  • 2
  • 27
  • 51
0
votes
1 answer

How to upper bound a map's value type

When should one of these be preferred to another? public static void doStuffWithIt_A( Map theThings){ ...} public static void doStuffWithIt_B( Map theThings){ ...} public static void…
Matt S.
  • 878
  • 10
  • 21
0
votes
3 answers

How to test if a generic list contains the exact subcollection of a subtype in Java?

I have an abstract class, AbstractService, and several classes which extend this abstract class: I then have a ServiceFactory that returns me a generic list with some services, according to a parameter I pass: public class ServiceFactory { …
Tarek
  • 3,080
  • 5
  • 38
  • 54
0
votes
1 answer

Trouble with upper bounded generics

I'm having some trouble with a class that contains a member that uses generics. Consider the following sample classes: class BaseRequest { // Content goes here } class SubRequest extends BaseRequest { // IFace methods go here, etc. } //…
vol
  • 1,533
  • 3
  • 13
  • 18
0
votes
3 answers

Difference between using `? super` in parameter and variable

I thought I'd figured out generics with super. But I must be completely wrong: With this simple example: class Animal{} class Dog extends Animal{} This code works fine because we are passing in a super class of Dog: static void addThis(ArrayList
Neil Walker
  • 6,400
  • 14
  • 57
  • 86
0
votes
1 answer

Java: not operator for wildcard bounds

After doing some reading, it appears that it is possible to use the & operator to require multiple extends: Class classObj; However, I'm looking for a way to enforce "not" functionality at compile time. I have the example…
jr.
  • 1,699
  • 14
  • 31
0
votes
3 answers

Java: Help me understand: How to use interface methods on a bounded wildcard field?

I'm having trouble understanding why I can use bounded wildcards like this, if I can't (seem to) make any (genericly-typed) use of it. If I have a wildcard field in a class, I can't use any of the methods with generic parameters of the implemented…
Cristian Diaconescu
  • 34,633
  • 32
  • 143
  • 233
0
votes
1 answer

Generic upper bounded wildcard instantiation known at run time

class Aliphatic extends Organic{} class Hexane extends Aliphatic{} public class Organic{ void react(E e){} static void main(String[] args){ Organic compound = new Aliphatic(); …
Joe
  • 7,749
  • 19
  • 60
  • 110
0
votes
2 answers

How to pass generics using wildcard to a function without casting?

Here is my problem: my function in class A: public void setData(Map>){...} my call: Map> a=...; instanceOfA.setData(a); //does not…
giscope
  • 77
  • 1
  • 8
-1
votes
2 answers

Is there an actual practical usecase of the wildcard character (?) in Java?

I don't get the actual practical use case of the wildcard character ? in Java, when using it with extends or super. The official Oracle Java documentation shows an example of adding numbers to a list and sum them up: private static double…
macwarrior
  • 31
  • 3
-1
votes
1 answer

Wildcard vs TypeParameter

class Employee { // valid } class Employee { // invalid } private static void test(List list1) { // valid } private static void test(List list1) { // invalid } what exactly…
user4811324
-1
votes
2 answers

Bounded wildcard as a method parameter

I would like to create a method which returns type is a map object and parameter should be a class which extends A and implements I. So my code is as follows: public Map getIdea(Class < ? extends A & I) { .....} But i am getting a…
user1459497
  • 659
  • 2
  • 9
  • 18
-1
votes
1 answer

add (List) where Z is an interface . Class B extends A implements Z why cant we call add() with a List

Interface Z{} Consider a simple interface Z as declared above. Class A{} A simple class A as declared above. Class B extends A implements Z{} Class B extends class A and implements interface Z class Test{ static void add(List
-1
votes
1 answer

Casting bounded wildcard to unbounded wildcard within a generic type is an error (X> to X>

Consider this sample: private void m(Class k, Set> sk) { Class ku = k; Set> sku = sk; // <-- Type mismatch: cannot convert from // Set> to…
davidbak
  • 5,775
  • 3
  • 34
  • 50
1 2 3
16
17