Java8 list conversion with Stream map

We will compare the list conversion when using the for statement and when using the Stream map. Use the list below.

stream-map.java


final List<String> months = 
Arrays.asList("January", "February", "March", "April", "May", "June", "July", "Augast", "September", "October", "November", "December");

Make a list consisting of only the first letter of each element.

1. Use for statement

We declare an empty list to store the first character and store it character by character in the for loop.

for.java


final List<String> firstChars = new ArrayList<>();
for (String c : months) {
  firstChars.add(c.substring(0,1));
}
for (String fc : firstChars) {
  System.out.print(fc);
}

The output is as follows. JFMAMJJASOND

2. Use the forEach statement

Although it has been rewritten to a functional type, an empty list must be prepared by the programmer.

forEach.java


final List<String> firstChars = new ArrayList<>();
months.forEach(c -> firstChars.add(c.substring(0,1)));
firstChars.forEach(System.out::print);

3. Use Stream map ()

There is no step to prepare an empty list, and the result of extracting the first character from the list appears to be stored in the new list as is. Stream's map () method converts contiguous inputs into contiguous outputs.

stream-map.java


final List<String> firstChars = months.stream().map(c -> c.substring(0,1)).collect(Collectors.toList());
firstChars3.forEach(System.out::print);

The map () method guarantees that the list of inputs and the list of outputs have the same number of elements, but not necessarily for the data types. If you want to get the number of characters of each element, it is converted from the character string type to the numeric type and output.

The following does not prepare the converted list, but directly outputs the contents of the element with forEach. If that is the case, it can be described in one line.

stream-map2.java


months.stream().map(c -> c.length()).forEach(cnt -> System.out.print(cnt + " "));
//Output as 7 8 5 5 3 4 4 6 9 7 8 8

Recommended Posts

Java8 list conversion with Stream map
Java array / list / stream mutual conversion list
[Java] Stream API / map
Endian conversion with JAVA
[Java] List type / Array type conversion
List processing to understand with pictures --java8 stream / javaslang-
[Java 8] Duplicate deletion (& duplicate check) with Stream
[Java] Stream (filter, map, forEach, reduce)
[Java] Element existence check with Stream
JAVA (Map)
Make a list map with LazyMap
[Java] Convert 1-to-N List to Map
List processing to understand with pictures --java8 stream / javaslang --bonus
[Java] Conversion from array to List
Full-width → half-width conversion with Java String (full-width kana → half-width kana)
Immutable (immutable) List object conversion function in Java8
Java type conversion
List, Set, Map
[JAVA] Stream type
Java memorandum (list)
Clone Java List.
Array / list / map
[Java] Map comparison
Java Stream termination
About List [Java]
Java 9 Optional :: stream
Make Java Stream line breaks nice with eclipse
[Java] How to operate List using Stream API
[java] sort in list
Install java with Homebrew
[Java] Full-width ⇔ half-width conversion
[Java] Stream Collectors notes
Install Java with Ansible
[Java] Stream API-Stream generation
I learned stream (I want to convert List to Map <Integer, List>)
Stream API map method
Comfortable download with JAVA
Switch java with direnv
About Java Array List
Enum reverse map Java
Java8 Stream API practice
[Java] Get the file path in the folder with List
Java bidirectional map library
Download Java with Ansible
Let's scrape with Java! !!
Getting Started with Legacy Java Engineers (Stream + Lambda Expression)
Build Java with Wercker
I want to get along with Map [Java beginner]
[Memo] Java Linked List
Output FizzBuzz with stream
Java8 Stream Rough Summary
[Java] Date type conversion
Integer check method with ruby
Make a list map with LazyMap
Check when moss with SimpleDateFormat parse
Java8 list conversion with Stream map
Only the top level Stream can be parallelized with Java Stream.
How to handle exceptions coolly with Java 8 Stream or Optional
Handle exceptions coolly with Java 8 lambda expressions and Stream API
I want to make a list with kotlin and java!
Rethinking design patterns with Java8 lambda expressions & Stream --Builder pattern -
About the behavior when doing a file map with java
For Java beginners: List, Map, Iterator / Array ... How to convert?
Convert 2D array to csv format with Java 8 Stream API