Flattening operation flow java8 stream flatMap

  flatMap usage and meaning to live through a case to explain,

  Case: given a list of words [ "Hello", "World"], you want to return the list [ "H", "e", "l", "o", "W", "r", "d"]

  The first way

  String[] words = new String[]{"Hello","World"};

  List a = Arrays.stream(words)

  .map(word -> word.split(""))

  .distinct()

  .collect(toList());

  a.forEach(System.out::print);

  Code output is: [Ljava.lang.String; @ 12edcd21 [Ljava.lang.String; @ 34c45dca

  (Two return a String [] a list)

  This implementation is a problem, a method is passed to the lambda map is generated for each word a String [] (String list). Thus, map the return flow actually Stream type. What you really want is to use Stream to represent a string.

  FIG operational flow is below the top of the code stream

  The second embodiment: flatMap (convection flattening treatment)

  String[] words = new String[]{"Hello","World"};

  List a = Arrays.stream(words)

  .map(word -> word.split(""))

  .flatMap(Arrays::stream)

  .distinct()

  .collect(toList());

  a.forEach(System.out::print);

  The results output: HeloWrd

  FlatMap method using the effect that each array are mapped not a stream, but mapped to the content stream generated using all map (Array :: stream) are combined to a single stream, i.e. into a flat flow.

  The figure is the use of flatMap the stream running processes,

  // flat stream

  // Find a unique array of characters

  String[] strArray = {"hello", "world"};

  //Implementation

  List res = Arrays.stream(strArray)

  .map(w -> w.split(""))

  .flatMap(Arrays::stream)

  .distinct()

  .collect(Collectors.toList());

  System.out.println(res);

  // TODO Case

  System.out.println("--------------------------------");

  // Demo1: Given a list, returns an array of squares (directly mapped)

  //[1,2,3,4]=>[1,4,9,16]

  Integer[] nums1 = {1, 2, 3, 4};

  List nums1List = Arrays.asList(nums1);

  List res1 = nums1List.stream().map(i -> i * i).collect(Collectors.toList());

  System.out.println(res1);

  System.out.println("--------------------------------");

  // Demo2: Given two arrays, returns an array of

  //[1,2,3],[3,4]=>[1,3],[1,4],[2,3],[2,4],[3,3],[3,4]

  Integer[] nums2 = {1, 2, 3};

  Integer [] nums3 = {3, 4}; Wuxi normal gynecological http://www.xasgyy.net/

  List nums2List = Arrays.asList(nums2);

  List nums3List = Arrays.asList(nums3);

  // map using two nested filter

  List res2 = nums2List.stream().flatMap(i -> nums3List.stream().map(j -> new int[]{i, j})).collect(Collectors.toList());

  System.out.println(res2.size());

  System.out.println("--------------------------------");

  // Demo3: Returns the total number of combinations for Demo1 Demo2 divisible by 3 and the

  // (2,4) and (3,3) is to meet the conditions

  List res3 = nums2List.stream().flatMap(i -> nums3List.stream().filter(j -> (i + j) % 3 == 0).map(j -> new int[]{i, j})).collect(Collectors.toList());

  System.out.println(res3.size());

  }

  }


Guess you like

Origin blog.51cto.com/14335413/2401399