Java 中的 Collections.sort() 方法及示例
Collections 类提供了一个增强型静态方法 `sort()`,用于对列表或数组中的元素进行排序。对于操作原始集合类型元素的集合,我们也可以使用 TreeSet。`Collections.sort()` 方法位于 `java.util.Collections` 类中,允许用户按升序对特定列表中的元素进行排序。该方法是 `java.util.Arrays.sort()` 方法类的升级版本,它也能够对链表、队列等进行排序。简而言之,Collections 类能够有效地对列表中的元素进行排序。让我们来看一个例子:
我们将使用 `
此过程中可能的 myList > 它是一个 List 类型(任何类型),我们希望从中对对象进行排序。
不幸的是,此方法不会返回任何输出。
例如
假设我们的列表包含
{"TutorialsPoint", "Friends", "Dear", "Is", "The", "Best", “Hey”}
使用 Collection.sort() 后,我们得到一个排序后的列表:
{“Hey”, "Dear", "Friends", "TutorialsPoint", "Is", "The", "Best"}
有一些方法可以对集合列表中的元素进行排序。使用此方法,我们可以从字符串对象、包装类或用户定义的类中收集数据。
**`public void sort(List list)`** - 此表示法可用于对列表中的元素进行排序。唯一的条件是,现有元素必须具有可比较的类型。这里我们可以使用字符串类和包装类来实现此过程。要存储字符串和包装类中存在的对象的 value,它必须具有可比较的性质。
**`sort(List list, Comparator c)`** - 此方法根据声明的 Comparator 类指定的顺序对特定列表的元素进行排序。
使用 Collections.sort() 方法的算法
在这个可能的算法中,我们将向您展示如何在 Java 环境中应用 Collections.sort() 方法。通过这个算法,我们将尝试构建一些语法和一些 Java 代码来进一步解释问题陈述。
**步骤 1** - 开始此过程。
**步骤 2** - 导入并声明可用的 Java 包。
**步骤 3** - 声明一个公共类。
**步骤 4** - 构造一个字符串参数。
**步骤 5** - 创建一个字符串列表。
**步骤 6** - 通过向其中添加一些数据元素来填充列表。
**步骤 7** - 声明 Collections.sort 方法,以根据流程过滤数组列表的元素。
**步骤 8** - 运行此过程。
**步骤 9** - 打印排序列表中的数据元素。
**步骤 10** - 获取结果并终止此过程。
实现 Collections.sort() 方法的语法
Fruit(int id, String name, String taste){
this.id=id;
this.name=name;
this.taste=taste;
}
@Override //Override the method
public int compareTo(Object o) {
Fruit f = (Fruit) o;
return this.id - f.id ;
}
}
//Sorting Method 1
Collections.sort(fruitList);
fruitList.forEach(fruit -> {
System.out.println(fruit.getId() + " " + fruit.getName() + " " +
fruit.getTaste());
});
//Sorting Method 2
Collections.sort(fruitList, (a, b) -> {
return a.getName().compareTo(b.getName());
});
//Sorting by using a wrapper class{
ArrayList al=new ArrayList();
al.add(Integer.valueOf(201));
al.add(Integer.valueOf(101));
al.add(230);
Collections.sort(al);
Iterator itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
在上面提到的语法中,我们尝试以两种不同的方式向您展示 Collection.sort() 方法的构造过程。通过使用它们,我们正在朝着可能的途径前进,以通过应用各种 Java 方法来解释给定的语句。
遵循的方法
**方法 1** - Java 程序,用于解释 Collections.sort() 方法的工作原理
**方法 2** - Java 程序,用于说明 Comparator 接口和 Collections.sort() 的工作方法,以便根据用户定义的条件进行排序
方法 1:演示 Collections.sort() 的工作原理
在这种可能的方法中,我们将向您展示如何以升序方式对 ArrayList 进行排序。对于此过程:
**时间复杂度** - 表示为 O(N log N),其中此过程的基本时间复杂度为 O(nlog(n))。
**辅助空间** - 辅助空间表示为 O(1)。
示例 1
//Java program to explain the working of a Collections.sort() method
import java.util.*;
public class Collectionsorting0710{
public static void main(String[] args){
ArrayList<String> al = new ArrayList<String>();
al.add("I");
al.add("AM");
al.add("GOING");
al.add("TO");
al.add("DHAKA");
Collections.sort(al);
System.out.println("Here is the list after the use of the" +
" Collection.sort() method:\n" + al);
}
}
输出
Here is the list after the use of the Collection.sort() method: [AM, DHAKA, GOING, I, TO]
在此特定方法中,我们将操作一个字符串类,从中构建另一个 ArrayList。时间复杂度表示为 O(N log N),其中此过程的基本时间复杂度为 O(nlog(n))。辅助空间:辅助空间表示为 O(1)。
示例 2
//Java program to explain the working of the Collections.sort() to a descending order.
import java.util.*;
public class Collectionsorting{
public static void main(String[] args){
ArrayList<String> al = new ArrayList<String>();
al.add("I");
al.add("AM");
al.add("GOING");
al.add("BY");
al.add("MAITREE EXPRESS");
Collections.sort(al, Collections.reverseOrder());
System.out.println("Possible list after the use of the" +
" Collection.sort() method:\n" + al);
}
}
输出
Possible list after the use of the Collection.sort() method: [MAITREE EXPRESS, I, GOING, BY, AM]
方法 2:使用 Comparator 接口和 Collections.sort() 进行排序操作
现在我们将解释一些用户特定的过程,以演示使用 ArrayList 进行排序的过程。
在示例 3 中,我们将使用 Comparator 接口。
在示例 3A 中,我们将向您展示 Arrays.sort() 和 Collections.sort() 之间的区别。
最后,在 3B 中;我们将向您展示 Arrays.sort() 和 Collections.sort() 的时间复杂度。
示例 1
//Java program to illustrate the working method of a Comparator interface and the Collections.sort() to sort according to the user defined criteria.
import java.util.*;
import java.lang.*;
import java.io.*;
class Student{
int rollno;
String name, address;
public Student(int rollno, String name,
String address){
this.rollno = rollno;
this.name = name;
this.address = address;
}
public String toString(){
return this.rollno + " " + this.name +
" " + this.address;
}
}
class Sortbyroll implements Comparator<Student>{
public int compare(Student a, Student b){
return a.rollno - b.rollno;
}
}
public class Main{
public static void main (String[] args){
ArrayList<Student> ar = new ArrayList<Student>();
ar.add(new Student(07, "ARB - 0710", "BAD BRINBACH"));
ar.add(new Student(16, "RDD - 1610", "ROTTAL INN"));
ar.add(new Student(2022, "REBA - 2001", "EU CAMPUS"));
System.out.println("Unsorted Data Elements Present In The List");
for (int i=0; i<ar.size(); i++)
System.out.println(ar.get(i));
Collections.sort(ar, new Sortbyroll());
System.out.println("\nSorted elements by the Roll Number");
for (int i=0; i<ar.size(); i++)
System.out.println(ar.get(i));
}
}
输出
Unsorted Data Elements Present In The List 7 ARB - 0710 BAD BRINBACH 16 RDD - 1610 ROTTAL INN 2022 REBA - 2001 EU CAMPUS Sorted elements by the Roll Number 7 ARB - 0710 BAD BRINBACH 16 RDD - 1610 ROTTAL INN 2022 REBA - 2001 EU CAMPUS
示例 2
import java.util.*;
public class Collectionsort0710{
public static void main(String[] args){
String domains[] = {"I", "WILL REACH",
"BY", "AFTERNOON"};
List colList =
new ArrayList(Arrays.asList(domains));
Collections.sort(colList);
System.out.println("The list after the use of the" +
"method of Collection.sort() :\n" +
colList);
}
}
输出
Note: Collectionsort0710.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. The list after the use of themethod of Collection.sort() : [AFTERNOON, BY, I, WILL REACH]
示例 3
import java.io.*;
import java.util.*;
public class ARBRDD100520220101 {
public static void main (String[] args) {
int len = 5000000;
int[] arr = new int[len];
for (int i = len; i > 0; i--)
arr[len - i] = i;
ArrayList<Integer> list = new ArrayList<>();
for (int i = len; i > 0; i--)
list.add(i);
long startA = System.currentTimeMillis();
Arrays.sort(arr);
long stopA = System.currentTimeMillis();
long startAL = System.currentTimeMillis();
Collections.sort(list);
long stopAL = System.currentTimeMillis();
System.out.println("Time taken by the process of Arrays.sort(): " +
(stopA - startA));
System.out.println("Time taken by the process of Collections.sort() is: " + (stopAL - startAL));
}
}
输出
Time taken by the process of Arrays.sort() is: 15 Time taken by the process of Collections.sort() is: 31
结论
Collection.sort 过程执行按升序排序的过程。使用此方法,我们可以执行 `reverseOrder()` 操作,该操作返回一个 Comparator 类。另一方面,`reverseOrder(Comparator cmp)` 方法为我们提供了一个返回的 Comparator,它将是该特定 Comparator 类的反向值。在今天的文章中,我们学习了集合类的排序过程。通过使用上述算法、语法和 Java 代码,我们以有效的方式解决了问题陈述。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP