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

C# generic wildcards

I spent quite some time solving this but I cannot figure it out. I have the following Java method signature: public static boolean isSameCollectionSets( Collection> set1, Collection>…
Biocoder
  • 65
  • 9
1
vote
1 answer

Java F-Bound types with generics

Is there any way to express f-bound types in java where at the call site, a generic response is returned? interface Functor> public T map(Function fn); // won't compile because types don't match I can use f-bound…
ekaqu
  • 2,038
  • 3
  • 24
  • 38
1
vote
2 answers

How to use multiple wildcards in an API call

I'm working with Anthill (an UrbanCode/IBM product) and one of the methods requires a parameter of type java.lang.Class> sourceConfigType. After reading the tutorial on generics I found that a class GitSourceConfig is a…
Wheeler
  • 454
  • 5
  • 17
1
vote
4 answers

Java generics: incompatible wildcard capture

I have a repository interface parameterized with stored entities type. Among other methods in it, I have two of interest: the create() method which instantiates an entity, and the save() method that saves the entity: public interface…
Pavel S.
  • 1,202
  • 1
  • 13
  • 29
1
vote
1 answer

Is there a need to use bounded wildcard generics in a passthrough method?

I know that in the following method in Collection: public void addAll(Collection subcollection); We use Collection there to allow a collection that solely exists of sub-elements, example: List drivables = new…
skiwi
  • 66,971
  • 31
  • 131
  • 216
1
vote
1 answer

Using wildcards on interfaces

Consider the following classes: interface Notifiable { } class NotifiableImpl1 implements Notifiable { } class NotifiableImpl2 implements Notifiable { } class NotifiableImpl3 implements Notifiable { } It is normal that the following code…
skiwi
  • 66,971
  • 31
  • 131
  • 216
1
vote
1 answer

generics in constructors in Java?

Consider this hypothetical class (which I found in a online video): public class Contrived extends ArrayList { List values; ...... } Here the type variables that Contrived can accept is Number or…
brain storm
  • 30,124
  • 69
  • 225
  • 393
1
vote
2 answers

Java Wildcard writing and reading permissions

When learning about Java Wildcards i found myself misunderstanding about this theme, so. Upper Bound allows me to read-only members of generic class Lower Bound allows me to write to member, but only if it is the types lower bound List
1
vote
1 answer

Java - Generics wildcard issue

My goal is pretty simple. I've a SetInterface: public interface SetInterface { public T duplicateSet(); } I then also have an ExerciseInterface > public interface ExerciseInterface
mgibson
  • 6,103
  • 4
  • 34
  • 49
1
vote
1 answer

Java: Generic Interface, instance chosen at runtime

I have the following simple interface: public interface ISimmilarityMeasure { public double getSim(T s, T t); } and implementations like public class NormalizedLevenstheinSim implements …
feob
  • 1,930
  • 5
  • 19
  • 31
1
vote
2 answers

Java bounded wilcard type

I need to define a generic class, and the type parameter must be an enum. I think it should look something like public class MyClass> { } But I can't seem to figure out the exact syntax. I should mention that I need a way to…
Dónal
  • 185,044
  • 174
  • 569
  • 824
1
vote
5 answers

Java Generics (bounded wildcards)

According to the book "Effective Java" of Joshua Bloch there is a rule about how/when use the bounded wildcards in generics. This rule is PECS (Producer-Extends, Comsumer-Super). When I study the following example: Stack numberStack = new…
LiTTle
  • 1,811
  • 1
  • 20
  • 37
1
vote
2 answers

Is lower bound in Java exclusive or inclusive?

I am reading Java the Complete Reference 7th edition, and it says that lower bound in Java Generics are exclusive, but I found the opposite here (It says that it is inclusive.) Is it because SE 6 differs from SE 7? Edit: Java the Complete Reference…
Ghasan غسان
  • 5,577
  • 4
  • 33
  • 44
1
vote
2 answers

Crafting a Comparator object for sorting generic List using Collections.sort()

I am trying to implement a generic sort utility method for a List of objects of any class that implements MyInterface. Per Java API (http://java.sun.com/javase/6/docs/api/java/util/Collections.html), Collections.sort() method signature is: public…
user186712
1
vote
3 answers

Why we can instantiate Pair but we can't with Pair

So why can we able to instantiate Pair but we can't able to instantiate Pair Pair p=new Pair(); VS Pair p=new Pair(); I know that mean unknown type --> but isn't mean the same thing --->
skystar7
  • 4,419
  • 11
  • 38
  • 41