gpt4 book ai didi

Java List sort()排序方法示例

转载 作者:知者 更新时间:2024-03-12 08:01:21 25 4
gpt4 key购买 nike

sortjava.util.List 接口的默认方法。 Java 8 中引入了 Listsort 方法。
1.sort 方法接受 Comparator 作为参数,并根据指定的 Comparator 对该 List 进行排序。

default void sort(Comparator<? super E> c)

2. 如果 List 的元素具有可比性,即元素类实现了 Comparable 接口,那么我们可以将 null 传递给 sort 方法,并按照自然顺序进行排序。
3. 尽管 List 可以修改,但它不能调整大小。

sort() 带Comparator

示例 1: 我们将使用 Comparator.comparing 方法创建一个 Comparator 对象。

Comparator<Student> nameComparator = Comparator.comparing(Student::getName);

我们将上述对象传递给 Listsort 方法。

students.sort(nameComparator);

List students 将根据指定的 Comparator 诱导的顺序对所有学生对象进行排序。
我们可以将排序顺序颠倒如下。

students.sort(nameComparator.reversed());

现在找到完整的例子。
ListSort1.java

package com.concretepage;
import java.util.Comparator;
import java.util.List;
public class ListSort1 {
  public static void main(String[] args) {
	List<Student> students = Student.getStudents();

	System.out.println("--- Sort by name in ascending order ---");
	Comparator<Student> nameComparator = Comparator.comparing(Student::getName);
	students.sort(nameComparator);
	students.forEach(s -> System.out.println(s));
	
	System.out.println("--- Sort by name in descending order ---");
	students.sort(nameComparator.reversed());
	students.forEach(s -> System.out.println(s));
	
	System.out.println("--- Sort by age in ascending order ---");
	Comparator<Student> ageComparator = Comparator.comparing(Student::getAge);
	students.sort(ageComparator);
	students.forEach(s -> System.out.println(s));
	
	System.out.println("--- Sort by age in descending order ---");
	students.sort(ageComparator.reversed());
	students.forEach(s -> System.out.println(s));	
  }
}

Student.java

package com.concretepage;
import java.util.Arrays;
import java.util.List;
public class Student {
  private String name;
  private int age;
  public Student(String name, int age) {
	this.name = name;
	this.age = age;
  }

  //Setters and Getters

  public static List<Student> getStudents() {
	Student s1 = new Student("Lakshman", 25);
	Student s2 = new Student("Ram", 30);
	Student s3 = new Student("Shatrudhan", 23);
	Student s4 = new Student("Bharat", 28);

	return Arrays.asList(s1, s2, s3, s4);
  }
  @Override
  public String toString() {
	return name + " - " + age;
  }
}

输出

--- Sort by name in ascending order ---
Bharat - 28
Lakshman - 25
Ram - 30
Shatrudhan - 23
--- Sort by name in descending order ---
Shatrudhan - 23
Ram - 30
Lakshman - 25
Bharat - 28
--- Sort by age in ascending order ---
Shatrudhan - 23
Lakshman - 25
Bharat - 28
Ram - 30
--- Sort by age in descending order ---
Ram - 30
Bharat - 28
Lakshman - 25
Shatrudhan - 23

示例 2: 这里我们将通过实现 Comparator 接口来创建比较器类,并将它们与 sort 方法一起使用。
ListSort2.java

package com.concretepage;
import java.util.Comparator;
import java.util.List;

class NameComparator implements Comparator<Student> {
  @Override
  public int compare(Student s1, Student s2) {
     return s1.getName().compareTo(s2.getName());
  }
}

class AgeComparator implements Comparator<Student> {
  @Override
  public int compare(Student s1, Student s2) {
     return s1.getAge() - s2.getAge();
  }
}

public class ListSort2 {
  public static void main(String[] args) {
	List<Student> students = Student.getStudents();

	System.out.println("--- Sort by name in ascending order ---");
	students.sort(new NameComparator());
	students.forEach(s -> System.out.println(s));
	
	System.out.println("--- Sort by age in Ascending order ---");
	students.sort(new AgeComparator());
	students.forEach(s -> System.out.println(s));
	
  }
}

输出

--- Sort by name in ascending order ---
Bharat - 28
Lakshman - 25
Ram - 30
Shatrudhan - 23
--- Sort by age in Ascending order ---
Shatrudhan - 23
Lakshman - 25
Bharat - 28
Ram - 30

sort() 与 Comparable

如果 List 的元素类实现了 Comparable 接口,我们可以使用 Listsort 方法对其进行排序,即自然排序。要使用 sort 方法根据其自然顺序对 List 的元素进行排序,我们可以将 nullComparator.naturalOrder() 传递给 sort 方法。

employees.sort(null);
employees.sort(Comparator.naturalOrder());

在上述两种情况下,元素的排序将根据其自然顺序进行。如下例子。
ListSort3.java

package com.concretepage;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class ListSort3 {
  public static void main(String[] args) {
	Employee e1 = new Employee(101, "Mohit");
	Employee e2 = new Employee(102, "Mahesh");
	Employee e3 = new Employee(103, "Jitendra");
	Employee e4 = new Employee(104, "Keshav");	
	List<Employee> employees = Arrays.asList(e1, e2, e3, e4);
	
	System.out.println("--- Sort by employee name in ascending order ---");
	employees.sort(null);
	//employees.sort(Comparator.naturalOrder());
	employees.forEach(s -> System.out.println(s));
	
	System.out.println("--- Sort by employee name in descending order ---");
	employees.sort(Comparator.reverseOrder());
	employees.forEach(s -> System.out.println(s));	
  }
}

class Employee implements Comparable<Employee> {
  private int empId;
  private String empName;
  public Employee(int empId, String empName) {
	this.empId = empId;
	this.empName = empName;
  }
  //Setters and Getters
  
  @Override
  public int compareTo(Employee e) {
	return empName.compareTo(e.getEmpName());
  }
  @Override  
  public String toString(){
	return empId + "-" + empName; 
  }  
}

输出

--- Sort by employee name in ascending order ---
103-Jitendra
104-Keshav
102-Mahesh
101-Mohit
--- Sort by employee name in descending order ---
101-Mohit
102-Mahesh
104-Keshav
103-Jitendra

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com