Grouping elements of Stream – Collectors.groupingBy()

This function lets us collect the elements of a Stream into a Map function while grouping them. The basic difference of this function with Collectors.toMap is that this function lets you create a Map<K,List<T>> function, that is, it lets you create a Map function that will hold a List value as its value for each of the groups.

Consider the following example: 

  fun main(args: Array<String>) { 
      val resultantSet = (1..20).asSequence().asStream() 
              .collect(Collectors.groupingBy<Int,Int> { it%5 }) 
      println("resultantSet $resultantSet") 
  } 

The output is as follows:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.144.108