HashSet为什么不按照代码中的顺序添加对象?

huangapple go评论57阅读模式
英文:

Why the HashSet add the Objects not in the order as in the code?

问题

以下是您提供的代码的翻译部分:

import java.util.*;
public class SetDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        HashSet<String> s1 = new HashSet<String>();
        s1.add("Java");
        s1.add("C++");
        s1.add("C");
        s1.add("dotNet");
        s1.add("JavaScript");
        s1.add("Script");
        s1.add("Python");
        s1.add("JavaScript");
    }
}

输出结果为:
[Java, Script, C++, dotNet, C, JavaScript, Python]

编辑:刚刚阅读了这个,我认为它可能是我正在寻找的答案

英文:

This is what i tried.

import java.util.*;
public class SetDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		HashSet&lt;String&gt; s1=new HashSet&lt;String&gt;();
		s1.add(&quot;Java&quot;);
		s1.add(&quot;C++&quot;);
		s1.add(&quot;C&quot;);
		s1.add(&quot;dotNet&quot;);
		s1.add(&quot;JavaScript&quot;);
		s1.add(&quot;Script&quot;);
		s1.add(&quot;Python&quot;);
		s1.add(&quot;JavaScript&quot;);
	}
}

Output
[Java, Script, C++, dotNet, C, JavaScript, Python]

Edit:Just read this , i think it might be answer to what i was asking

答案1

得分: 6

来自相关的 JavaDoc

这个类实现了 Set 接口,由哈希表(实际上是 HashMap 实例)支持。它不保证集合的迭代顺序;特别地,它不保证顺序会随时间保持恒定。这个类允许 null 元素。

基本上,一个 HashSet:

  • 存储唯一的元素并允许 null
  • 由 HashMap 支持
  • 不保持插入顺序
  • 不是线程安全的
英文:

From the relevant JavaDoc:

> This class implements the Set interface, backed by a hash table
> (actually a HashMap instance). 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.

Basically, a HashSet:

  • stores unique elements and permits nulls
  • is backed by a HashMap
  • doesn't maintain insertion order
  • is not thread-safe

答案2

得分: 2

你需要使用 LinkedHashSet&lt;&gt;() 来实现相同的功能。

import java.util.*;
public class SetDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        LinkedHashSet<String> s1 = new LinkedHashSet<String>();
        s1.add("Java");
        s1.add("C++");
        s1.add("C");
        s1.add("dotNet");
        s1.add("JavaScript");
        s1.add("Script");
        s1.add("Python");
        s1.add("JavaScript");
    }
}

如果你想要自然顺序,也可以使用 SortedSet

英文:

You need you to use LinkedHashSet&lt;&gt;() for the same.

import java.util.*;
public class SetDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        HashSet&lt;String&gt; s1=new LinkedHashSet&lt;String&gt;();
        s1.add(&quot;Java&quot;);
        s1.add(&quot;C++&quot;);
        s1.add(&quot;C&quot;);
        s1.add(&quot;dotNet&quot;);
        s1.add(&quot;JavaScript&quot;);
        s1.add(&quot;Script&quot;);
        s1.add(&quot;Python&quot;);
        s1.add(&quot;JavaScript&quot;);
    }
}

You can also use the SortedSet if you want natural order.

huangapple
  • 本文由 发表于 2020年8月19日 21:00:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63487546.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定