ListSet(collection.immutable.ListSet)是一个反向排序集。我需要订购套装。这是原始ListSet的一个示例:
var a = ListSet(1,2,3)
var ite = a.iterator
ite.next // returns 3
ite.next // returns 2
ite.next // returns 1
这是我需要的一个例子:
var a = ListSet(1,2,3)
var ite = a.iterator
ite.next // returns 1
ite.next // returns 2
ite.next // returns 3
更新:
“有序”是我的“插入订单”。我需要这个:
var a = ListSet(1,2,3)
a += 5
a += 4
var ite = a.iterator
ite.next // returns 1
ite.next // returns 2
ite.next // returns 3
ite.next // returns 5
ite.next // returns 4
答案 0 :(得分:14)
collection.mutable.LinkedHashSet
是一个按照插入顺序迭代其成员的集合。 (我在这里避免使用“有序”一词,因为我更倾向于将其保留给值的排序关系,而不是执行某些操作的特定顺序。)
答案 1 :(得分:4)
没有订购:
val a = ListSet(3,1,2)
val ite = a.iterator
ite.next // returns 2
ite.next // returns 1
ite.next // returns 3
答案 2 :(得分:4)
var eti = a.toList.reverse.iterator
答案 3 :(得分:2)
如果您想按照插入的顺序检索元素,则需要先进先出的集合,因此只需使用Queue。
import collection.mutable.Queue
val queue = Queue(1,2,3)
queue += 5
queue += 4
for(i <- queue)
println(i)
打印
1
2
3
5
4
答案 4 :(得分:1)
import collection.SetLike
import collection.generic.{CanBuildFrom, ImmutableSetFactory, GenericCompanion, GenericSetTemplate}
@serializable @SerialVersionUID(2L)
class OrderedListSet[A] extends Set[A]
with GenericSetTemplate[A, OrderedListSet]
with SetLike[A, OrderedListSet[A]] {
override def companion: GenericCompanion[OrderedListSet] = OrderedListSet
override def size: Int = 0
override def empty = OrderedListSet.empty[A]
def iterator: Iterator[A] = Iterator.empty
override def foreach[U](f: A => U): Unit = { }
def contains(e: A): Boolean = get0(e)
override def + (e: A): OrderedListSet[A] = updated0(e)
override def + (elem1: A, elem2: A, elems: A*): OrderedListSet[A] = this + elem1 + elem2 ++ elems
def - (e: A): OrderedListSet[A] = removed0(e)
protected def get0(key: A): Boolean = false
protected def updated0(key: A): OrderedListSet[A] =
new OrderedListSet.OrderedListSet1(key)
protected def removed0(key: A): OrderedListSet[A] = this
protected val indexes:List[Int] = List[Int]()
protected val nextIndex:Int = 1
def pairIterator:Iterator[(A,Int)] = Iterator.empty
protected def writeReplace(): AnyRef = new OrderedListSet.SerializationProxy(this)
protected def pairForeach[U](f: ((A,Int)) => U): Unit = { }
}
object OrderedListSet extends ImmutableSetFactory[OrderedListSet] {
/** $setCanBuildFromInfo */
implicit def canBuildFrom[A]: CanBuildFrom[Coll, A, OrderedListSet[A]] = setCanBuildFrom[A]
override def empty[A]: OrderedListSet[A] = EmptyOrderedListSet.asInstanceOf[OrderedListSet[A]]
private object EmptyOrderedListSet extends OrderedListSet[Any] {
}
class OrderedListSet1[A](private[OrderedListSet] var key: A) extends OrderedListSet[A] {
override def size = 1
override val indexes = List[Int](1)
override val nextIndex = indexes.head + 1
override def get0(key: A): Boolean = (key == this.key)
override def updated0(key: A): OrderedListSet[A] =
if (this.key == key) {
this
} else {
val m = new EEOrderedListSet[A](List[A](this.key), indexes, nextIndex)
m.updated0(key)
}
override def removed0(key: A): OrderedListSet[A] = if (key == this.key) OrderedListSet.empty[A] else this
override def iterator = Iterator(key)
override def pairIterator: Iterator[(A, Int)] = Iterator((key, indexes.head))
override def foreach[U](f: A => U): Unit = f(key)
override def pairForeach[U](f: ((A,Int)) => U): Unit = f (key, indexes.head)
}
class EEOrderedListSet[A](private var elems: List[A],
override protected[OrderedListSet] val indexes: List[Int],
override protected[OrderedListSet] val nextIndex:Int)
extends OrderedListSet[A] {
override def size = elems.size
override def get0(key: A): Boolean = elems.contains(key)
override def updated0(key: A): OrderedListSet[A] = {
if (elems contains key) {
this
} else {
new EEOrderedListSet(elems.:+(key), indexes.:+(nextIndex), nextIndex+1)
}
}
override def removed0(key: A): OrderedListSet[A] = {
val r = elems findIndexOf (_ == key)
if ( r != -1 ) {
val e = elems filterNot (_ == key)
val (i1, i2) = indexes splitAt r
val i = i1 ++ i2.tail
new EEOrderedListSet(e, i, nextIndex)
} else {
this
}
}
override def iterator = elems.iterator
override def pairIterator: Iterator[(A, Int)] = (elems zip indexes).iterator
override def foreach[U](f: A => U): Unit = elems.foreach(f)
override def pairForeach[U](f: ((A,Int)) => U): Unit = (elems zip indexes).foreach(f)
}
@serializable @SerialVersionUID(2L) private class SerializationProxy[A,B](@transient private var orig: OrderedListSet[A]) {
private def writeObject(out: java.io.ObjectOutputStream) {
val s = orig.size
out.writeInt(s)
for (e <- orig) {
out.writeObject(e)
}
}
private def readObject(in: java.io.ObjectInputStream) {
orig = empty
val s = in.readInt()
for (i <- 0 until s) {
val e = in.readObject().asInstanceOf[A]
orig = orig + e
}
}
private def readResolve(): AnyRef = orig
}
}
答案 5 :(得分:0)
实际上,它根本不是一个有序集。如果您需要订购,请使用immutable.SortedSet
的实施,例如immutable.TreeSet
。