简体   繁体   English

HashSet按升序给出输出

[英]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. 我已经编写了上面的代码来实现HashSet,但是当我运行它时,我总是得到升序的输出。 I am unable to understand why is this happening as a HashSet doesn't order it's elements. 我不明白为什么会发生这种情况,因为HashSet不能对元素进行排序。

HashSet makes no guarantees as to the iteration order of the set; HashSet不保证集合的迭代顺序。 in particular, it does not guarantee that the order will remain constant over time 特别是,它不能保证订单会随着时间的推移保持恒定

from HashSet JavaDoc . HashSet JavaDoc中

Please note that a HashSet will never return your values in any particular order. 请注意, HashSet绝不会以任何特定顺序返回您的值。

You have to use a TreeSet (or some other kind of SortedSet ) to achieve a sorted iteration. 您必须使用TreeSet (或其他某种SortedSet )来实现排序的迭代。

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: 根据源代码的初始容量为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: 根据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. 您永远不应该依赖Hashset的迭代顺序,因为它“不能保证迭代顺序”,尽管在实践中,如果使用默认构造函数创建新的Hashset并添加相同的元素,则最终得到相同的迭代订购。

According to Java API Document of Hashset. 根据哈希集的Java API文档。

Set does not retrieve the Order of the elements. Set不会检索元素的Order。

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. 由于您已经在HashSet中输入了元素,并且它可以按任何顺序返回,这可能是因为它每次都占用不同的顺序。

Behavior of Set especially Hashset is depends upon the Hashcode for each object you have added into the Set. Set的行为,尤其是Hashset,取决于您添加到Set中的每个对象的Hashcode。

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. 操纵哈希码不在我们(开发人员)手中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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