How to declare a sorted set of array of ints in Scala?

294 views Asked by At

I have tried all of the following for scala.collection.mutable.SortedSet:

var s = SortedSet[Array[Int]]
var s = SortedSet[Array[Int]]()
var s = new SortedSet[Array[Int]]
var s = new SortedSet[Array[Int]]()
var s = SortedSet[Array]
var s = SortedSet[Array]()
var s = new SortedSet[Array]()

I don't know why it has to be so hard to just declare this stuff in Scala. All I want is a sorted set of Int-arrays.

1

There are 1 answers

7
Akos Krivachy On BEST ANSWER

SortedSet is not a "default" collection, therefore not defined in Predef.

First import it then it should work:

import scala.collection.SortedSet
val s = SortedSet[Array[Int]]()

Next to this you need an implicit ordering defined:

implicit val arrayOrdering = new Ordering[Array[Int]] {
   override def compare(x: Array[Int], y: Array[Int]): Int = ???
     // Implement whatever you mean by one array is greater than the other array
}

Also be careful with Array-s as that is a Java array, so it does not have the equals and hashcode methods overridden and does not look at value equality, but rather reference equality.