Questions tagged [flatmap]

flatMap is a Java and Scala function that works by applying a function that returns a sequence for each element in the list, and flattening the results into the original list.

flatMap works by applying a function that returns a sequence for each element in the list, and flattening the results into the original list. This is easier to show than to explain:

scala> def g(v:Int) = List(v-1, v, v+1)
g: (v: Int)List[Int]

scala> l.map(x => g(x))
res64: List[List[Int]] = List(List(0, 1, 2), List(1, 2, 3), List(2, 3, 4), List(3, 4, 5), List(4, 5, 6))

scala> l.flatMap(x => g(x))
res65: List[Int] = List(0, 1, 2, 1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6)

Source: http://www.brunton-spall.co.uk/post/2011/12/02/map-map-and-flatmap-in-scala/

545 questions
-2
votes
1 answer

flatmap for inner classes

I have a class. I want to get list of objects that will contain all this parameters flat. For now I'm doing it with foreach. Is there a way to do it with flatMap? public class A{ String parA; List parB; public static class B{ String…
janneob
  • 959
  • 2
  • 11
  • 25
-3
votes
2 answers

How can I flatten the below nested object to array of strings?

I am trying to convert below object to array of string JSON Object Input : [ { "name": "Pantry", "childrenItems": [ { "name": "Butter", "childrenItems": [ { …
-3
votes
1 answer

How to get minimum value for each distinct key using ReduceByKey() in Scala

I have a flat map that returns the Sequence Seq((20,6),(22,6),(23,6),(24,6),(20,1),(22,1)) now I need to use the reduceByKey() on the sequence that I got from the flat map to find the minimum value for each key. I tried using .reduceByKey(a,min(b))…
varun
  • 25
  • 2
  • 11
-4
votes
1 answer

Order (k,) RDD

I have an rdd of the following form: rdd = sc.parallelize([(2, [199.99, 250.0, 129.99]), (4, [49.98, 299.95, 150.0, 199.92]), (8, [179.97, 299.95, 199.92, 50.0]), (10, [199.99, 99.96, 129.99, 21.99, 199.99]), (12, [299.98, 100.0, 149.94, 499.95,…
juamd
  • 363
  • 2
  • 5
-4
votes
3 answers

How does flatMap work on Maps in scala? map can be used as mapValues on Maps but how does the flatMap function work on Map objects?

I am not able to understand the functioning of flatMap function on Map objects.
Avik Aggarwal
  • 599
  • 7
  • 28
1 2 3
36
37