Timeline for Java Stream API - Best way to transform a list: map or forEach?
Current License: CC BY-SA 4.0
42 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
S Aug 23, 2023 at 19:35 | history | suggested | Philippe Cloutier | CC BY-SA 4.0 |
update following Java 9's release
|
Aug 21, 2023 at 20:23 | review | Suggested edits | |||
S Aug 23, 2023 at 19:35 | |||||
Oct 30, 2022 at 18:33 | comment | added | John Gilmer |
With Java 17 map(elt -> doSomething(elt)).collect(Collectors.toList()); can be map(elt -> doSomething(elt)).toList()); The Java 17 version has the added benefit of returning an immutable list.
|
|
S May 16, 2021 at 12:05 | history | bounty ended | Naman | ||
S May 16, 2021 at 12:05 | history | notice removed | Naman | ||
S May 13, 2021 at 2:17 | history | bounty started | Naman | ||
S May 13, 2021 at 2:17 | history | notice added | Naman | Reward existing answer | |
Mar 6, 2018 at 16:16 | history | edited | herman | CC BY-SA 3.0 |
added instance creation for method 1 to not give the wrong impression that it is shorter
|
Mar 10, 2017 at 15:53 | answer | added | John McClean | timeline score: 0 | |
Jan 25, 2017 at 23:21 | comment | added | phil294 |
why not .filter().collect() ??
|
|
Jan 19, 2017 at 10:50 | history | edited | Eran | CC BY-SA 3.0 |
deleted 9 characters in body; edited tags
|
Apr 5, 2016 at 13:56 | answer | added | Kumar Abhishek | timeline score: 0 | |
Mar 12, 2015 at 2:16 | answer | added | harshtuna | timeline score: 0 | |
Mar 12, 2015 at 1:59 | comment | added | harshtuna | there's a third way (using toArray) - will add below for the sake of completeness, but the method 2 is preferrable | |
Mar 6, 2015 at 6:50 | audit | First posts | |||
Mar 6, 2015 at 7:18 | |||||
Mar 5, 2015 at 13:14 | audit | First posts | |||
Mar 5, 2015 at 13:14 | |||||
Mar 2, 2015 at 16:20 | audit | First posts | |||
Mar 2, 2015 at 16:20 | |||||
Feb 23, 2015 at 5:28 | audit | First posts | |||
Feb 23, 2015 at 5:35 | |||||
Feb 20, 2015 at 19:58 | audit | First posts | |||
Feb 20, 2015 at 21:00 | |||||
Feb 17, 2015 at 17:52 | audit | First posts | |||
Feb 17, 2015 at 17:53 | |||||
Feb 13, 2015 at 3:43 | audit | First posts | |||
Feb 13, 2015 at 3:44 | |||||
Feb 12, 2015 at 14:50 | audit | First posts | |||
Feb 12, 2015 at 14:51 | |||||
Feb 10, 2015 at 11:28 | audit | First posts | |||
Feb 10, 2015 at 11:28 | |||||
Feb 6, 2015 at 11:09 | audit | Reopen votes | |||
Feb 6, 2015 at 11:09 | |||||
Feb 5, 2015 at 22:21 | answer | added | Craig P. Motlin | timeline score: 6 | |
Feb 4, 2015 at 16:01 | vote | accept | Emilien Brigand | ||
Feb 4, 2015 at 14:14 | answer | added | user3248346 | timeline score: 5 | |
Feb 4, 2015 at 12:09 | answer | added | assylias | timeline score: 59 | |
Feb 4, 2015 at 11:55 | comment | added | herman |
Note that you can write .map(this::doSomething) assuming that doSomething is a non-static method. If it's static you can replace this with the class name.
|
|
S Feb 4, 2015 at 11:51 | history | suggested | herman | CC BY-SA 3.0 |
changed title: 'parse' is not really the correct term in this context
|
Feb 4, 2015 at 11:38 | review | Suggested edits | |||
S Feb 4, 2015 at 11:51 | |||||
Feb 4, 2015 at 11:05 | history | edited | Emilien Brigand | CC BY-SA 3.0 |
deleted 5 characters in body
|
Feb 4, 2015 at 10:51 | comment | added | Szymon Roziewski | @I.K. yes, that's true, not everytime parallelization is a good choice. But if your data is quite big, you can think about it. | |
Feb 4, 2015 at 10:49 | history | edited | tobias_k | CC BY-SA 3.0 |
code format, added java8 tag
|
Feb 4, 2015 at 10:48 | comment | added | user3248346 | @SzymonRoziewski, not quite. For something as trivial as this, the work needed to setup the parallelstream under the hood will make using this construct mute. | |
Feb 4, 2015 at 10:47 | comment | added | herman |
@the8472 Even better would be to make sure there are no null values in the collection in the first place, and use Optional<T> instead in combination with flatMap .
|
|
Feb 4, 2015 at 10:46 | comment | added | Szymon Roziewski |
You can use the parallelStream myFinalList = myListToParse.parallelStream().filter(elt -> elt != null).map(elt -> doSomething(elt)).collect(Collectors.toList()); to boost performance
|
|
Feb 4, 2015 at 10:40 | comment | added | the8472 |
just a matter of style, but elt -> elt != null can be replaced with Objects::nonNull
|
|
Feb 4, 2015 at 10:38 | answer | added | herman | timeline score: 225 | |
Feb 4, 2015 at 10:34 | answer | added | Eran | timeline score: 1 | |
Feb 4, 2015 at 10:32 | comment | added | ThanksForAllTheFish | The second one. A proper function should have no side effects, in your first implementation you are modifying the external world. | |
Feb 4, 2015 at 10:30 | history | asked | Emilien Brigand | CC BY-SA 3.0 |