Adding Object to Generic List with two types
I read a couple of posts such as here but I was unable to find the solution for my problem.
Why I am unable to add d? It is a subtype of Object...
Type of d: A<B<X>>
List<A<B<? extends Object>>> rv=new LinkedList<>();
rv.add(d); //not working
EDIT
I tried to simplify the problem. When I do:
A<B<?>> abcv=new A<B<String>>();
I get the error: Type mismatch: cannot convert from A<B<String>> to A<B<?>>
However, String is compatible with "?" - so why is it not working? I want to add elements to a list where the last type can by anything, something like this:
List<A<B<?>>> rv=new LinkedList<>();
rv.add(new A<B<X>>());
rv.add(new A<B<String>>());
rv.add(new A<B<Integer>>());
java generics
add a comment |
I read a couple of posts such as here but I was unable to find the solution for my problem.
Why I am unable to add d? It is a subtype of Object...
Type of d: A<B<X>>
List<A<B<? extends Object>>> rv=new LinkedList<>();
rv.add(d); //not working
EDIT
I tried to simplify the problem. When I do:
A<B<?>> abcv=new A<B<String>>();
I get the error: Type mismatch: cannot convert from A<B<String>> to A<B<?>>
However, String is compatible with "?" - so why is it not working? I want to add elements to a list where the last type can by anything, something like this:
List<A<B<?>>> rv=new LinkedList<>();
rv.add(new A<B<X>>());
rv.add(new A<B<String>>());
rv.add(new A<B<Integer>>());
java generics
You need to create an object of typeA<B<? extends Object>>. This should be enough. What do you mean by "not working"?
– JF Meier
Jan 2 at 9:59
2
You can't guarantee that? extends Objectcan contain something of typeX. It could be something else entirely likeA<B<String>>.
– TiiJ7
Jan 2 at 10:01
2
And note that<? extends Object>is the same as<?>.
– Andy Turner
Jan 2 at 10:02
add a comment |
I read a couple of posts such as here but I was unable to find the solution for my problem.
Why I am unable to add d? It is a subtype of Object...
Type of d: A<B<X>>
List<A<B<? extends Object>>> rv=new LinkedList<>();
rv.add(d); //not working
EDIT
I tried to simplify the problem. When I do:
A<B<?>> abcv=new A<B<String>>();
I get the error: Type mismatch: cannot convert from A<B<String>> to A<B<?>>
However, String is compatible with "?" - so why is it not working? I want to add elements to a list where the last type can by anything, something like this:
List<A<B<?>>> rv=new LinkedList<>();
rv.add(new A<B<X>>());
rv.add(new A<B<String>>());
rv.add(new A<B<Integer>>());
java generics
I read a couple of posts such as here but I was unable to find the solution for my problem.
Why I am unable to add d? It is a subtype of Object...
Type of d: A<B<X>>
List<A<B<? extends Object>>> rv=new LinkedList<>();
rv.add(d); //not working
EDIT
I tried to simplify the problem. When I do:
A<B<?>> abcv=new A<B<String>>();
I get the error: Type mismatch: cannot convert from A<B<String>> to A<B<?>>
However, String is compatible with "?" - so why is it not working? I want to add elements to a list where the last type can by anything, something like this:
List<A<B<?>>> rv=new LinkedList<>();
rv.add(new A<B<X>>());
rv.add(new A<B<String>>());
rv.add(new A<B<Integer>>());
java generics
java generics
edited Jan 2 at 10:53
user3579222
asked Jan 2 at 9:57
user3579222user3579222
1449
1449
You need to create an object of typeA<B<? extends Object>>. This should be enough. What do you mean by "not working"?
– JF Meier
Jan 2 at 9:59
2
You can't guarantee that? extends Objectcan contain something of typeX. It could be something else entirely likeA<B<String>>.
– TiiJ7
Jan 2 at 10:01
2
And note that<? extends Object>is the same as<?>.
– Andy Turner
Jan 2 at 10:02
add a comment |
You need to create an object of typeA<B<? extends Object>>. This should be enough. What do you mean by "not working"?
– JF Meier
Jan 2 at 9:59
2
You can't guarantee that? extends Objectcan contain something of typeX. It could be something else entirely likeA<B<String>>.
– TiiJ7
Jan 2 at 10:01
2
And note that<? extends Object>is the same as<?>.
– Andy Turner
Jan 2 at 10:02
You need to create an object of type
A<B<? extends Object>>. This should be enough. What do you mean by "not working"?– JF Meier
Jan 2 at 9:59
You need to create an object of type
A<B<? extends Object>>. This should be enough. What do you mean by "not working"?– JF Meier
Jan 2 at 9:59
2
2
You can't guarantee that
? extends Object can contain something of type X. It could be something else entirely like A<B<String>>.– TiiJ7
Jan 2 at 10:01
You can't guarantee that
? extends Object can contain something of type X. It could be something else entirely like A<B<String>>.– TiiJ7
Jan 2 at 10:01
2
2
And note that
<? extends Object> is the same as <?>.– Andy Turner
Jan 2 at 10:02
And note that
<? extends Object> is the same as <?>.– Andy Turner
Jan 2 at 10:02
add a comment |
3 Answers
3
active
oldest
votes
List<SubClaz> is not a subtype of List<SuperClaz> in Java. That's why the wildcards are used: List<SubClaz> is a subtype of List<? extends SuperClaz>.
Now for your A<B<?>> abcv=new A<B<String>>(); example:
By adding the wildcard, you're making B<String> a subtype of B<?>, but since these are also wrapped by another type A, we're back to the first problem:A<B<String>> is not a subtype of A<B<?>>
(Notice B<?> is the SuperClaz and B<String> is the SubClaz in this case).
You can fix this the same way; by adding another wildcard: A<B<String>>() is a subtype of A<? extends B<?>>.
Keep in mind that this doesn't allow you to read or manipulate the list as you want. Search for covariance and contravariance for more detail. Here is a good one: http://bayou.io/draft/Capturing_Wildcards.html
add a comment |
d should have type A<B<? extends Object>> or compatible.
1
It never will have that exact type though, because the?will refer to different types in the declaration ofdand the one in theList's elements.
– daniu
Jan 2 at 10:13
@daniu yes. It is why I said "or compatible". Should have think that there is no d with exact same type.
– talex
Jan 2 at 10:18
add a comment |
List<A<B<?>>> rv = new LinkedList<>();
? in generic means any type.
For example:
You can assign any type of generic list to List<?> but you can not add any type of object into list because compiler does not know what type it is because of wildcard(?)
List<?> genericList1 = new ArrayList<String>();
List<?> genericList2 = new ArrayList<Integer>();
List<?> genericList3 = new ArrayList<X>();
/**
* Compiler will allow to assign any type of generic list to List<?>
* but it will not allow to add.
*/
genericList1.add("xyz"); // It will give compiler error
The <?> wildcard allows a list of ANY type to be assigned, but the add() method is not valid on that list because you could put the wrong kind of thing into the collection. Compiler does not know which type you would pass because of wildcard(?).
If you want to add any type of object to your list than you can take list like this.
List<Object> rv = new LinkedList<>();
rv.add(new A<B<X>>());
rv.add(new A<B<String>>());
rv.add(new A<B<Integer>>());
Now compiler knows that rv is the list which accept only Object type, so compiler will not complain anything.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54004281%2fadding-object-to-generic-list-with-two-types%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
List<SubClaz> is not a subtype of List<SuperClaz> in Java. That's why the wildcards are used: List<SubClaz> is a subtype of List<? extends SuperClaz>.
Now for your A<B<?>> abcv=new A<B<String>>(); example:
By adding the wildcard, you're making B<String> a subtype of B<?>, but since these are also wrapped by another type A, we're back to the first problem:A<B<String>> is not a subtype of A<B<?>>
(Notice B<?> is the SuperClaz and B<String> is the SubClaz in this case).
You can fix this the same way; by adding another wildcard: A<B<String>>() is a subtype of A<? extends B<?>>.
Keep in mind that this doesn't allow you to read or manipulate the list as you want. Search for covariance and contravariance for more detail. Here is a good one: http://bayou.io/draft/Capturing_Wildcards.html
add a comment |
List<SubClaz> is not a subtype of List<SuperClaz> in Java. That's why the wildcards are used: List<SubClaz> is a subtype of List<? extends SuperClaz>.
Now for your A<B<?>> abcv=new A<B<String>>(); example:
By adding the wildcard, you're making B<String> a subtype of B<?>, but since these are also wrapped by another type A, we're back to the first problem:A<B<String>> is not a subtype of A<B<?>>
(Notice B<?> is the SuperClaz and B<String> is the SubClaz in this case).
You can fix this the same way; by adding another wildcard: A<B<String>>() is a subtype of A<? extends B<?>>.
Keep in mind that this doesn't allow you to read or manipulate the list as you want. Search for covariance and contravariance for more detail. Here is a good one: http://bayou.io/draft/Capturing_Wildcards.html
add a comment |
List<SubClaz> is not a subtype of List<SuperClaz> in Java. That's why the wildcards are used: List<SubClaz> is a subtype of List<? extends SuperClaz>.
Now for your A<B<?>> abcv=new A<B<String>>(); example:
By adding the wildcard, you're making B<String> a subtype of B<?>, but since these are also wrapped by another type A, we're back to the first problem:A<B<String>> is not a subtype of A<B<?>>
(Notice B<?> is the SuperClaz and B<String> is the SubClaz in this case).
You can fix this the same way; by adding another wildcard: A<B<String>>() is a subtype of A<? extends B<?>>.
Keep in mind that this doesn't allow you to read or manipulate the list as you want. Search for covariance and contravariance for more detail. Here is a good one: http://bayou.io/draft/Capturing_Wildcards.html
List<SubClaz> is not a subtype of List<SuperClaz> in Java. That's why the wildcards are used: List<SubClaz> is a subtype of List<? extends SuperClaz>.
Now for your A<B<?>> abcv=new A<B<String>>(); example:
By adding the wildcard, you're making B<String> a subtype of B<?>, but since these are also wrapped by another type A, we're back to the first problem:A<B<String>> is not a subtype of A<B<?>>
(Notice B<?> is the SuperClaz and B<String> is the SubClaz in this case).
You can fix this the same way; by adding another wildcard: A<B<String>>() is a subtype of A<? extends B<?>>.
Keep in mind that this doesn't allow you to read or manipulate the list as you want. Search for covariance and contravariance for more detail. Here is a good one: http://bayou.io/draft/Capturing_Wildcards.html
answered Jan 2 at 16:48
oskansavlioskansavli
12614
12614
add a comment |
add a comment |
d should have type A<B<? extends Object>> or compatible.
1
It never will have that exact type though, because the?will refer to different types in the declaration ofdand the one in theList's elements.
– daniu
Jan 2 at 10:13
@daniu yes. It is why I said "or compatible". Should have think that there is no d with exact same type.
– talex
Jan 2 at 10:18
add a comment |
d should have type A<B<? extends Object>> or compatible.
1
It never will have that exact type though, because the?will refer to different types in the declaration ofdand the one in theList's elements.
– daniu
Jan 2 at 10:13
@daniu yes. It is why I said "or compatible". Should have think that there is no d with exact same type.
– talex
Jan 2 at 10:18
add a comment |
d should have type A<B<? extends Object>> or compatible.
d should have type A<B<? extends Object>> or compatible.
answered Jan 2 at 10:01
talextalex
11.4k11648
11.4k11648
1
It never will have that exact type though, because the?will refer to different types in the declaration ofdand the one in theList's elements.
– daniu
Jan 2 at 10:13
@daniu yes. It is why I said "or compatible". Should have think that there is no d with exact same type.
– talex
Jan 2 at 10:18
add a comment |
1
It never will have that exact type though, because the?will refer to different types in the declaration ofdand the one in theList's elements.
– daniu
Jan 2 at 10:13
@daniu yes. It is why I said "or compatible". Should have think that there is no d with exact same type.
– talex
Jan 2 at 10:18
1
1
It never will have that exact type though, because the
? will refer to different types in the declaration of d and the one in the List's elements.– daniu
Jan 2 at 10:13
It never will have that exact type though, because the
? will refer to different types in the declaration of d and the one in the List's elements.– daniu
Jan 2 at 10:13
@daniu yes. It is why I said "or compatible". Should have think that there is no d with exact same type.
– talex
Jan 2 at 10:18
@daniu yes. It is why I said "or compatible". Should have think that there is no d with exact same type.
– talex
Jan 2 at 10:18
add a comment |
List<A<B<?>>> rv = new LinkedList<>();
? in generic means any type.
For example:
You can assign any type of generic list to List<?> but you can not add any type of object into list because compiler does not know what type it is because of wildcard(?)
List<?> genericList1 = new ArrayList<String>();
List<?> genericList2 = new ArrayList<Integer>();
List<?> genericList3 = new ArrayList<X>();
/**
* Compiler will allow to assign any type of generic list to List<?>
* but it will not allow to add.
*/
genericList1.add("xyz"); // It will give compiler error
The <?> wildcard allows a list of ANY type to be assigned, but the add() method is not valid on that list because you could put the wrong kind of thing into the collection. Compiler does not know which type you would pass because of wildcard(?).
If you want to add any type of object to your list than you can take list like this.
List<Object> rv = new LinkedList<>();
rv.add(new A<B<X>>());
rv.add(new A<B<String>>());
rv.add(new A<B<Integer>>());
Now compiler knows that rv is the list which accept only Object type, so compiler will not complain anything.
add a comment |
List<A<B<?>>> rv = new LinkedList<>();
? in generic means any type.
For example:
You can assign any type of generic list to List<?> but you can not add any type of object into list because compiler does not know what type it is because of wildcard(?)
List<?> genericList1 = new ArrayList<String>();
List<?> genericList2 = new ArrayList<Integer>();
List<?> genericList3 = new ArrayList<X>();
/**
* Compiler will allow to assign any type of generic list to List<?>
* but it will not allow to add.
*/
genericList1.add("xyz"); // It will give compiler error
The <?> wildcard allows a list of ANY type to be assigned, but the add() method is not valid on that list because you could put the wrong kind of thing into the collection. Compiler does not know which type you would pass because of wildcard(?).
If you want to add any type of object to your list than you can take list like this.
List<Object> rv = new LinkedList<>();
rv.add(new A<B<X>>());
rv.add(new A<B<String>>());
rv.add(new A<B<Integer>>());
Now compiler knows that rv is the list which accept only Object type, so compiler will not complain anything.
add a comment |
List<A<B<?>>> rv = new LinkedList<>();
? in generic means any type.
For example:
You can assign any type of generic list to List<?> but you can not add any type of object into list because compiler does not know what type it is because of wildcard(?)
List<?> genericList1 = new ArrayList<String>();
List<?> genericList2 = new ArrayList<Integer>();
List<?> genericList3 = new ArrayList<X>();
/**
* Compiler will allow to assign any type of generic list to List<?>
* but it will not allow to add.
*/
genericList1.add("xyz"); // It will give compiler error
The <?> wildcard allows a list of ANY type to be assigned, but the add() method is not valid on that list because you could put the wrong kind of thing into the collection. Compiler does not know which type you would pass because of wildcard(?).
If you want to add any type of object to your list than you can take list like this.
List<Object> rv = new LinkedList<>();
rv.add(new A<B<X>>());
rv.add(new A<B<String>>());
rv.add(new A<B<Integer>>());
Now compiler knows that rv is the list which accept only Object type, so compiler will not complain anything.
List<A<B<?>>> rv = new LinkedList<>();
? in generic means any type.
For example:
You can assign any type of generic list to List<?> but you can not add any type of object into list because compiler does not know what type it is because of wildcard(?)
List<?> genericList1 = new ArrayList<String>();
List<?> genericList2 = new ArrayList<Integer>();
List<?> genericList3 = new ArrayList<X>();
/**
* Compiler will allow to assign any type of generic list to List<?>
* but it will not allow to add.
*/
genericList1.add("xyz"); // It will give compiler error
The <?> wildcard allows a list of ANY type to be assigned, but the add() method is not valid on that list because you could put the wrong kind of thing into the collection. Compiler does not know which type you would pass because of wildcard(?).
If you want to add any type of object to your list than you can take list like this.
List<Object> rv = new LinkedList<>();
rv.add(new A<B<X>>());
rv.add(new A<B<String>>());
rv.add(new A<B<Integer>>());
Now compiler knows that rv is the list which accept only Object type, so compiler will not complain anything.
answered Jan 3 at 5:13
sagar varsanisagar varsani
1831311
1831311
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54004281%2fadding-object-to-generic-list-with-two-types%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You need to create an object of type
A<B<? extends Object>>. This should be enough. What do you mean by "not working"?– JF Meier
Jan 2 at 9:59
2
You can't guarantee that
? extends Objectcan contain something of typeX. It could be something else entirely likeA<B<String>>.– TiiJ7
Jan 2 at 10:01
2
And note that
<? extends Object>is the same as<?>.– Andy Turner
Jan 2 at 10:02