Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insertion-ordered ListSet

Tags:

list

set

scala

ListSet (collection.immutable.ListSet) is a inverse ordered set. I need ordered set. This is a example of original ListSet:

var a = ListSet(1,2,3)
var ite = a.iterator
ite.next // returns 3
ite.next // returns 2
ite.next // returns 1

And this is a example of I need:

var a = ListSet(1,2,3)
var ite = a.iterator
ite.next // returns 1
ite.next // returns 2
ite.next // returns 3

UPDATE:

"Ordered" is a "Insertion Ordered" for me. I need this:

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
like image 634
barroco Avatar asked Aug 14 '10 03:08

barroco


People also ask

What does insertion order mean?

What is an insertion order. An insertion order is a contract between an advertiser or advertising agency and a publisher to run an advertising campaign in print or online.

What is meant by insertion order is preserved?

Insertion order refers to the order in which you are adding elements to the data structure (i.e., a collection like List , Set , Map , etc..). For example, a List object maintains the order in which you are adding elements, whereas a Set object doesn't maintain the order of the elements in which they are inserted.

What is ListSet in Scala?

In scala, ListSet class implements immutable sets using a list-based data structure. Elements are stored in reversed insertion order, That means the newest element is at the head of the list. It maintains insertion order. Listset is used only for a small number of elements.

Does Python set preserve insertion order?

Unlike lists, ordinary sets do not preserve the order in which we insert the elements. This is because the elements in a set are usually not stored in the order in which they appear.


3 Answers

collection.mutable.LinkedHashSet is a set that iterates its members in the same sequence they were inserted. (I avoid the term "ordered" here, since I prefer to reserve that to cases of an ordering relation on the values, not the particular sequence in which some actions were carried out.)

like image 155
Randall Schulz Avatar answered Sep 21 '22 13:09

Randall Schulz


var eti = a.toList.reverse.iterator
like image 21
user unknown Avatar answered Sep 17 '22 13:09

user unknown


It is not ordered:

val a = ListSet(3,1,2)
val ite = a.iterator
ite.next // returns 2
ite.next // returns 1
ite.next // returns 3
like image 20
Daniel C. Sobral Avatar answered Sep 18 '22 13:09

Daniel C. Sobral