Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Scala have a 'unique list' type?

I'm looking for something like the immutable SortedSet, except I want elements to be ordered in the sequence they were passed into the constructor.

UniqueList(4,2,3,1,1) // Throws exception
UniqueList(4,2,3,1) // Iterator yields: 4,2,3,1
like image 875
Lawrence Wagerfield Avatar asked Dec 07 '25 06:12

Lawrence Wagerfield


2 Answers

Try collection.mutable.LinkedHashSet - it has unique elements and traversal occurs in the order that the elements were added.

like image 182
axel22 Avatar answered Dec 08 '25 22:12

axel22


There's ListSet, although it will have the values in the reverse order that they were passed into the constructor.

scala> import scala.collection.immutable.ListSet
import scala.collection.immutable.ListSet

scala> ListSet(1,2,3,4)
res7: scala.collection.immutable.ListSet[Int] = ListSet(4, 3, 2, 1)
like image 30
Erik Engbrecht Avatar answered Dec 08 '25 23:12

Erik Engbrecht