Java中的List是一種常見的數據結構,它可以存儲多個元素,並且允許重複元素存在。在Java中對List進行排序通常使用sort方法。
一、基本使用
sort方法是List介面中的一個方法,用於對列表中的元素進行排序。下面是對一個List進行簡單排序的示例:
List<String> strList = new ArrayList<String>(); strList.add("apple"); strList.add("banana"); strList.add("pear"); Collections.sort(strList); System.out.println(strList);
運行結果:
[apple, banana, pear]
排序方法是由Collections工具類提供的靜態方法sort()實現的,它會自動按照字元串的字典序進行排序。
二、使用Comparator介面自定義排序規則
sort方法還可以通過實現Comparator介面來自定義排序規則。Comparator介面中有一個compare()方法,它用於比較兩個元素的大小。
下面是一個自定義排序規則的示例,對Student對象按照成績進行排序:
class Student { private String name; private int score; public Student(String name, int score) { this.name = name; this.score = score; } public String getName() { return name; } public int getScore() { return score; } } List<Student> studentList = new ArrayList<Student>(); studentList.add(new Student("小明", 80)); studentList.add(new Student("小紅", 90)); studentList.add(new Student("小王", 70)); Collections.sort(studentList, new Comparator<Student>() { public int compare(Student s1, Student s2) { return s1.getScore() - s2.getScore(); } }); for(Student s : studentList) { System.out.println(s.getName() + " : " + s.getScore()); }
運行結果:
小王 : 70 小明 : 80 小紅 : 90
這裡通過實現Comparator這個介面,並實現compare()方法對List進行排序。在compare()方法中,如果s1比s2小,則返回負數;如果s1比s2大,則返回正數;如果s1和s2相等,則返回0。
三、使用Lambda表達式簡化代碼
在Java 8之後,可以使用Lambda表達式來替代Comparator介面中的compare()方法,代碼變得更加簡化。
下面是使用Lambda表達式對上一個示例進行優化:
List<Student> studentList = new ArrayList<Student>(); studentList.add(new Student("小明", 80)); studentList.add(new Student("小紅", 90)); studentList.add(new Student("小王", 70)); Collections.sort(studentList, (s1, s2) -> s1.getScore() - s2.getScore()); for(Student s : studentList) { System.out.println(s.getName() + " : " + s.getScore()); }
運行結果:
小王 : 70 小明 : 80 小紅 : 90
使用Lambda表達式可以省去定義Comparator介面的過程,代碼更加簡潔。
四、總結
sort方法是Java中對List進行排序的常用方法,它可以自動按照元素的大小進行排序,也可以通過實現Comparator介面來自定義排序規則。在Java 8中還可以使用Lambda表達式來簡化代碼。
代碼示例:
List<String> strList = new ArrayList<String>(); strList.add("apple"); strList.add("banana"); strList.add("pear"); Collections.sort(strList); System.out.println(strList); class Student { private String name; private int score; public Student(String name, int score) { this.name = name; this.score = score; } public String getName() { return name; } public int getScore() { return score; } } List<Student> studentList = new ArrayList<Student>(); studentList.add(new Student("小明", 80)); studentList.add(new Student("小紅", 90)); studentList.add(new Student("小王", 70)); Collections.sort(studentList, new Comparator<Student>() { public int compare(Student s1, Student s2) { return s1.getScore() - s2.getScore(); } }); for(Student s : studentList) { System.out.println(s.getName() + " : " + s.getScore()); } List<Student> studentList = new ArrayList<Student>(); studentList.add(new Student("小明", 80)); studentList.add(new Student("小紅", 90)); studentList.add(new Student("小王", 70)); Collections.sort(studentList, (s1, s2) -> s1.getScore() - s2.getScore()); for(Student s : studentList) { System.out.println(s.getName() + " : " + s.getScore()); }
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/187764.html