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

Java Lower Bound Wildcard

How come the List in the main method below compiles? class Breakfast { } class Drink extends Breakfast { } class Juice extends Drink { } class Food extends Breakfast { } class Bread extends Food { } public static void main(String[] args)…
Clatty Cake
  • 719
  • 4
  • 8
  • 16
0
votes
0 answers

Bounded type paratemers in Collections.max()

I am tryng to understand how generics works in Java. The following code is from Collections.max() method of OpenJDK. public static > T max(Collection coll) { Iterator i =…
ryandam
  • 684
  • 5
  • 13
0
votes
1 answer

Same instances should have same reference

At the moment I have a class Fraction that allows me to create Fractions in three different ways With one Integer, in this case the given Integer will be the numerator and the denominator will be set to 1 With 2 Integers, the numerator and the…
Trikalium
  • 25
  • 6
0
votes
1 answer

Java Generic Type Converted to Scala does not accept super class itself

I'm writing a framework. The interfaces are written and compiled in Java code. The client uses Scala and those interfaces. Here is an example of the interface. public interface Context { MyComponent getComponent(); } Now my scala…
TrongBang
  • 923
  • 1
  • 12
  • 23
0
votes
3 answers

Filling a List

Having a collection of abstract objects: Set foes; I want to have a method like this: List getFoesByType(TypeEnum type); I've tried: List result = new ArrayList<>(); for(Foo f : foes) { if(f.getType() ==…
anat0lius
  • 2,145
  • 6
  • 33
  • 60
0
votes
2 answers

Java - Bounded generic as input to bounded generic method

Given a generic Type Result with the following partial implementation public class Result { /* fields, ctor, etc... */ public Result mergeWith(Result other) { /* fiddle with this and other (acting as producer)…
baeda
  • 189
  • 12
0
votes
2 answers

Java bounded wildcards in C#

I'm stuck with this problem for several hours. I'm trying to find an equivalent method for C#. Java, works: public class Main { public static void main(String[] args) { ArrayList> list = new ArrayList<>(); …
boraida
  • 3
  • 4
0
votes
2 answers

retrieving sub type from a

There is an existing Set that is passed on to another function that takes in a Set as an argument. Given that Set is going to contain either a Politician Object or a Trump Object only. The Politican…
user46743
  • 89
  • 1
  • 10
0
votes
1 answer

Java: Having trouble implementing bounded generics interface

I searched through a lot of questions and other internet articles, but I can't seem to find the one that caters to my specific case, and none of the other ones solutions worked for me. I have this interface here: public interface…
gameCoder95
  • 349
  • 1
  • 5
  • 19
0
votes
1 answer

Java generics, wildcards, collections: compilation error

Given the following class: import java.util.ArrayList; import java.util.Collection; public class Main { private static class A { } private static class B { private void thenReturn(T value) { } } private…
Johannes Flügel
  • 3,112
  • 3
  • 17
  • 32
0
votes
1 answer

Limiting the bounded-wildcard type in Java to only one level of the sub-classes of a class?

I would like to limit the compatible types of a method's input to a collection of a certain classes sub classes. However, I would only like to allow the first level down of the sub-classes to be accepted. For instance the class 'shape' has a variety…
Delrog
  • 735
  • 2
  • 11
  • 27
0
votes
1 answer

How does a method with a varargs of Bounded Wildcard type compile?

I'm scratching my head over how this example works and seems to print appropriately . public class Test { static class Shape { public String toString() { return "Shape"; } } static class Circle extends Shape…
wave
  • 1,740
  • 3
  • 15
  • 16
0
votes
1 answer

Generic Wildcard Bounded Type vs Generic Bounded Type Parameter

While on a quest on understanding about Java Generics, I've come across this: public static int sumListElems(List list){ int total = 0; for(Number n: list) total += n.intValue(); return total; } Suppose I have…
yev
  • 512
  • 4
  • 14
0
votes
4 answers

A simple clarification about Wild Cards in Java

I just read about Wild Cards in Java, and I'm looking at a case in which I have 2 classes, ParentClass and SubClass which inherits from it. (ParentClass, SubClass extends ParentClass) Now I write these lines. List list1 = new…
Ana M
  • 657
  • 6
  • 9
0
votes
2 answers

Are wildcards in java only applicable to collections?

I am not able to figure out how to declare a parameter in a function or a field in a class using ? wildcard without using any collection. For eg. I can write something like List l1 but how do I use ? without using List or any such collection.