Java 以相反的顺序对 Set 进行排序

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

Sort a Set in reverse order

java

提问by carlspring

Apologies for the newbie question, but what's the proper way to get a Set(say LinkedHashSet) in reverse order? For Collections there's Collections.reverse(Collection c), but how does one do it for a Setwith ordered elements (like a LinkedHashSet)?

为新手问题道歉,但是以相反的顺序获得Set(例如LinkedHashSet)的正确方法是什么?对于Collections 有Collections.reverse(Collection c),但是如何为Set具有有序元素(如 a LinkedHashSet)的a 做到这一点?

采纳答案by Peter

Sets are not ordered in general, so to preserve the sorting, after sorting the set as a list, you would need to use a known iteration order implementation of Set, such as LinkedHashSet

集合一般不排序,所以为了保持排序,在将集合排序为列表后,您需要使用 Set 的已知迭代顺序实现,例如 LinkedHashSet

List list = new ArrayList(set);
Collections.sort(list, Collections.reverseOrder());
Set resultSet = new LinkedHashSet(list);

You could also use TreeSet with a comparator, but that is not as fast as the ArrayList method above.

您也可以将 TreeSet 与比较器一起使用,但这不如上面的 ArrayList 方法快。

回答by Daniel Williams

Check this out

看一下这个

http://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html#descendingSet()

http://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html#descendingSet()

If you use a TreeSet you can get reverse order by calling descendingSet.

如果您使用 TreeSet,您可以通过调用descendingSet 获得相反的顺序。

回答by Ved Prakash

public class LargestArray {
public static void main(String[] args) {

    ArrayList<Integer> al = new ArrayList<>();
    Set<Integer> set = new TreeSet<>();
    set.add(10);
    set.add(20);
    set.add(7);
    set.add(4);
    set.add(1);
    set.add(2);
    set.add(3);
    set.add(4);
    System.out.println("after Sorting");
    for(int i : set) {
        System.out.print("  " + i);
    }
    al.addAll(set);
    set.clear();
    Collections.reverse(al);
    System.out.println();
    System.out.println("After Reverse");
    for (int i : al) {
        System.out.print(" " + i);
    }

}

}

output = after Sorting 1 2 3 4 7 10 20 After Reverse 20 10 7 4 3 2 1

输出 = 排序后 1 2 3 4 7 10 20 反向后 20 10 7 4 3 2 1

回答by Manjunatha B

I will explain you with an example. Comments are added in mid of the code for better understanding.

我会用一个例子来解释你。代码中间添加了注释,以便更好地理解。

public class ReverseLinkedHashSet {

    public static void main(String[] args) {

        // creating a LinkedHashSet object which is
        // of type String or any. Will take a example of String.
        HashSet<String> cars = new LinkedHashSet<String>();

        // adding car elements to LinkedHashSet object as below
        cars.add("Toyato");
        cars.add("Hundai");
        cars.add("Porshe");
        cars.add("BMW");

        // Iterating using enhanced for-loop to see the order.
        System.out.println("Insertion Order: Iterating LinkedHashSet\n");
        for(String car : cars) {
            System.out.println(car);

        // Output will be as below  
        //Toyato
        //Hundai
        //Porshe
        //BMW
        }

        // Now convert to ArrayList to rearrange to reverse
        // the linkedHashset
        List<String> listOfCars = new ArrayList<String>(cars);

        // to reverse LinkedHashSet contents
        Collections.reverse(listOfCars);

        // reverse order of LinkedHashSet contents
        // can be done as below
        System.out.println("\n\n\nReverse Order of LinkedHashSet\n");
        for(String car : listOfCars) {
            System.out.println(car);

        // Output will be as below  
        //BMW
        //Porshe
        //Hundai
        //Toyato    
        }
    }
}

Also, I suggest not to use LinkedhashSetwithout a strong reason. For a complex application, it will reduce the performance. Use HashSetinstead.

另外,我建议不要在LinkedhashSet没有充分理由的情况下使用。对于复杂的应用程序,它会降低性能。使用HashSet来代替。

回答by bamossza

Java 8, I using solution below,

Java 8,我使用下面的解决方案,

Set<String> setTest = new HashSet<>();

setTest.add("1");
setTest.add("2");
setTest.add("3");

List<String> list = new ArrayList<>(setTest);
list.sort(Collections.reverseOrder());
Set<String> result = new LinkedHashSet<>(list);

for (String item: result) {
    System.out.println("---> " + item);
}

Result:

---> 3
---> 2
---> 1

Work for me.

为我工作。