简体   繁体   中英

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.

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 .

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.

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

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);

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM