Generic method to copy a list into a list of lists in Java
I'm trying to create a method as much generic as possible to copy a List into a List of Lists. I've tried with this one:
public static <E> void addToMatrix(List<E> list, List<List<? extends E>> matrix) {
matrix.add(list);
}
Here is the main I've used to test the method:
public static void main(String args) {
List< List<? extends Number> > matrix = new ArrayList<>();
List<Integer> list = new ArrayList<>();
addToMatrix(list, matrix);
}
Why it is not working ?
How can I do this in a proper way without changing the logic of the method ?
java generics wildcard
add a comment |
I'm trying to create a method as much generic as possible to copy a List into a List of Lists. I've tried with this one:
public static <E> void addToMatrix(List<E> list, List<List<? extends E>> matrix) {
matrix.add(list);
}
Here is the main I've used to test the method:
public static void main(String args) {
List< List<? extends Number> > matrix = new ArrayList<>();
List<Integer> list = new ArrayList<>();
addToMatrix(list, matrix);
}
Why it is not working ?
How can I do this in a proper way without changing the logic of the method ?
java generics wildcard
Is there an issue you're having with the current method?
– Joe C
Nov 25 '18 at 15:28
I'm getting the following error on compile time: The method addToMatrix(List<E>, List<List<? extends E>>) in the type Main is not applicable for the arguments (List<Integer>, List<List<? extends Number>>)
– Aleksandar Stojkovski
Nov 25 '18 at 15:39
Why do you need a method? Callingmatrix.add(list)
directly seems to do what you need.
– Andy Turner
Nov 25 '18 at 16:27
add a comment |
I'm trying to create a method as much generic as possible to copy a List into a List of Lists. I've tried with this one:
public static <E> void addToMatrix(List<E> list, List<List<? extends E>> matrix) {
matrix.add(list);
}
Here is the main I've used to test the method:
public static void main(String args) {
List< List<? extends Number> > matrix = new ArrayList<>();
List<Integer> list = new ArrayList<>();
addToMatrix(list, matrix);
}
Why it is not working ?
How can I do this in a proper way without changing the logic of the method ?
java generics wildcard
I'm trying to create a method as much generic as possible to copy a List into a List of Lists. I've tried with this one:
public static <E> void addToMatrix(List<E> list, List<List<? extends E>> matrix) {
matrix.add(list);
}
Here is the main I've used to test the method:
public static void main(String args) {
List< List<? extends Number> > matrix = new ArrayList<>();
List<Integer> list = new ArrayList<>();
addToMatrix(list, matrix);
}
Why it is not working ?
How can I do this in a proper way without changing the logic of the method ?
java generics wildcard
java generics wildcard
edited Nov 25 '18 at 15:36
Aleksandar Stojkovski
asked Nov 25 '18 at 15:26
Aleksandar StojkovskiAleksandar Stojkovski
133
133
Is there an issue you're having with the current method?
– Joe C
Nov 25 '18 at 15:28
I'm getting the following error on compile time: The method addToMatrix(List<E>, List<List<? extends E>>) in the type Main is not applicable for the arguments (List<Integer>, List<List<? extends Number>>)
– Aleksandar Stojkovski
Nov 25 '18 at 15:39
Why do you need a method? Callingmatrix.add(list)
directly seems to do what you need.
– Andy Turner
Nov 25 '18 at 16:27
add a comment |
Is there an issue you're having with the current method?
– Joe C
Nov 25 '18 at 15:28
I'm getting the following error on compile time: The method addToMatrix(List<E>, List<List<? extends E>>) in the type Main is not applicable for the arguments (List<Integer>, List<List<? extends Number>>)
– Aleksandar Stojkovski
Nov 25 '18 at 15:39
Why do you need a method? Callingmatrix.add(list)
directly seems to do what you need.
– Andy Turner
Nov 25 '18 at 16:27
Is there an issue you're having with the current method?
– Joe C
Nov 25 '18 at 15:28
Is there an issue you're having with the current method?
– Joe C
Nov 25 '18 at 15:28
I'm getting the following error on compile time: The method addToMatrix(List<E>, List<List<? extends E>>) in the type Main is not applicable for the arguments (List<Integer>, List<List<? extends Number>>)
– Aleksandar Stojkovski
Nov 25 '18 at 15:39
I'm getting the following error on compile time: The method addToMatrix(List<E>, List<List<? extends E>>) in the type Main is not applicable for the arguments (List<Integer>, List<List<? extends Number>>)
– Aleksandar Stojkovski
Nov 25 '18 at 15:39
Why do you need a method? Calling
matrix.add(list)
directly seems to do what you need.– Andy Turner
Nov 25 '18 at 16:27
Why do you need a method? Calling
matrix.add(list)
directly seems to do what you need.– Andy Turner
Nov 25 '18 at 16:27
add a comment |
2 Answers
2
active
oldest
votes
I think the most general you can make it is like this (replace "E" with "? extends E" in the first List parameter):
public static <E> void addToMatrix(List<? extends E> list, List<List<? extends E>> matrix)
{
matrix.add(list);
}
This compiles and allows your method to accept any subtypes of E (including Integer and another subtype of Number).
Could you please explain why it is working, and what is wrong with my implementation ?
– Aleksandar Stojkovski
Nov 25 '18 at 15:58
@AleksandarStojkovski when you put in "? extends E" you are telling your program to expect a subtype of E. In your case, you are expecting a subtype of Number. But then when you are also requesting to initiate a list of integer (List<Integer>) the compiler gets confused, since it assigns Integer to E and then expects one of E's subtypes in List<List<? extends E>>. Problem is that a subtype of Number (as in "? extends Number") will not necessarily be a subtype of Integer too, so the compiler complains.
– Elia Anagnostou
Nov 25 '18 at 16:04
add a comment |
You can also change your method definition to:
public static <E> void addToMatrix(List<? extends E> list, List<List<? extends E>> matrix) {
matrix.add(list);
}
since the add
API for List<E>
expects the argument E e
which in your case is list
of type List<? extends E>
.
This seems to work. Could you please explain why it is working, and what is wrong with my implementation ?
– Aleksandar Stojkovski
Nov 25 '18 at 15:54
@AleksandarStojkovski have added the details of what theList.add
API contracts are.
– Naman
Nov 25 '18 at 15:55
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%2f53468984%2fgeneric-method-to-copy-a-list-into-a-list-of-lists-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think the most general you can make it is like this (replace "E" with "? extends E" in the first List parameter):
public static <E> void addToMatrix(List<? extends E> list, List<List<? extends E>> matrix)
{
matrix.add(list);
}
This compiles and allows your method to accept any subtypes of E (including Integer and another subtype of Number).
Could you please explain why it is working, and what is wrong with my implementation ?
– Aleksandar Stojkovski
Nov 25 '18 at 15:58
@AleksandarStojkovski when you put in "? extends E" you are telling your program to expect a subtype of E. In your case, you are expecting a subtype of Number. But then when you are also requesting to initiate a list of integer (List<Integer>) the compiler gets confused, since it assigns Integer to E and then expects one of E's subtypes in List<List<? extends E>>. Problem is that a subtype of Number (as in "? extends Number") will not necessarily be a subtype of Integer too, so the compiler complains.
– Elia Anagnostou
Nov 25 '18 at 16:04
add a comment |
I think the most general you can make it is like this (replace "E" with "? extends E" in the first List parameter):
public static <E> void addToMatrix(List<? extends E> list, List<List<? extends E>> matrix)
{
matrix.add(list);
}
This compiles and allows your method to accept any subtypes of E (including Integer and another subtype of Number).
Could you please explain why it is working, and what is wrong with my implementation ?
– Aleksandar Stojkovski
Nov 25 '18 at 15:58
@AleksandarStojkovski when you put in "? extends E" you are telling your program to expect a subtype of E. In your case, you are expecting a subtype of Number. But then when you are also requesting to initiate a list of integer (List<Integer>) the compiler gets confused, since it assigns Integer to E and then expects one of E's subtypes in List<List<? extends E>>. Problem is that a subtype of Number (as in "? extends Number") will not necessarily be a subtype of Integer too, so the compiler complains.
– Elia Anagnostou
Nov 25 '18 at 16:04
add a comment |
I think the most general you can make it is like this (replace "E" with "? extends E" in the first List parameter):
public static <E> void addToMatrix(List<? extends E> list, List<List<? extends E>> matrix)
{
matrix.add(list);
}
This compiles and allows your method to accept any subtypes of E (including Integer and another subtype of Number).
I think the most general you can make it is like this (replace "E" with "? extends E" in the first List parameter):
public static <E> void addToMatrix(List<? extends E> list, List<List<? extends E>> matrix)
{
matrix.add(list);
}
This compiles and allows your method to accept any subtypes of E (including Integer and another subtype of Number).
answered Nov 25 '18 at 15:51
Elia AnagnostouElia Anagnostou
261
261
Could you please explain why it is working, and what is wrong with my implementation ?
– Aleksandar Stojkovski
Nov 25 '18 at 15:58
@AleksandarStojkovski when you put in "? extends E" you are telling your program to expect a subtype of E. In your case, you are expecting a subtype of Number. But then when you are also requesting to initiate a list of integer (List<Integer>) the compiler gets confused, since it assigns Integer to E and then expects one of E's subtypes in List<List<? extends E>>. Problem is that a subtype of Number (as in "? extends Number") will not necessarily be a subtype of Integer too, so the compiler complains.
– Elia Anagnostou
Nov 25 '18 at 16:04
add a comment |
Could you please explain why it is working, and what is wrong with my implementation ?
– Aleksandar Stojkovski
Nov 25 '18 at 15:58
@AleksandarStojkovski when you put in "? extends E" you are telling your program to expect a subtype of E. In your case, you are expecting a subtype of Number. But then when you are also requesting to initiate a list of integer (List<Integer>) the compiler gets confused, since it assigns Integer to E and then expects one of E's subtypes in List<List<? extends E>>. Problem is that a subtype of Number (as in "? extends Number") will not necessarily be a subtype of Integer too, so the compiler complains.
– Elia Anagnostou
Nov 25 '18 at 16:04
Could you please explain why it is working, and what is wrong with my implementation ?
– Aleksandar Stojkovski
Nov 25 '18 at 15:58
Could you please explain why it is working, and what is wrong with my implementation ?
– Aleksandar Stojkovski
Nov 25 '18 at 15:58
@AleksandarStojkovski when you put in "? extends E" you are telling your program to expect a subtype of E. In your case, you are expecting a subtype of Number. But then when you are also requesting to initiate a list of integer (List<Integer>) the compiler gets confused, since it assigns Integer to E and then expects one of E's subtypes in List<List<? extends E>>. Problem is that a subtype of Number (as in "? extends Number") will not necessarily be a subtype of Integer too, so the compiler complains.
– Elia Anagnostou
Nov 25 '18 at 16:04
@AleksandarStojkovski when you put in "? extends E" you are telling your program to expect a subtype of E. In your case, you are expecting a subtype of Number. But then when you are also requesting to initiate a list of integer (List<Integer>) the compiler gets confused, since it assigns Integer to E and then expects one of E's subtypes in List<List<? extends E>>. Problem is that a subtype of Number (as in "? extends Number") will not necessarily be a subtype of Integer too, so the compiler complains.
– Elia Anagnostou
Nov 25 '18 at 16:04
add a comment |
You can also change your method definition to:
public static <E> void addToMatrix(List<? extends E> list, List<List<? extends E>> matrix) {
matrix.add(list);
}
since the add
API for List<E>
expects the argument E e
which in your case is list
of type List<? extends E>
.
This seems to work. Could you please explain why it is working, and what is wrong with my implementation ?
– Aleksandar Stojkovski
Nov 25 '18 at 15:54
@AleksandarStojkovski have added the details of what theList.add
API contracts are.
– Naman
Nov 25 '18 at 15:55
add a comment |
You can also change your method definition to:
public static <E> void addToMatrix(List<? extends E> list, List<List<? extends E>> matrix) {
matrix.add(list);
}
since the add
API for List<E>
expects the argument E e
which in your case is list
of type List<? extends E>
.
This seems to work. Could you please explain why it is working, and what is wrong with my implementation ?
– Aleksandar Stojkovski
Nov 25 '18 at 15:54
@AleksandarStojkovski have added the details of what theList.add
API contracts are.
– Naman
Nov 25 '18 at 15:55
add a comment |
You can also change your method definition to:
public static <E> void addToMatrix(List<? extends E> list, List<List<? extends E>> matrix) {
matrix.add(list);
}
since the add
API for List<E>
expects the argument E e
which in your case is list
of type List<? extends E>
.
You can also change your method definition to:
public static <E> void addToMatrix(List<? extends E> list, List<List<? extends E>> matrix) {
matrix.add(list);
}
since the add
API for List<E>
expects the argument E e
which in your case is list
of type List<? extends E>
.
answered Nov 25 '18 at 15:48
NamanNaman
44.4k11102204
44.4k11102204
This seems to work. Could you please explain why it is working, and what is wrong with my implementation ?
– Aleksandar Stojkovski
Nov 25 '18 at 15:54
@AleksandarStojkovski have added the details of what theList.add
API contracts are.
– Naman
Nov 25 '18 at 15:55
add a comment |
This seems to work. Could you please explain why it is working, and what is wrong with my implementation ?
– Aleksandar Stojkovski
Nov 25 '18 at 15:54
@AleksandarStojkovski have added the details of what theList.add
API contracts are.
– Naman
Nov 25 '18 at 15:55
This seems to work. Could you please explain why it is working, and what is wrong with my implementation ?
– Aleksandar Stojkovski
Nov 25 '18 at 15:54
This seems to work. Could you please explain why it is working, and what is wrong with my implementation ?
– Aleksandar Stojkovski
Nov 25 '18 at 15:54
@AleksandarStojkovski have added the details of what the
List.add
API contracts are.– Naman
Nov 25 '18 at 15:55
@AleksandarStojkovski have added the details of what the
List.add
API contracts are.– Naman
Nov 25 '18 at 15:55
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%2f53468984%2fgeneric-method-to-copy-a-list-into-a-list-of-lists-in-java%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
Is there an issue you're having with the current method?
– Joe C
Nov 25 '18 at 15:28
I'm getting the following error on compile time: The method addToMatrix(List<E>, List<List<? extends E>>) in the type Main is not applicable for the arguments (List<Integer>, List<List<? extends Number>>)
– Aleksandar Stojkovski
Nov 25 '18 at 15:39
Why do you need a method? Calling
matrix.add(list)
directly seems to do what you need.– Andy Turner
Nov 25 '18 at 16:27