java 默认设置是否排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7685362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:58:38  来源:igfitidea点击:

Set by Default Sorted or Not?

java

提问by Ravi Parekh

UPDATED:

更新

    Set s = new HashSet();
    s.add(1);
    s.add(5);
    s.add(4);
    s.add(9);
    s.add(7);
    s.add(8);        
    s.add("b");
    s.add("a");
    s.add("B");
    s.add("A");
    s.add("s");
    s.add("x");        
    s.add("d");        
    System.out.println(s);
    s.remove("b");
    s.remove("d");
    System.out.println(s);

Output:

输出:

[1, d, 4, b, 5, A, B, 7, a, 8, 9, s, x]
[1, 4, 5, A, B, 7, a, 8, 9, s, x]
[1, 4, 5, A, B, 7, a, 8, 9, s, x]

Need some information that Set default sort Integer value when we add but if i Add String to Set it would not sort by default.

添加时需要一些设置默认排序整数值的信息,但如果我将字符串添加到设置,则默认情况下不会排序。

Update:and Also Caps letter would always Sorted after runs many times.

更新:并且大写字母在多次运行后总是会排序。

java version "1.6.0_26" Java(TM) SE Runtime Environment (build 1.6.0_26-b03) Java HotSpot(TM) Client VM (build 20.1-b02, mixed mode, sharing)

java version "1.6.0_26" Java(TM) SE Runtime Environment (build 1.6.0_26-b03) Java HotSpot(TM) Client VM(build 20.1-b02,混合模式,共享)

please give me some idea. Thanks

请给我一些想法。谢谢

采纳答案by NPE

HashSetdoes not guaranteed that its contents will be sorted in any way. There is a special interface for sets that do provide such a guarantee: it's called SortedSet:

HashSet不保证其内容会以任何方式排序。对于确实提供这种保证的集合,有一个特殊的接口:它被称为SortedSet

A Setthat further provides a total ordering on its elements. The elements are ordered using their natural ordering, or by a Comparatortypically provided at sorted set creation time. The set's iterator will traverse the set in ascending element order. Several additional operations are provided to take advantage of the ordering. (This interface is the set analogue of SortedMap.)

ASet进一步提供其元素的总排序。元素使用它们的自然顺序进行排序,或者通过Comparator通常在排序集创建时提供的顺序进行排序。集合的迭代器将按元素升序遍历集合。提供了几个额外的操作来利用排序。(此接口是 的集合模拟SortedMap。)

In Java 6, there are two classes that implement this interface: ConcurrentSkipListSetand TreeSet.

在 Java 6 中,有两个类实现了这个接口:ConcurrentSkipListSetTreeSet.

回答by Jon Skeet

No, HashSetis notsorted - or at least, not reliably. You may happento get ordering in some situations, but you must not rely on it. For example, it's possiblethat it will always return the entries sorted by "hash code modulo some prime" - but it's not guaranteed, and it's almost certainly not useful anyway.

不,HashSet没有排序的-或者至少不可靠。在某些情况下,您可能会碰巧收到订单,但您不能依赖它。例如,它可能总是返回按“哈希码模一些素数”排序的条目 - 但不能保证,而且几乎可以肯定它无论如何都没有用。

If you need a sorted set implementation, look at TreeSet.

如果您需要排序集实现,请查看TreeSet.

回答by Prithwis Chatterjee

From Oracle documentation we can find the HashSet class implements the set interface and internally backed by Hash Table.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. But My code Which is given bellow finds it sorted even after running number of time.

从Oracle文档中我们可以发现HashSet类实现了集合接口,内部由哈希表支持。它不保证集合的迭代顺序;特别是,它不保证订单会随着时间的推移保持不变。但是下面给出的我的代码发现它即使在运行次数之后也已排序。

 public static void main(String[] args){
        Random rand = new Random(47);           
        SortedSet<Integer> intset = new TreeSet<Integer>();
        Set<Integer> intsetUnsorted = new HashSet<Integer>();
        for(int i=0;i<10000;i++){
            intset.add(rand.nextInt(30));
        }
        intsetUnsorted.add(500);
        for(int i=0;i<10000;i++){
            intsetUnsorted.add(rand.nextInt(30));
        }
        String version = System.getProperty("java.version");
        System.out.println("JDK version-"+version);     
        System.out.println("TreeSet o/p");
        System.out.println(intset);
        System.out.println("Hash Set o/p");
        System.out.println(intsetUnsorted);

    }  

O/P

开/关

JDK version-1.6.0
TreeSet o/p
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
Hash Set o/p
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 16, 19, 18, 21, 20, 23, 22, 25, 24, 27, 26, 29, 28, 500]

回答by Jan Zyka

I would suggest you check the Set interface javaDoc. In the All Known Implementing Classessection you have the provided implementations and if you check HashSet the first sentence answers your question.

我建议您检查Set 接口 javaDoc。在所有已知的实现类部分中,您有提供的实现,如果您选中 HashSet,第一句话就可以回答您的问题。

Btw. checking javaDoc is always a good place to start ;-)

顺便提一句。检查 javaDoc 始终是一个很好的起点 ;-)