Java Set 的排序值

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

Sorting Values of Set

javacollections

提问by Umesh Awasthi

I am trying to sort elements of a set but unable to do so far. here is my code which i am trying to do

我正在尝试对集合的元素进行排序,但目前还无法做到。这是我正在尝试做的代码

public static void main(String [] args){
    Set<String> set=new HashSet<String>();
    set.add("12");
    set.add("15");
    set.add("5");
    List<String> list=asSortedList(set);
}

public static
<T extends Comparable<? super T>> List<T> asSortedList(Collection<T> c) {
  List<T> list = new ArrayList<T>(c);
  Collections.sort(list);
  return list;
}

but this or other way is not working since its all time giving me the same order in which they have been filled 12,15,5

但是这种或其他方式不起作用,因为它一直给我与填充它们相同的顺序 12,15,5

采纳答案by mikej

If you sort the strings "12", "15"and "5"then "5"comes last because "5"> "1". i.e. the natural ordering of Strings doesn't work the way you expect.

如果排序的字符串"12""15"并且"5"然后"5"是最后因为"5"> "1"。即字符串的自然排序不符合您的预期。

If you want to store strings in your list but sort them numerically then you will need to use a comparator that handles this. e.g.

如果您想在列表中存储字符串但按数字对它们进行排序,那么您将需要使用处理此问题的比较器。例如

Collections.sort(list, new Comparator<String>() {
    public int compare(String o1, String o2) {
        Integer i1 = Integer.parseInt(o1);
        Integer i2 = Integer.parseInt(o2);
        return (i1 > i2 ? -1 : (i1 == i2 ? 0 : 1));
    }
});

Also, I think you are getting slightly mixed up between Collectiontypes. A HashSetand a HashMapare different things.

另外,我认为您在Collection类型之间有些混淆。AHashSet和aHashMap是不同的东西。

回答by Faisal Feroz

You need to pass in a Comparator instance to the sort method otherwise the elements will be sorted in their natural order.

您需要将 Comparator 实例传递给 sort 方法,否则元素将按自然顺序排序。

For more information check Collections.sort(List, Comparator)

有关更多信息,请查看Collections.sort(List, Comparator)

回答by Matt Ball

You're using the default comparator to sort a Set<String>. In this case, that means lexicographic order. Lexicographically, "12"comes before "15", comes before "5".

您正在使用默认比较器对Set<String>. 在这种情况下,这意味着lexicographic order。按字典顺序,"12"在 之前"15",在 之前"5"

Either use a Set<Integer>:

要么使用Set<Integer>

Set<Integer> set=new HashSet<Integer>();
set.add(12);
set.add(15);
set.add(5);

Or use a different comparator:

或者使用不同的比较器:

Collections.sort(list, new Comparator<String>() {
    public int compare(String a, String b) {
        return Integer.parseInt(a) - Integer.parseInt(b);
    }
});

回答by Jonathon Faust

Strings are sorted lexicographically. The behavior you're seeing is correct.

字符串按字典顺序排序。你看到的行为是正确的。

Define your own comparator to sort the stringshowever you prefer.

定义您自己的比较器以根据您的喜好对字符串进行排序

It would also work the way you're expecting (5 as the first element) if you changed your collections to Integer instead of using String.

如果您将集合更改为 Integer 而不是使用 String,它也会按照您期望的方式工作(5 作为第一个元素)。

回答by Sean Patrick Floyd

Use a SortedSet (TreeSet is the default one):

使用 SortedSet(TreeSet 是默认的):

SortedSet<String> set=new TreeSet<String>();
set.add("12");
set.add("15");
set.add("5");
List<String> list=new ArrayList<String>(set);

No extra sorting code needed.

不需要额外的排序代码。

Oh, I see you want a different sort order. Supply a Comparator to the TreeSet:

哦,我看到您想要不同的排序顺序。为 TreeSet 提供一个比较器:

new TreeSet<String>(Comparator.comparing(Integer::valueOf));

Now your TreeSet will sort Strings in numeric order (which implies that it will throw exceptions if you supply non-numeric strings)

现在您的 TreeSet 将按数字顺序对字符串进行排序(这意味着如果您提供非数字字符串,它将抛出异常)

Reference:

参考:

回答by dimitrisli

Use the Integerwrapper class instead of String because it is doing the hard work for you by implementing Comparable<Integer>. Then java.util.Collections.sort(list);would do the trick.

使用Integer包装类而不是 String,因为它通过实现Comparable<Integer>. 然后java.util.Collections.sort(list);会做的伎俩。