Java?List排序?qū)嵗a詳解
一、自然排序
自然排序是按照對象的自然順序進(jìn)行排序,例如數(shù)字的大小或字符串的字典序。對于實(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í)現(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á)式簡化 Comparator
從 Java 8 開始,可以使用 Lambda 表達(dá)式來簡化 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é)合多個條件進(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 提供了多種方式對 List 進(jìn)行排序,包括自然排序和自定義排序。通過實(shí)現(xiàn) Comparable
接口或使用 Comparator
,可以靈活地定義排序規(guī)則。Java 8 的 Lambda 表達(dá)式進(jìn)一步簡化了排序代碼,使代碼更加簡潔易讀。
到此這篇關(guān)于Java List排序的文章就介紹到這了,更多相關(guān)Java List 排序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java GZip 基于內(nèi)存實(shí)現(xiàn)壓縮和解壓的方法
這篇文章主要介紹了Java GZip 基于內(nèi)存實(shí)現(xiàn)壓縮和解壓的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08javaSE基礎(chǔ)如何通俗的理解javaBean是什么
所謂的Java Bean,就是一個java類,編譯后成為了一個后綴名是 .class的文件。這就是Java Bean,很多初學(xué)者,包括當(dāng)年的我自己,總是被這些專有名詞搞的暈頭轉(zhuǎn)向2021-10-10Java使用過濾器防止SQL注入XSS腳本注入的實(shí)現(xiàn)
這篇文章主要介紹了Java使用過濾器防止SQL注入XSS腳本注入,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Springboot啟動報錯Input length = 2的問題解決
最近使用Springboot啟動報錯,報錯內(nèi)容java.nio.charset.MalformedInputException: Input length = 2,下面就來介紹一下解決方法,感興趣的可以了解一下2024-08-08JDBC實(shí)現(xiàn)學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了JDBC實(shí)現(xiàn)學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-02-02