user2472968
user2472968

Reputation: 395

HashSet giving the output in ascending order

  import java.util.Iterator;
  import java.util.*;
  public class HashSetDemo
  {
  public static void main(String[] args)
  {
  HashSet<Integer> intSet = new HashSet<Integer>();
  intSet.add(2);
  intSet.add(7);
  intSet.add(7);
  intSet.add(3);
  intSet.add(4);
  intSet.add(9);
  intSet.add(1);
  intSet.add(13);
  System.out.println(intSet);
  intSet.remove(1);
  System.out.println(intSet);

I have written the above code to implement HashSet but when I run it, I always get the output in ascending order. I am unable to understand why is this happening as a HashSet doesn't order it's elements.

Upvotes: 0

Views: 2621

Answers (8)

emphywork
emphywork

Reputation: 448

According to Java API Document of Hashset.

Set does not retrieve the Order of the elements.

Since you have entered the elements in the HashSet and it can return in any manner of order, may be because it takes up different order every time or not.

Behavior of Set especially Hashset is depends upon the Hashcode for each object you have added into the Set.

So, if you run the program after a while it may show you same or different order. If it does not show any changes in the order it may be taking the hashcodes that way. And manipulating hashcodes is not in our(developers) hand.

Upvotes: 0

Tala
Tala

Reputation: 8928

Hashset doesn't guarantee the order of elements. But it calculates hashcode for objects in it. You might be having it since integers might be giving a sequential hashcode (until the capacity max is reached)

Hashset has an array of buckets. According to source code initial capacity is 16:

static final int DEFAULT_INITIAL_CAPACITY = 16;

So when you try your small integers they got placed up in order

Upvotes: 3

Suresh Atta
Suresh Atta

Reputation: 121998

As you add and remove elements over time, the iteration order may change. You should never rely on the iteration order of Hashset, as it "makes no guarantees as to the iteration order," although in practice if you make a new Hashset with the default constructor and you add the same elements you end up with the same iteration order.

Upvotes: 0

Sanjaya Liyanage
Sanjaya Liyanage

Reputation: 4746

It is not guaranteed.set these values and test.

  intSet.add(21);
  intSet.add(22);
  intSet.add(7);
  intSet.add(3);
  intSet.add(4);
  intSet.add(9);
  intSet.add(1);
  intSet.add(13);

Upvotes: 2

Mac
Mac

Reputation: 1495

According to documentation of HashSet:

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.

And , It too doesn't guarantee that iteration order will always not be constant. You can get iteration order. FYI at my system the iteration order is changing on every execution.

Upvotes: 0

UmNyobe
UmNyobe

Reputation: 22890

Because a set conceptually doesn't have an order whatsoever. if the set is explicitly a List, a tree, etc then there is a specific ordering. If you see a specific order from the code above, it is specific to the implementation and the values.

Upvotes: 0

Uwe Plonus
Uwe Plonus

Reputation: 9954

Please note that a HashSet will never return your values in any particular order.

You have to use a TreeSet (or some other kind of SortedSet) to achieve a sorted iteration.

Upvotes: 1

Adam Siemion
Adam Siemion

Reputation: 16039

HashSet 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

from HashSet JavaDoc.

Upvotes: 1

Related Questions