Reputation: 15
I am learning Java 8 and came across a situation. Where in I have to iterate over a list of strings and then convert them to upperCase. The possible solutions would be to stream the list. Among many suggestions from Intellij the below two seems to be useful.
list.stream()
.map(String::toUpperCase)
or
list.stream().
forEach(p -> p.toUpperCase())
I am confused on which one to use and the use cases for all the Suggestions. Can I get help regarding which method to use and how to understand using all those suggestions?
Upvotes: 1
Views: 292
Reputation: 50776
Stream.map()
will never run unless you end the pipeline in a terminal operation, like forEach()
. But calling toUpperCase()
in a forEach()
won't do anything either, because strings are immutable. String.toUpperCase()
doesn't change the string; it returns a new one.
If you just want to update the list in-place, you can use
list.replaceAll(String::toUpperCase);
which actually replaces each element with the result of the passed function.
If you want the results in a new list, use the map()
snippet with a collector:
List<String> list2 = list.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
Upvotes: 6
Reputation: 2651
forEach
is an terminal operation that makes a difference through side effects. map
is a non-terminal operation that makes a direct mapping from one element to another. For example, here is a canonical usage of forEach
:
stream.forEach(System.out::println);
This will invoke, on each element of the stream, the equivalent of System.out.println(element);
. However, the stream will be closed after this, and no operations may be executed on stream
afterwards. map
, on the other hand, may be used like this:
streamOfObjects.map(Object::toString).collect(Collectors.toList());
In this case, each Object within streamOfObjects
is mapped to a String, created by invocation of toString
. Then, the stream of Strings produced by map
is collected into a List using a Collector.
In any case, I'd suggest using replaceAll
for this use case, as suggested by @shmosel.
As for how to understand suggestions provided by autocomplete, I would strongly suggest reading JavaDocs on the related classes.
Upvotes: 1