Creating an ArrayList from a method which returns a String
I have an custom class InfoAQ
which has a method called public String getSeqInf()
. Now I have an ArrayList<InfoAQ> infList
and
I need an ArrayList<String>strList = new ArrayList<String>
with the content from getSeqInf()
for each element.
This is the way Im doing it right now ...
for(InfoAQ currentInf : infList)
strList.add(currentInf.getSeqInf());
Is there an alternative way to make it ? Maybe a faster one or one liner ?
java
add a comment |
I have an custom class InfoAQ
which has a method called public String getSeqInf()
. Now I have an ArrayList<InfoAQ> infList
and
I need an ArrayList<String>strList = new ArrayList<String>
with the content from getSeqInf()
for each element.
This is the way Im doing it right now ...
for(InfoAQ currentInf : infList)
strList.add(currentInf.getSeqInf());
Is there an alternative way to make it ? Maybe a faster one or one liner ?
java
1
Possible duplicate of Lambda expression to convert array/List of String to array/List of Integers
– Sven Hakvoort
Nov 21 '18 at 13:14
Try this --> List<String> outputList = list.stream().map(it -> it.getSeqInf()).collect(Collectors.toList());
– Suryavel TR
Nov 21 '18 at 13:15
1
What you wrote can be one line. Just delete the new line you added.
– matt
Nov 21 '18 at 13:21
add a comment |
I have an custom class InfoAQ
which has a method called public String getSeqInf()
. Now I have an ArrayList<InfoAQ> infList
and
I need an ArrayList<String>strList = new ArrayList<String>
with the content from getSeqInf()
for each element.
This is the way Im doing it right now ...
for(InfoAQ currentInf : infList)
strList.add(currentInf.getSeqInf());
Is there an alternative way to make it ? Maybe a faster one or one liner ?
java
I have an custom class InfoAQ
which has a method called public String getSeqInf()
. Now I have an ArrayList<InfoAQ> infList
and
I need an ArrayList<String>strList = new ArrayList<String>
with the content from getSeqInf()
for each element.
This is the way Im doing it right now ...
for(InfoAQ currentInf : infList)
strList.add(currentInf.getSeqInf());
Is there an alternative way to make it ? Maybe a faster one or one liner ?
java
java
asked Nov 21 '18 at 13:11
Ahmet KazamanAhmet Kazaman
352324
352324
1
Possible duplicate of Lambda expression to convert array/List of String to array/List of Integers
– Sven Hakvoort
Nov 21 '18 at 13:14
Try this --> List<String> outputList = list.stream().map(it -> it.getSeqInf()).collect(Collectors.toList());
– Suryavel TR
Nov 21 '18 at 13:15
1
What you wrote can be one line. Just delete the new line you added.
– matt
Nov 21 '18 at 13:21
add a comment |
1
Possible duplicate of Lambda expression to convert array/List of String to array/List of Integers
– Sven Hakvoort
Nov 21 '18 at 13:14
Try this --> List<String> outputList = list.stream().map(it -> it.getSeqInf()).collect(Collectors.toList());
– Suryavel TR
Nov 21 '18 at 13:15
1
What you wrote can be one line. Just delete the new line you added.
– matt
Nov 21 '18 at 13:21
1
1
Possible duplicate of Lambda expression to convert array/List of String to array/List of Integers
– Sven Hakvoort
Nov 21 '18 at 13:14
Possible duplicate of Lambda expression to convert array/List of String to array/List of Integers
– Sven Hakvoort
Nov 21 '18 at 13:14
Try this --> List<String> outputList = list.stream().map(it -> it.getSeqInf()).collect(Collectors.toList());
– Suryavel TR
Nov 21 '18 at 13:15
Try this --> List<String> outputList = list.stream().map(it -> it.getSeqInf()).collect(Collectors.toList());
– Suryavel TR
Nov 21 '18 at 13:15
1
1
What you wrote can be one line. Just delete the new line you added.
– matt
Nov 21 '18 at 13:21
What you wrote can be one line. Just delete the new line you added.
– matt
Nov 21 '18 at 13:21
add a comment |
5 Answers
5
active
oldest
votes
Yes, there is:
strList = infList.stream().map(e -> g.getSeqInf()).collect(Collectors.toList());
The map
step can be also written in another way:
strList = infList.stream().map(InfoAQ::getSeqInf).collect(Collectors.toList());
which is know as method reference passing. Those two solutions are equivalent.
4
InfoAQ::getSeqInf ?
– matt
Nov 21 '18 at 13:15
1
It is an equivalent way for map. Good suggestion.
– Lorelorelore
Nov 21 '18 at 13:18
add a comment |
Also maybe this one:
List<String> strList = new ArrayList<String>();
infList.forEach(e -> strList.add(e.getSeqInf()));
add a comment |
And there is another one (-liner, if you format it in a single line):
infList.forEach(currentInf -> {strList.add(currentInf.getSeqInf());});
while I would prefer a formatting in more lines:
infList.forEach(currentInf -> {
strList.add(currentInf.getSeqInf());
});
add a comment |
Using streams
infList.stream()
.map(InfoAQ::getSeqInf)
.collect(Collectors.toCollection(ArrayList::new))
Using Collectors.toCollection
here to create an ArrayList
that will hold the results as you do in your case. (Important if you do care about the result list type as Collectors.toList()
does not guarantee this)
May not be the fastest as using stream has some overhead. You need to measure/benchmark to find out its performance
add a comment |
This code will iterate all the data in the list, as getSeqInf returns a String, the collect method will store all returns of the getSeqInf method in a list.
`List listString = infList.stream().map(InfoAQ::getSeqInf).collect(Collectors.toList());`
or
`
ArrayList<String> listString = new ArrayList<>();
for(int i = 0; i < infoAq.size(); i++) {
listString.add(infoAq.get(i).getSeqInf());
}`
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%2f53412813%2fcreating-an-arraylist-from-a-method-which-returns-a-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
Yes, there is:
strList = infList.stream().map(e -> g.getSeqInf()).collect(Collectors.toList());
The map
step can be also written in another way:
strList = infList.stream().map(InfoAQ::getSeqInf).collect(Collectors.toList());
which is know as method reference passing. Those two solutions are equivalent.
4
InfoAQ::getSeqInf ?
– matt
Nov 21 '18 at 13:15
1
It is an equivalent way for map. Good suggestion.
– Lorelorelore
Nov 21 '18 at 13:18
add a comment |
Yes, there is:
strList = infList.stream().map(e -> g.getSeqInf()).collect(Collectors.toList());
The map
step can be also written in another way:
strList = infList.stream().map(InfoAQ::getSeqInf).collect(Collectors.toList());
which is know as method reference passing. Those two solutions are equivalent.
4
InfoAQ::getSeqInf ?
– matt
Nov 21 '18 at 13:15
1
It is an equivalent way for map. Good suggestion.
– Lorelorelore
Nov 21 '18 at 13:18
add a comment |
Yes, there is:
strList = infList.stream().map(e -> g.getSeqInf()).collect(Collectors.toList());
The map
step can be also written in another way:
strList = infList.stream().map(InfoAQ::getSeqInf).collect(Collectors.toList());
which is know as method reference passing. Those two solutions are equivalent.
Yes, there is:
strList = infList.stream().map(e -> g.getSeqInf()).collect(Collectors.toList());
The map
step can be also written in another way:
strList = infList.stream().map(InfoAQ::getSeqInf).collect(Collectors.toList());
which is know as method reference passing. Those two solutions are equivalent.
edited Nov 21 '18 at 13:19
answered Nov 21 '18 at 13:14


LoreloreloreLorelorelore
2,02761527
2,02761527
4
InfoAQ::getSeqInf ?
– matt
Nov 21 '18 at 13:15
1
It is an equivalent way for map. Good suggestion.
– Lorelorelore
Nov 21 '18 at 13:18
add a comment |
4
InfoAQ::getSeqInf ?
– matt
Nov 21 '18 at 13:15
1
It is an equivalent way for map. Good suggestion.
– Lorelorelore
Nov 21 '18 at 13:18
4
4
InfoAQ::getSeqInf ?
– matt
Nov 21 '18 at 13:15
InfoAQ::getSeqInf ?
– matt
Nov 21 '18 at 13:15
1
1
It is an equivalent way for map. Good suggestion.
– Lorelorelore
Nov 21 '18 at 13:18
It is an equivalent way for map. Good suggestion.
– Lorelorelore
Nov 21 '18 at 13:18
add a comment |
Also maybe this one:
List<String> strList = new ArrayList<String>();
infList.forEach(e -> strList.add(e.getSeqInf()));
add a comment |
Also maybe this one:
List<String> strList = new ArrayList<String>();
infList.forEach(e -> strList.add(e.getSeqInf()));
add a comment |
Also maybe this one:
List<String> strList = new ArrayList<String>();
infList.forEach(e -> strList.add(e.getSeqInf()));
Also maybe this one:
List<String> strList = new ArrayList<String>();
infList.forEach(e -> strList.add(e.getSeqInf()));
answered Nov 21 '18 at 13:18
PulszarPulszar
44426
44426
add a comment |
add a comment |
And there is another one (-liner, if you format it in a single line):
infList.forEach(currentInf -> {strList.add(currentInf.getSeqInf());});
while I would prefer a formatting in more lines:
infList.forEach(currentInf -> {
strList.add(currentInf.getSeqInf());
});
add a comment |
And there is another one (-liner, if you format it in a single line):
infList.forEach(currentInf -> {strList.add(currentInf.getSeqInf());});
while I would prefer a formatting in more lines:
infList.forEach(currentInf -> {
strList.add(currentInf.getSeqInf());
});
add a comment |
And there is another one (-liner, if you format it in a single line):
infList.forEach(currentInf -> {strList.add(currentInf.getSeqInf());});
while I would prefer a formatting in more lines:
infList.forEach(currentInf -> {
strList.add(currentInf.getSeqInf());
});
And there is another one (-liner, if you format it in a single line):
infList.forEach(currentInf -> {strList.add(currentInf.getSeqInf());});
while I would prefer a formatting in more lines:
infList.forEach(currentInf -> {
strList.add(currentInf.getSeqInf());
});
answered Nov 21 '18 at 13:19


deHaardeHaar
2,45451628
2,45451628
add a comment |
add a comment |
Using streams
infList.stream()
.map(InfoAQ::getSeqInf)
.collect(Collectors.toCollection(ArrayList::new))
Using Collectors.toCollection
here to create an ArrayList
that will hold the results as you do in your case. (Important if you do care about the result list type as Collectors.toList()
does not guarantee this)
May not be the fastest as using stream has some overhead. You need to measure/benchmark to find out its performance
add a comment |
Using streams
infList.stream()
.map(InfoAQ::getSeqInf)
.collect(Collectors.toCollection(ArrayList::new))
Using Collectors.toCollection
here to create an ArrayList
that will hold the results as you do in your case. (Important if you do care about the result list type as Collectors.toList()
does not guarantee this)
May not be the fastest as using stream has some overhead. You need to measure/benchmark to find out its performance
add a comment |
Using streams
infList.stream()
.map(InfoAQ::getSeqInf)
.collect(Collectors.toCollection(ArrayList::new))
Using Collectors.toCollection
here to create an ArrayList
that will hold the results as you do in your case. (Important if you do care about the result list type as Collectors.toList()
does not guarantee this)
May not be the fastest as using stream has some overhead. You need to measure/benchmark to find out its performance
Using streams
infList.stream()
.map(InfoAQ::getSeqInf)
.collect(Collectors.toCollection(ArrayList::new))
Using Collectors.toCollection
here to create an ArrayList
that will hold the results as you do in your case. (Important if you do care about the result list type as Collectors.toList()
does not guarantee this)
May not be the fastest as using stream has some overhead. You need to measure/benchmark to find out its performance
edited Nov 21 '18 at 13:21
answered Nov 21 '18 at 13:15
user7user7
9,55932443
9,55932443
add a comment |
add a comment |
This code will iterate all the data in the list, as getSeqInf returns a String, the collect method will store all returns of the getSeqInf method in a list.
`List listString = infList.stream().map(InfoAQ::getSeqInf).collect(Collectors.toList());`
or
`
ArrayList<String> listString = new ArrayList<>();
for(int i = 0; i < infoAq.size(); i++) {
listString.add(infoAq.get(i).getSeqInf());
}`
add a comment |
This code will iterate all the data in the list, as getSeqInf returns a String, the collect method will store all returns of the getSeqInf method in a list.
`List listString = infList.stream().map(InfoAQ::getSeqInf).collect(Collectors.toList());`
or
`
ArrayList<String> listString = new ArrayList<>();
for(int i = 0; i < infoAq.size(); i++) {
listString.add(infoAq.get(i).getSeqInf());
}`
add a comment |
This code will iterate all the data in the list, as getSeqInf returns a String, the collect method will store all returns of the getSeqInf method in a list.
`List listString = infList.stream().map(InfoAQ::getSeqInf).collect(Collectors.toList());`
or
`
ArrayList<String> listString = new ArrayList<>();
for(int i = 0; i < infoAq.size(); i++) {
listString.add(infoAq.get(i).getSeqInf());
}`
This code will iterate all the data in the list, as getSeqInf returns a String, the collect method will store all returns of the getSeqInf method in a list.
`List listString = infList.stream().map(InfoAQ::getSeqInf).collect(Collectors.toList());`
or
`
ArrayList<String> listString = new ArrayList<>();
for(int i = 0; i < infoAq.size(); i++) {
listString.add(infoAq.get(i).getSeqInf());
}`
answered Nov 21 '18 at 13:28


Marcelo Lubanco ThoméMarcelo Lubanco Thomé
111
111
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%2f53412813%2fcreating-an-arraylist-from-a-method-which-returns-a-string%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
1
Possible duplicate of Lambda expression to convert array/List of String to array/List of Integers
– Sven Hakvoort
Nov 21 '18 at 13:14
Try this --> List<String> outputList = list.stream().map(it -> it.getSeqInf()).collect(Collectors.toList());
– Suryavel TR
Nov 21 '18 at 13:15
1
What you wrote can be one line. Just delete the new line you added.
– matt
Nov 21 '18 at 13:21