Map: For the elements contained in the stream, the newly generated stream contains only the elements that are generated by the transformation, using the given conversion function. This method has three variant methods for the original type, namely Maptoint,maptolong and maptodouble. These three methods are also better understood, for example Maptoint is to convert the original stream into a new stream, and the elements in the newly generated stream are of type int. There are three variants that can dispense with the extra consumption of automatic boxing/unpacking;
Map method:
FLATMAP: Similar to map, the difference is that each of its elements translates to a stream object, which compresses the elements in the stream into the parent collection;
Flatmap Method:
Original address
Http://www.java67.com/2016/03/how-to-use-flatmap-in-java-8-stream.html
One word
Convert several small lists to a large list.
One figure Flatmap manipulating a piece of code
Package test;Import java.util.ArrayList;Import Java.util.Arrays;Import java.util.List;Import java.util.stream.Collectors;/** * Java program to demonstrate how to use the FlatMap () function in Java 8. * The FlatMap () function is used to convert a stream of list of values to * just a stream of values. This is also called flattening of stream. * * @author Javin Paul */publicClassTest {publicStaticvoid Main (String args[]) {list<string> Teamindia = Arrays.aslist ("Virat","Dhoni","Jadeja"); list<string> Teamaustralia = Arrays.aslist ("Warner","Watson","Smith"); list<string> Teamengland = Arrays.aslist ("Alex","Bell","Broad"); list<string> Teamnewzeland = Arrays.aslist ("Kane","Nathan","Vettori"); list<string> Teamsouthafrica = Arrays.aslist ("AB","Amla","Faf"); list<string> teamwestindies = Arrays.aslist ("Sammy","Gayle","Narine"); list<string> Teamsrilanka = Arrays.aslist ("Mahela","Sanga","Dilshan"); list<string> Teampakistan = Arrays.aslist ("Misbah","Afridi","Shehzad"); list<list<String>> playersInWorldCup2016 =New Arraylist<> (); Playersinworldcup2016.add (Teamindia); Playersinworldcup2016.add (Teamaustralia); Playersinworldcup2016.add (Teamengland); Playersinworldcup2016.add (Teamnewzeland); Playersinworldcup2016.add (Teamsouthafrica); Playersinworldcup2016.add (teamwestindies); Playersinworldcup2016.add (Teamsrilanka); Playersinworldcup2016.add (Teampakistan);Let's print all players before Java 8 list<String> listofallplayers =New Arraylist<> ();for (list<String> team:playersinworldcup2016) {ForString name:team) {listofallplayers.add (name);}} System.out.println ("Players playing in World Cup 2016"); System.out.println (listofallplayers);//now let's do this in Java 8 using FlatMap list<string> FL Atmaplist = Playersinworldcup2016.stream (). FlatMap (PList-Plist.stream ()). Collect (Collectors.tolist ()); System.out.println ( "List of all Players using Java 8"); System.out.println (flatmaplist); }}outputrun:players playing in World Cup 2016[virat, Dhoni, Jadeja, Warner, Watson, Smith, Alex, Bell, Broad, Kane, Nathan, Vettori, AB, Amla, Faf, Sammy, Gayle, Narine, Mahela, Sang A, Dilshan, Misbah, Afridi, Shehzad]list of all Players using Java 8[virat, Dhoni, Jadeja, Warner, Watson, Smith, Alex, Bell, Broad, Kane, Nathan, Vettori, AB, Amla, Faf, Sammy, Gayle, Nari NE, Mahela, Sanga, Dilshan, Misbah, Afridi, shehzad]build successful (total time: 0 seconds)
JAVA8 Map Flatmap