How to purpose flatMap() inward Java viii - Stream Example Tutorial

In lodge to empathise the flatMap() method, you lot commencement postulate to empathise the map() function of Java 8. The map() part is declared inwards  the java.util.stream.Stream story together with uses to transform 1 Stream into only about other e.g. a current of integer numbers into only about other current of ints where each chemical component is the foursquare of the corresponding chemical component inwards root stream. In the map() function, a part is applied to each chemical component of Stream together with furnish values are inserted into a novel Stream. The telephone commutation betoken to banknote hither is that the part used past times map() operation returns a single value. Now, if the map performance uses a part which instead of returning a unmarried value returns a Stream of values e.g. prime factors of the pose out together with then you lot bring a Stream of Stream of integers. The flatMap() method is used to flatten that current into a Stream of integers. For example, suppose, you lot bring a listing of numbers e.g. [21, 23, 42] together with nosotros utilisation getPrimeFactors() method along alongside the map() operation to transform this stream. The lawsuit would move [[3,7],[23],[2,3,7]]. If you lot desire to apartment this current of a current into a current of values, you lot tin utilisation the flatMap() which volition furnish [3,7,2,3,2,3,7].


In short, a flatMap() is used to convert a Stream of Stream into a listing of values, for to a greater extent than details you lot tin likewise read Java SE 8 for Really Impatient, 1 of the best majority to larn novel features of Java 8 e.g. lambda expression, streams, novel Date together with Time API together with other enhancements.

 story together with uses to transform 1 Stream into only about other e How to utilisation flatMap() inwards Java 8 - Stream Example Tutorial




Java  8 FlatMap Example

Here is a sample Java programme to demonstrate how to utilisation the flatMap() part inwards Java 8. As I told, you lot tin utilisation the flatMap() to flatten a Stream of Stream of values into only a Stream of values. In our example, nosotros bring a Stream of the listing of String together with past times using the flatMap() nosotros convert this into only a Stream of String to acquire the amount listing of players participating inwards cricket basis loving cup 2016. This performance is actually useful to acquire the full list past times combining several modest lists.


It's said that a moving-picture exhibit is worth a grand words together with this is true, next paradigm explains how flatMap plant inwards Java 8 quite easily:

 story together with uses to transform 1 Stream into only about other e How to utilisation flatMap() inwards Java 8 - Stream Example Tutorial


together with hither is our Java programme to demonstrate how to utilisation the Stream.flatMap() inwards Java 8.

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 utilisation the flatMap() part inwards Java 8.  * The flatMap() part is used to convert a Stream of listing of values to  * only a Stream of values. This is likewise called flattening of stream.  *  * @author Javin Paul  */ public class Test {      public static void 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 impress all players earlier Java 8         List<String> listOfAllPlayers = new ArrayList<>();                  for(List<String> squad : playersInWorldCup2016){             for(String advert : team){                 listOfAllPlayers.add(name);             }         }                  System.out.println("Players playing inwards basis loving cup 2016");         System.out.println(listOfAllPlayers);                           // Now let's produce this inwards Java 8 using FlatMap         List<String> flatMapList = playersInWorldCup2016.stream()                                                         .flatMap(pList -> pList.stream())                                                         .collect(Collectors.toList());                  System.out.println("List of all Players using Java 8");         System.out.println(flatMapList);     }  }  Output run: Players playing inwards basis loving cup 2016 [Virat, Dhoni, Jadeja, Warner, Watson, Smith, Alex, Bell, Broad, Kane, Nathan, Vettori, AB, Amla, Faf, Sammy, Gayle, Narine, Mahela, Sanga, 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, Narine, Mahela, Sanga, Dilshan, Misbah, Afridi, Shehzad] BUILD SUCCESSFUL (total time: 0 seconds)

You tin run into that in conclusion listing contains all the elements from each list. So flatMap() is working fine to flatten a Stream of List of String into only a Stream of String, that's the truthful ability of apartment map performance inwards Java 8.


That's all well-nigh how to utilisation the flatMap() part inwards Java 8.  Just squall back the departure betwixt map() and flatMap() inwards Java 8 together with when to utilisation the map() vs flatMap(). If you lot utilisation a part which returns a listing of values inwards map() performance you lot acquire a Stream of Stream together with past times using flatMap you lot tin convert that to Stream of values. In short, you lot tin combine several modest listing of values into a big listing of values using flatMap(). It's called flatMap() because it flattens the Stream. Read Java SE 8 for Really Impatient to larn more.

Subscribe to receive free email updates:

0 Response to "How to purpose flatMap() inward Java viii - Stream Example Tutorial"

Posting Komentar