Soumajyoti Sarkar
Soumajyoti Sarkar

Reputation: 593

How to concatenate list of strings with map reduce in scala

I have a list of list of strings and I want to concatenate all the unique strings into a single (delimited space) string, something that flatMap allows one to do. However I am confused about the correct usage of the reduce function when concatenating 2 strings.

Input:

val listStrings: List[List[String]] = List(List("this", "is", "the"), List("this", "here"))

Expected Output (the order does not matter): String("this is the here")

Upvotes: 0

Views: 1158

Answers (1)

9000
9000

Reputation: 40894

If you have colossal amounts of repeating strings to deduplicate, you likely need two steps:

  • Map a list of strings to a set of strings (a trivial constructor invocation).
  • Reduce a sequence of sets into one set (maybe even in-place merging is possible).

If you only have 2-3 entries, as shown in your example, @jwh's answer in comments suffices.

Upvotes: 1

Related Questions