Vahe Akhsakhalyan
Vahe Akhsakhalyan

Reputation: 2186

Why set <Integer> is sorting added values?

When I start to add value into Set<Integer> I get sorting elements. Please refer to this example:

Set<Integer> generated = new HashSet<Integer>();
generated.add(2);
generated.add(1);
generated.add(0);

Here I get sorting Set [0, 1, 2]. I would like to get value as I add to generated object.

Upvotes: 2

Views: 433

Answers (4)

Tarik
Tarik

Reputation: 5031

First, regarding the title of your question, Set<Integer> is only the declaration type and its not responsible of any sorting / unsorting behavior, the main reason for using the Set interface is when caring about uniqueness — it doesn't allow duplicates, additional informations from Javadocs:

A Set is a Collection that cannot contain duplicate elements.

Second, it's pure concidence that you got sorted set, use HashSet when you don't care about order when iterating through it, more infos from javadocs:

It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element.

Third, regarding what you are looking for:

I would like to get value as I add to generated object.

then you need to use LinkedHashSet which takes care of the order in which elements were inserted, again from javadocs:

This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order). Note that insertion order is not affected if an element is re-inserted into the set

you may use it simply like this:

Set<Integer> generated = new LinkedHashSet<Integer>();

Fourth and Last, as additional information, another important collection that you need to be aware of it, is the TreeSetwhich guarantees that the elements will be sorted in ascending order, according to natural order, javadocs:

The elements are ordered using their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used

Upvotes: 0

M A
M A

Reputation: 72864

A HashSet does not have a predictable order for elements. Use a LinkedHashSet to preserve insertion order of elements in a set:

Hash table and linked list implementation of the Set interface, with predictable iteration order.

Set<Integer> generated = new LinkedHashSet<Integer>();
generated.add(2);
generated.add(1);
generated.add(0);

Upvotes: 2

wassgren
wassgren

Reputation: 19221

The HashSet does not guarantee the order of the elements. From the JavaDoc:

It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time.

So, in order to keep guarantee the order a LinkedHashSet can be used. From the JavaDoc:

Hash table and linked list implementation of the Set interface, with predictable iteration order.

This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order).

Simply instantiate your Set like this:

Set<Integer> generated = new LinkedHashSet<>();

Upvotes: 2

Rohit Jain
Rohit Jain

Reputation: 213281

Firstly it's just a co-incidence that you get sorted value first time. If you run that code multiple time, you'll see the output in some random order. That's because a HashSet doesn't enforce any ordering on elements you add.

Now to get the elements in the order you inserted, you can use LinkedHashSet, that maintains the insertion order.

Upvotes: 2

Related Questions