Java?List排序?qū)嵗a詳解
一、自然排序
自然排序是按照對(duì)象的自然順序進(jìn)行排序,例如數(shù)字的大小或字符串的字典序。對(duì)于實(shí)現(xiàn)了 Comparable 接口的類,可以直接使用 Collections.sort() 或 List.sort() 方法進(jìn)行排序。
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class NaturalSortExample {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>();
numbers.add(5);
numbers.add(2);
numbers.add(8);
numbers.add(1);
// 使用 Collections.sort() 排序
Collections.sort(numbers);
System.out.println("排序后的數(shù)字列表: " + numbers);
List<String> words = new ArrayList<>();
words.add("apple");
words.add("banana");
words.add("cherry");
// 使用 List.sort() 排序
words.sort(null);
System.out.println("排序后的單詞列表: " + words);
}
}
二、自定義排序規(guī)則
當(dāng)需要按照特定規(guī)則排序時(shí),可以實(shí)現(xiàn) Comparator 接口來自定義排序規(guī)則。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CustomSortExample {
public static void main(String[] args) {
List<Person> people = new ArrayList<>();
people.add(new Person("Alice", 30));
people.add(new Person("Bob", 25));
people.add(new Person("Charlie", 35));
// 按年齡升序排序
Collections.sort(people, new Comparator<Person>() {
@Override
public int compare(Person p1, Person p2) {
return Integer.compare(p1.getAge(), p2.getAge());
}
});
System.out.println("按年齡升序排序:");
for (Person person : people) {
System.out.println(person);
}
// 按年齡降序排序
Collections.sort(people, new Comparator<Person>() {
@Override
public int compare(Person p1, Person p2) {
return Integer.compare(p2.getAge(), p1.getAge());
}
});
System.out.println("按年齡降序排序:");
for (Person person : people) {
System.out.println(person);
}
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + '}';
}
}
三、使用 Lambda 表達(dá)式簡(jiǎn)化 Comparator
從 Java 8 開始,可以使用 Lambda 表達(dá)式來簡(jiǎn)化 Comparator 的實(shí)現(xiàn)。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class LambdaSortExample {
public static void main(String[] args) {
List<Person> people = new ArrayList<>();
people.add(new Person("Alice", 30));
people.add(new Person("Bob", 25));
people.add(new Person("Charlie", 35));
// 使用 Lambda 表達(dá)式按年齡升序排序
people.sort((p1, p2) -> Integer.compare(p1.getAge(), p2.getAge()));
System.out.println("按年齡升序排序:");
people.forEach(System.out::println);
// 使用 Lambda 表達(dá)式按年齡降序排序
people.sort((p1, p2) -> Integer.compare(p2.getAge(), p1.getAge()));
System.out.println("按年齡降序排序:");
people.forEach(System.out::println);
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + '}';
}
}
四、多條件排序
可以結(jié)合多個(gè)條件進(jìn)行排序,例如先按年齡排序,再按姓名排序。
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class MultiConditionSortExample {
public static void main(String[] args) {
List<Person> people = new ArrayList<>();
people.add(new Person("Alice", 30));
people.add(new Person("Bob", 25));
people.add(new Person("Charlie", 30));
people.add(new Person("David", 25));
// 按年齡升序,年齡相同則按姓名字典序排序
people.sort(Comparator.comparingInt(Person::getAge)
.thenComparing(Person::getName));
System.out.println("按年齡升序,年齡相同按姓名排序:");
people.forEach(System.out::println);
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + '}';
}
}
五、總結(jié)
Java 提供了多種方式對(duì) List 進(jìn)行排序,包括自然排序和自定義排序。通過實(shí)現(xiàn) Comparable 接口或使用 Comparator,可以靈活地定義排序規(guī)則。Java 8 的 Lambda 表達(dá)式進(jìn)一步簡(jiǎn)化了排序代碼,使代碼更加簡(jiǎn)潔易讀。
到此這篇關(guān)于Java List排序的文章就介紹到這了,更多相關(guān)Java List 排序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中的clone()和Cloneable接口實(shí)例
這篇文章主要介紹了Java中的clone()和Cloneable接口實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
SpringBoot使用druid配置多數(shù)據(jù)源問題
這篇文章主要介紹了SpringBoot使用druid配置多數(shù)據(jù)源問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
SpringBoot集成FFmpeg實(shí)現(xiàn)生成圖片預(yù)覽圖與縮略圖
這篇文章主要為大家詳細(xì)介紹了SpringBoot如何集成FFmpeg實(shí)現(xiàn)生成圖片預(yù)覽圖與縮略圖,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2026-02-02
SpringMVC實(shí)現(xiàn)文件的上傳和下載實(shí)例代碼
本篇文章主要介紹了SpringMVC實(shí)現(xiàn)文件的上傳和下載實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
Java調(diào)用opencv IDEA環(huán)境配置的教程詳解
這篇文章主要為大家詳細(xì)介紹了Java調(diào)用opencv IDEA環(huán)境配置的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
Spring中的底層架構(gòu)核心概念類型轉(zhuǎn)換器詳解
這篇文章主要介紹了Spring中的底層架構(gòu)核心概念類型轉(zhuǎn)換器詳解,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
JAVA編程實(shí)現(xiàn)TCP網(wǎng)絡(luò)通訊的方法示例
這篇文章主要介紹了JAVA編程實(shí)現(xiàn)TCP網(wǎng)絡(luò)通訊的方法,簡(jiǎn)單說明了TCP通訊的原理并結(jié)合具體實(shí)例形式分析了java實(shí)現(xiàn)TCP通訊的步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-08-08

