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
-1
votes
2 answers

Swapping and flattening a Java Map> using Stream API

Having this Map: Map> forwardMap = Map.of( 100, List.of(6), 300, List.of(49, 52), 500, List.of(293) ); I would like to 'flatten' the value Lists and swap the key and value in the Map,…
morsor
  • 1,263
  • 14
  • 29
-1
votes
1 answer

Java stream multiple filter problem on nested lists?

I have the following stream that works without any problem: final List productCategoryList = productCategoryService .findAllByMenu(menuUuid) .stream() .filter(category ->…
user18085807
-1
votes
1 answer

Merge a list of 2 different objects into one combined object

I need to merge 2 lists List, and List, into one combined object Combined, in the Combined only has one field, which is phone [ { "phones": [ { "id": 1, "phone": "44444444" …
hades
  • 4,294
  • 9
  • 46
  • 71
-1
votes
2 answers

Scala - Cannot resolve symbol flatMap

def pinShare(id: String, teamOpt: Option[String], expireTime: Long, interestOpt: Option[List[Interest]]): Future[Unit] = { for { teamObj <- teamOpt.map(team => teamRepository.getTeam(team)).getOrElse(Future.successful(None)) …
-1
votes
1 answer

Create multiple array from single array in spring boot java

Given that I have the array of : Array1 = [100,10,Banana], [101,10,Apple], [102,14,Banana], [103,12,Mango] -- size :4 I want to create a new array which is grouped by the 3rd parameter of Fruits object. So that my array becomes ArrayFinal =…
-1
votes
1 answer

Filtering data from JSONArray and assigning it to a new object

Consider the following example: const arr1 = [{'a': 'I', 'b': 'M', 'c': 'Q'}]; const arr2 = [{'x': 'C', 'y': 'D', 'z': 'M'}]; // I am getting these arrays from a service call if (value of key 'z' of arr2 matches with value of key 'b' of arr1) { //…
-1
votes
2 answers

Looking for a way to merge Javascript Object keys inside array, only on matching Ids. Map? flatmap?

Looking for a way to merge Javascript Object keys inside array, only on matching Ids. Should i use Map? or flatmap? I have const districtList =[ { id:'1234blah', companyId:'09871345', districtName:'abc1' }, { id:'2341blah', companyId:'87134590',…
DarkAdvent
  • 33
  • 7
-1
votes
1 answer

Always call the second API in flatMap

I am making calls to API1 and API2. The result obtained from API1 is passed to the call of API2. If the call to API1 fails, API2 should be called with a blank value. this.Api1().pipe(flatMap(result => { return this.Api2(result); })).subscribe(…
Malwaregeek
  • 2,274
  • 3
  • 15
  • 18
-1
votes
3 answers

Q : Dataset flatmap to empty dataset Spark Java

I want to drop duplicate value of dataset ex : +----------+---------------+--------------------+--------------------+---------+----+-------------+ | e_key|f_timestamp_day| key| …
Hero
  • 9
  • 4
-1
votes
2 answers

Swift [map] return array containing values of specific property

Say i have an array for objects let persons = [Person] struct Person { let name: String let position: Int } And i wanna return array of strings [String] containing persons' names whose position is equal to 1. If there any way to do this…
eugene_prg
  • 948
  • 2
  • 12
  • 25
-1
votes
1 answer

Reduce and get sum on Dictionary containing custom object based on Key

I have an dictionary var dic = [TimeInterval: [CustomObject: Double]]() TimeInterval is a timestamp. CustomObject is an object with property id inside (of type String). And AudioLevel is a double. I succeed to print object inside with: for (key,…
Vjardel
  • 1,065
  • 1
  • 13
  • 28
-1
votes
3 answers

How to convert a String into list of Integer in Scala

I have a string 1,3,5-10 and I have to convert this string into a list of integer in scala. The list will like this --->>. List(1,3,5,6,7,8,9,10) How will be the best way to convert a string list into an integer list using flatMap. or What will…
-1
votes
1 answer

Scala flatMap method on Option type cannot be resolved. What is the correct way to do this?

def jobEventToJobEventTimeLine(jobEvent: JobEvent): JobEventTimeline = { val OpFields(stepId, jobBaseStepId, fieldId) = extractFromOp(jobEvent.getOp) JobEventTimeline( jobEvent.getJobId, if (jobEvent.isSetTyp)…
Brian Yeh
  • 3,119
  • 3
  • 26
  • 40
-1
votes
1 answer

flatMap doesn't work as expected chain of calls

Problem to organize chain of calls by using rxjs observables with typescript I am new in RXJS and have some trouble to organize chain of calls in my typescript code. Question is - how to make this.http.get('http://www.gooogle.com'); calling only…
Alex Coder
  • 11
  • 2
-1
votes
1 answer

How to split a line of text and append that line to each element in Scala/Spark

Given a string like "The apple fell from a tree", how do I split it such that each word has the line of text appended to it such that I get an RDD of strings that would look like: "The | The apple fell from a tree" "apple | The apple fell from a…
teaguecole
  • 1
  • 1
  • 1