[JAVA] Difference between Stream map and flatMap

Java 8 Stream has map () and flatMap (). Let's examine the difference.

Conclusion

First of all, from the conclusion -Map (T-> R) is a 1: 1 conversion of the T data type to the R data type. -FlatMap (T-> Stream <R>) is a 1: N conversion from the data type of T toStream <R>.

Stream#map()

List<String> nameList = Arrays.asList("Tanaka", "Suzuki", "Takahashi");
Stream<Integer> stream = nameList.stream().map(x -> x.length());
System.out.println(stream.collect(Collectors.toList()));

The above execution result:

[6, 6, 9]
Tanaka Suzuki Takahashi
6 6 9

It is a 1: 1 conversion from a character string (String) to a character string length (Integer) as described above.

Stream#flatMap()

List<String> nameList = Arrays.asList("Tanaka", "Suzuki", "Takahashi");
Stream<Object> stream = nameList.stream().flatMap(x -> Stream.of(x, x.length()));
System.out.println((stream.collect(Collectors.toList())));

The above execution result:

[Tanaka, 6, Suzuki, 6, Takahashi, 9]
Tanaka Suzuki Takahashi
Tanaka, 6 Suzuki, 6 Takahashi, 9

1: N conversion from character string (String) to [character string (String), character string length (Integer)]. However, in that case, a two-dimensional array is not generated and a flat Stream is returned.

Recommended Posts

Difference between Stream map and flatMap
Difference between vh and%
Difference between i ++ and ++ i
Toward understanding of map and flatmap in Stream (1)
Difference between product and variant
Difference between redirect_to and render
[Java] Difference between == and equals
Rails: Difference between resources and resources
Difference between redirect_to and render
Difference between CUI and GUI
Difference between variables and instance variables
Difference between mockito-core and mockito-all
Difference between class and instance
Difference between bundle and bundle install
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between List and ArrayList
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
[Ruby] Difference between get and post
Difference between instance method and class method
Difference between render method and redirect_to
Difference between interface and abstract class
Difference between == operator and equals method
[Java] Difference between Hashmap and HashTable
[Terminal] Difference between irb and pry
JavaServlet: Difference between executeQuery and executeUpdate
[Ruby] Difference between is_a? And instance_of?
Difference between == operator and eqals method
Rough difference between RSpec and minitest
[Rails] Difference between find and find_by
Understand the difference between each_with_index and each.with_index
Difference between instance variable and class variable
[JAVA] Difference between abstract and interface
Difference between Thymeleaf @RestController and @Controller
[Java] Difference between array and ArrayList
Difference between primitive type and reference type
Difference between string.getByte () and Hex.decodeHex (string.toCharaArray ())
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
[rails] Difference between redirect_to and render
[Android] Difference between finish (); and return;
Note: Difference between Ruby "p" and "puts"
Difference between final and Immutable in Java
[Memo] Difference between bundle install and update
Difference between Ruby instance variable and local variable
Difference between pop () and peek () in stack
[For beginners] Difference between Java and Kotlin
Difference between isEmpty and isBlank of StringUtils
Difference between getText () and getAttribute () in Selenium
About the difference between irb and pry
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
Difference between EMPTY_ELEMENTDATA and DEFAULTCAPACITY_EMPTY_ELEMENTDATA in ArrayList
Difference between addPanel and presentModally of FloatingPanel
[Ruby] Difference between print, puts and p
[Java] Difference between Intstream range and rangeClosed
Difference between int and Integer in Java
[Rails] Difference between redirect_to and render [Beginner]