java 我如何保持哈希集按字母顺序排列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12314710/
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-31 08:24:44  来源:igfitidea点击:

How do i keep a hashset alphabetically ordered?

javacollectionshashset

提问by svz

I have a collection of a bignumber of objects that are defined by name/value pairs. I need to have fast access to any of their values and to be able to return them ordered alphabetically by name. First I thought I might use a HashMap to get fast access. But it gave me no ordering. I decided to switch to LinkedHashSet.
The problem with it is that I need to be able to insertnew Objects in the right places of the list, but LinkedHashSet doesn't allow that. I also need to be able to access Objects by their index as well as by name.

Will be thankful for any ideas.

我有一个集合由名称/值对定义的对象的数量。我需要快速访问它们的任何值,并能够按名称的字母顺序返回它们。首先,我想我可能会使用 HashMap 来快速访问。但它没有给我任何命令。我决定切换到 LinkedHashSet。
它的问题是我需要能够在列表的正确位置插入新对象,但 LinkedHashSet 不允许这样做。我还需要能够通过索引和名称访问对象。

将不胜感激任何想法。

回答by RNJ

Why not try TreeSet. Does your list not allow duplicates? If so then the Set should be ok. As you are adding strings and this implements Comparator the set will be automatically sorted for you

为什么不试试TreeSet。您的列表不允许重复吗?如果是这样,那么 Set 应该没问题。当您添加字符串并实现 Comparator 时,该集合将自动为您排序

If you had

如果你有

Set<String> s = new TreeSet<String>();
s.add("B");
s.add("C");
s.add("A");

then the contents of the set would be A, B, C

那么集合的内容将是 A, B, C

回答by Amit Deshpande

You can use TreeMap

您可以使用TreeMap

A Red-Black tree based NavigableMap implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.

基于红黑树的 NavigableMap 实现。映射根据其键的自然顺序进行排序,或者通过映射创建时提供的 Comparator 进行排序,具体取决于使用的构造函数。

回答by Sumit Singh

You can use Comparator..

您可以使用比较器..

    Collections.sort(yourCollection, new Comparator() {

    public int compare(YourObject o1, YourObject o2) {
         // put Comparesion according to your requirement 
        return AnyObject;
    }
});

回答by Peter Lawrey

I would use a TreeSet which is a SortedSet. You need to define your custom class as Comparable based on the name and your collection will always be sorted.

我会使用一个 TreeSet,它是一个 SortedSet。您需要根据名称将自定义类定义为 Comparable,并且您的集合将始终被排序。

Note: sorted collections have an O(log N) access time.

注意:排序集合的访问时间为 O(log N)。

回答by John Szakmeister

Have you looked at TreeMap? It's based off of Red-Black trees which help maintain ordering, but still gives fast access.

你看过TreeMap吗?它基于红黑树,有助于维持排序,但仍然提供快速访问。

回答by Santosh

A TreeMapshould address your requirements. If your keys are not literals then use appropriate Comparatorin TreeMap constructor.

ATreeMap应该满足您的要求。如果您的键不是文字,则Comparator在 TreeMap 构造函数中使用适当的。