Questions tagged [unbounded-wildcard]

An unbounded wildcard is the type argument "?", a feature of generics in the Java language. This type argument represents some "unknown" type which was once present.

An unbounded wildcard is the type argument ?, a feature of generics in the language. This type argument represents some "unknown" type which was once present.

A wildcard is useful as a type argument in a situation where knowing the exact type is unnecessary:

public static void printElements(Iterable<?> anyIterable) {
    for (Object element : anyIterable) {
        System.out.println( element );
    }
}

A type parameterized with a wildcard places restrictions on how an object can be used:

List<String> listOfString =
    new LinkedList<>( Arrays.asList("a String") );

// We can assign any List to a List<?>, but, in
// doing so, we lose information about its original
// type argument.
List<?> listOfUnknown = listOfString;

// The wildcard therefore causes methods that once
// returned String to now return Object.
Object unknown0 = listOfUnknown.get( 0 );

// Compiler error:
//  The wildcard prevents us from passing arguments
//  to any method that once accepted String.
listOfUnknown.add( new Object() );

// Compiler error:
//  Since we have lost knowledge of the original
//  type, we can not pass String to the List either.
//  (This is the case, even though we, as the programmer,
//  can see this would be safe to do.)
listOfUnknown.add( "another String" );

The ? extends Object is equivalent to the unbounded wildcard.

See also:

58 questions
2
votes
1 answer

Need to cast an object to unbounded wildcard....but how in my case?

public myConstuctor(Map myMap) { if (myMap.containsKey(MY_KEY)) { myMap.put(MY_KEY, someObject); } someMemberVariable = new someClass(myMap); } I am not able to put an object in the map due to the…
user1739658
  • 253
  • 1
  • 3
  • 10
1
vote
3 answers

Unable to assign the result returned by the Method that receives a generic List and returns a List

I'm trying to filter a List of objects that implements an interface. And I'm trying to create a generic method for all the classes. Something like: interface SomeInterface { String getFlag(); } class SomeObject implements SomeInterface { …
Victor Soares
  • 757
  • 1
  • 8
  • 34
1
vote
1 answer

List> and List> and how to use it correctly?

Consider the following snippet: List doubleList = null; List integerList = null; List numberList = null; //expression:1 List> superDoubleList = Arrays.asList(doubleList,…
1
vote
0 answers

#HyperSkill Type Erasure(JAVA) Violator question, why my solution isn't working?

Question Link Problem Statment: You are asked to perform a security audit in a baking company. Their products are represented by Bakery class and its different subclasses like Cake and LemonTart. All the pastries are sold in nice boxes. Prior to…
1
vote
1 answer

Generics in Java - Wildcard Usecases

We use wildcard in the method args when we want to pass list containing objects of child class. But as shown below we can achieve the same functionality using Type parameter. So why we need wildcards ? Scenario Lets say we have base-class named…
Harsh Kanakhara
  • 909
  • 4
  • 13
  • 38
1
vote
1 answer

How to avoid the wildcard in the return type

Given the following type hierarchy: public class GenericBaseClass { } public class SpecializedClass & SomeInterface> extends GenericBaseClass { } public class SomeProvider { private static final…
stg
  • 2,757
  • 2
  • 28
  • 55
1
vote
1 answer

How can I specify an unbounded wildcard type parameter?

I couldn't event choose right words for the question. I have a class and a factory method. class MyObject { static , U, V> T of( Class clazz, U some, V other) { //…
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
1
vote
0 answers

Java unbounded wildcard misunderstanding

Let’s say I define a collection of parametrized types like this: Deque> nodes = new LinkedList<>(); nodes.add(new Node(5)); Why is it an error to pass that reference to a method with an unbounded wildcard in the signature like…
Node1895
  • 11
  • 2
1
vote
1 answer

Java - casting from unbounded wildcard to string

So, at first I had a line saying : Object input = .... //some come code initialising it if(input instanceof Set) { doSomething(input); } However, since generic type information is erased at runtime, I cannot specify that it is a…
Kerage Chan
  • 116
  • 1
  • 9
1
vote
0 answers

Real life usage of unbounded wilcard

As far as I understand unbounded wildcard statement (?) could be used only at the method argument level and only for collections. Could someone please give me an example from his or her real experience where this can be helpful? I mean an example of…
sidlejinks
  • 709
  • 1
  • 9
  • 25
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
0
votes
2 answers

Java Generics supertypes Wildcard ? VS Object VS AbstractList

I have been trying to get a deep understanding of Java Generics, because I felt I needed to know how this works. I have been going through the Oracle documentation and one thing confuses me. In the Generics, Inheritance and Subtypes here:…
user16422658
0
votes
0 answers

Why is there an infeasible or unbounded solution in the c-plex while incorporating the objective function?

Because I am unable to do it in a single program, the values of pevch [j] [k], pevdis [j] [k], pbat [j], and c [j] are actually input from the outputs of another optimization program. This program's decision variables are pbatch[j][k], pbatnew[j],…
Harip
  • 9
  • 5
0
votes
2 answers

Java Function interface with bounded wildcard

I'm trying to use java.util.Function object as an input to a library class. Function mapper; public MyLibraryClass(Function mapper) { ... } public void aMethod(ClassExtendsMyEntity e) { …
Zizou
  • 831
  • 1
  • 10
  • 19
0
votes
1 answer

Intellij - Unexpected wildcard mark as problem

We moved from VS Code to use Intellij Ultimate latest version, and we have existing code using Java 8: private Map>> loadConditions(Map> conditions) { Map>> conditions =…
Ori Marko
  • 56,308
  • 23
  • 131
  • 233