0
  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.

user2472968
  • 377
  • 2
  • 5
  • 15
  • Buckets! Hash Buckets!!! – Thihara Jul 01 '13 at 09:48
  • possible duplicate of [Order of values retrieved from a HashMap](http://stackoverflow.com/questions/2144776/order-of-values-retrieved-from-a-hashmap) – Stephen C Jul 01 '13 at 09:50
  • What you have is a "fortuitous interaction" between 1) the way hash tables work, 2) the implementation of the `Integer.hashcode()` method, 3) the HashMap's default initial size and 4) your choice of keys. See the linked Question/Answer for a longer explanation. – Stephen C Jul 01 '13 at 09:52

8 Answers8

3

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

Tala
  • 8,888
  • 5
  • 34
  • 38
2

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);
Sanjaya Liyanage
  • 4,706
  • 9
  • 36
  • 50
1

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.

Adam Siemion
  • 15,569
  • 7
  • 58
  • 92
1

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.

Uwe Plonus
  • 9,803
  • 4
  • 41
  • 48
0

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.

UmNyobe
  • 22,539
  • 9
  • 61
  • 90
0

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.

Mac
  • 1,711
  • 3
  • 12
  • 26
0

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.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

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.

emphywork
  • 448
  • 1
  • 4
  • 15
  • when i was learning i tried to add many values and than i got the proof that Set does not maintain the order and in Hashset it took long because of the Hashcodes. I can't tell you to just believe me but just go ahead and try it may work. But it takes time. – emphywork Jul 12 '13 at 12:51