0

I am super new to scala , I have the the following code:

val content = List("a b","a b")

gives me : List[String] = List(a b, a b)

val split_data = content.map(testFile => testFile.split("\t"))

gives me : List[Array[String]] = List(Array(a b), Array(a b))

and I want to get a set out of the list, but split_data.toSet gives me: scala.collection.immutable.Set[Array[String]] = Set(Array(a b), Array(a b))

while I would want

scala.collection.immutable.Set[Array[String]] = Set(Array(a b))

How do I do that ?

1
  • Wait.. Don't do that.. though it outputs the expected result, but that's not what you want.. Commented Aug 29, 2018 at 2:32

2 Answers 2

3

In Scala, two arrays with the same values are not equal, as opposed to other collections:

Array(1, 2, 3) == Array(1, 2, 3) // false
List(1, 2, 3)  == List(1, 2, 3)  // true

This is because arrays are just Java arrays, and those don't have the same semantics as collections in functional languages. In Java, two array references are equal if they point to the same array in memory:

val a = Array(1, 2, 3)
a == a              // true, it's the same array
a == Array(1, 2, 3) // false, it's two different arrays

So, if you want to have a set of unique collections, you have to convert the arrays to Scala sequences:

val content = List("a b","a b")
val split_data = content.map(testFile => testFile.split("\t").toSeq)  // note the .toSeq
split_data.toSet   // set with one element
Sign up to request clarification or add additional context in comments.

Comments

2

You can't compare arrays like that as explained in the other answer. The easiest way around this is dedup (convert to Set) before .map:

content.toSet[String].map(_.split("\t"))

This is also a bit more efficient, because you end up spliting fewer strings.

3 Comments

error: missing parameter type for expanded function (Scala 2.12.6)
@jwvh hm yeah ... I have no idea why it does that :/
Ah, I got it ... that's because toSet takes a type parameter, gotta tell it explicitly, that you want String.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.