Java Collections reverseOrder()

reverseOrder() 方法用于获取一个比较有否就实现Comparable接口的对象的集合的自然顺序相反。有两个重载的reverseOrder()方法:

  • Java Collections reverseOrder() 方法
  • Java Collections reverseOrder(comp) 方法

1 语法

public static <T> Comparator<T> reverseOrder()  

或

public static <T> Comparator<T> reverseOrder(Comparator<T> comp)  

2 参数

comp:一个比较器,其顺序将由返回的比较器颠倒,或者为null。

3 返回值

  • reverseOrder():返回一个比较器,该比较器对实现Comparable接口的对象集合施加相反的自然顺序。
  • reverseOrder(Comparator comp):返回一个比较器,该比较器强加指定比较器的相反顺序。

4 Collections reverseOrder()示例1

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Collections.reverseOrder的例子
 */
import java.util.*;

public class Demo {

    public static void main(String[] args) {
        //Create linked list object
        LinkedList<Integer> list = new LinkedList<Integer>();
        //Add values in the list
        list.add(10);
        list.add(-20);
        list.add(50);
        list.add(90);
        list.add(-15);
        //Sort the list and return comparator for reverse order
        Collections.sort(list, Collections.reverseOrder());
        System.out.println("List sorted in ReverseOrder: ");
        System.out.println(list);
    }
}

输出结果为:

List sorted in ReverseOrder: 
[90, 50, 10, -15, -20]

5 Collections reverseOrder()示例2

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Collections.reverseOrder的例子
 */
import java.util.*;

public class Demo {

    public static void main(String[] args) {
        //Create Array list object  
        List<String> list = Arrays.asList("Java", "Python", "Ruby", "COBOL");
        Collections.sort(list, Collections.reverseOrder());
        System.out.println("List sorted in ReverseOrder: ");
        System.out.println(list);
    }
}

输出结果为:

List sorted in ReverseOrder: 
[Ruby, Python, Java, COBOL]

6 Collections reverseOrder()示例3

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Collections.reverseOrder的例子
 */
import java.util.*;

public class Demo {

    public static void main(String[] args) {
        //Create Array list object
        List<String> list = Arrays.asList("Jaya", "Pratham", "Ruby", "Commit");
        Collections.sort(list, Collections.reverseOrder (Demo::lastCharComparator));
        //Sort the list according to last character of word and return comparator for reverse order
        System.out.println(list);
    }
    private static int lastCharComparator(String s1, String s2) {
        return Character.compare(s1.charAt(s1.length() - 1), s2.charAt(s2.length()-1));
    }
}

输出结果为:

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Collections.reverseOrder的例子
 */
import java.util.*;

public class Demo {

    public static void main(String[] args) {
        //Create Array list object
        List<String> list = Arrays.asList("Java", "Pratham", "Ruby", "Commit");
        Collections.sort(list, Collections.reverseOrder (Demo::lastCharComparator));
        //Sort the list according to last character of word and return comparator for reverse order
        System.out.println(list);
    }
    private static int lastCharComparator(String s1, String s2) {
        return Character.compare(s1.charAt(s1.length() - 1), s2.charAt(s2.length()-1));
    }
}

输出结果为:

[Ruby, Commit, Pratham, Java]

7 Collections reverseOrder()示例4

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Collections.reverseOrder的例子
 */
import java.util.*;

public class Demo {

    public static void main(String[] args) {
        //Create linked list object
        LinkedList<Integer> list = new LinkedList<Integer>();
        //Add values in the list
        list.add(50);
        list.add(-20);
        list.add(120);
        list.add(-80);
        //Sort the list and Create comparator for reverse order
        Comparator<Integer> cmp = Collections.reverseOrder(null);
        Collections.sort(list, cmp);
        //Print the list
        System.out.println("List sorted in ReverseOrder: ");
        for(int i : list) {
            System.out.println(i+ " ");
        }
    }
}

输出结果为:

List sorted in ReverseOrder: 
120 
50 
-20 
-80

 

热门文章

优秀文章