使用示例的Java中的Collections.sort()

使用示例的Java中的Collections.sort()

java.util.Collections.sort() 方法位于java.util.Collections类中。它用于按升序对指定集合列表中的元素进行排序。它类似于java.util.Arrays.sort()方法,但它比它更好,因为它可以对数组的元素,链表、队列和许多其他元素进行排序。

public static void sort(List myList)

myList :我们要排序的List类型的对象。

此方法没有返回值

例如:

假设我们的列表包含
{“Geeks For Geeks”, “Friends”, “Dear”, “Is”, “Superb”}

使用 Collection.sort() 之后我们得到了一个排序的列表
{“Dear”, “Friends”, “Geeks For Geeks”, “Is”, “Superb”}

按升序排序ArrayList

// Java程序演示Collections.sort()的工作原理
import java.util.*;
 
public class Collectionsorting
{
    public static void main(String[] args)
    {
        // 创建字符串列表
        ArrayList<String> al = new ArrayList<String>();
        al.add("Geeks For Geeks");
        al.add("Friends");
        al.add("Dear");
        al.add("Is");
        al.add("Superb");
 
        /* Collections.sort方法按升序对ArrayList的元素进行排序。*/
        Collections.sort(al);
 
        // 让我们打印排序后的列表
        System.out.println("使用 Collection.sort() 后的列表为:\n" + al);
    }
}

输出

使用 Collection.sort() 后的列表为:
[Dear, Friends, Geeks For Geeks, Is, Superb]

时间复杂度: Collections.sort() 的时间复杂度为 O(N log N),因为它是 O(nlog(n))。

辅助空间: O(1)

按降序对ArrayList进行排序

// Java程序演示Collections.sort()的工作原理
//转为降序。
import java.util.*;
 
public class Collectionsorting
{
    public static void main(String[] args)
    {
        // 创建字符串列表
        ArrayList<String> al = new ArrayList<String>();
        al.add("Geeks For Geeks");
        al.add("Friends");
        al.add("Dear");
        al.add("Is");
        al.add("Superb");
 
        /* Collections.sort方法按升序对ArrayList的元素进行排序。*/
        Collections.sort(al, Collections.reverseOrder());
 
        // 让我们打印排序后的列表
        System.out.println("使用 Collection.sort() 后的列表为:\n" + al);
    }
}

输出

使用 Collection.sort() 后的列表为:
[Superb, Is, Geeks For Geeks, Friends, Dear]

时间复杂度: Collections.sort() 的时间复杂度为 O(N log N),因为它是 O(nlog(n))。
辅助空间: O(1)

根据用户定义的标准对ArrayList进行排序。 我们可以使用Comparator接口来实现这一目的。

// Java program to demonstrate the difference between
// Arrays.sort() and Collections.sort()
import java.util.*;
import java.lang.*;
import java.io.*;
 
class Main
{
    public static void main (String[] args)
    {
        int[] arr = {10, 20, 15, 22, 35};
 
        // Sorting the array using Arrays.sort()
        Arrays.sort(arr);
 
        System.out.printf("Modified arr[] : %s",
                         Arrays.toString(arr));
 
        // Sorting the list using Collections.sort()
        List list = new ArrayList();
        list.add(10);
        list.add(20);
        list.add(15);
        list.add(22);
        list.add(35);
        Collections.sort(list);
 
        System.out.printf("Modified list : %s", list);
    }
}

Output

Modified arr[] : [10, 15, 20, 22, 35]
Modified list : [10, 15, 20, 22, 35]
/*package whatever //do not write package name here */
 
import java.io.*;
import java.util.*;
 
class GFG {
    public static void main (String[] args) {
        int len = 5000000;
       
          // creating a large test array
        int[] arr = new int[len];
        for (int i = len; i > 0; i--)
            arr[len - i] = i;
       
          // creating a large test arraylist
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = len; i > 0; i--)
            list.add(i);
         
          // calculating time used by arrays.sort()
        long startA = System.currentTimeMillis();
        Arrays.sort(arr);
        long stopA = System.currentTimeMillis();
         
          // calculating time used by collections.sort()
          long startAL = System.currentTimeMillis();
          Collections.sort(list); 
        long stopAL = System.currentTimeMillis();
         
          System.out.println("Time taken by Arrays.sort(): " + (stopA - startA));
        System.out.println("Time taken by Collections.sort(): " + (stopAL - startAL));
    }
}
 
// This code is contributed by godcoder28```  

输出

Arrays.sort()花费的时间:29
Collections.sort()花费的时间:42

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程