Efficient way to convert Scala Array to Unique Sorted List

前端 未结 7 2050
长发绾君心
长发绾君心 2021-01-04 15:00

Can anybody optimize following statement in Scala:

// maybe large
val someArray = Array(9, 1, 6, 2, 1, 9, 4, 5, 1, 6, 5, 0, 6) 

// output a sorted list whic         


        
7条回答
  •  南笙
    南笙 (楼主)
    2021-01-04 15:34

    How about adding everything to a sorted set?

    val a = scala.collection.immutable.SortedSet(someArray filter (0 !=): _*)
    

    Of course, you should benchmark the code to check what is faster, and, more importantly, that this is truly a hot spot.

提交回复
热议问题