2

I'm looking for a Scala mutable sequential collection that sorts elements on insert. I.e. after I've finished inserting the elements, the resulting collection is sorted. It seems this thing doesn't exist, but maybe I'm missing something? It probably wouldn't be so hard to implement, but I don't want to reinvent the wheel.

EDIT: Set semantics are not suitable, since elements could be repeated.

EDIT: I'm looking for Scala collection, not Java one. This question is not about how to achieve such behavior on JVM, but about existing Scala collection that does this. If such thing doesn't exist, I'll accept such answer.

1
  • You could use Java's TreeSet. It's iterable so I suppose it matches your criterion of being "sequential"? Commented Nov 17, 2015 at 10:29

2 Answers 2

2

You could use java.util.PriorityQueue.

Edit

There is scala.collection.mutable.PriorityQueue. But only dequeue and dequeueAll method will maintain order.

import scala.collection.mutable.PriorityQueue
val pq = new PriorityQueue()(Ordering[Int].reverse) //default is descending order
pq += 5
pq += 4
pq += 2
pq.enqueue    //2
pq.enqueueAll //Vector(4,5)
Sign up to request clarification or add additional context in comments.

Comments

0

Since you looking for mutable collection consider using java's TreeSet

2 Comments

Set semantics are not suitable, since elements could be repeated. I've updated the question.
Map of List or some other collection type then, or map of Int (occurrences count)

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.