Java Comparator.thenComparing方法介绍

x33g5p2x  于2022-09-23 转载在 Java  
字(7.5k)|赞(0)|评价(0)|浏览(684)

thenComparingComparator 功能接口的默认方法。 Java 8 中引入了 Comparator.thenComparing 方法。 Comparator.thenComparing 返回一个字典顺序比较器,该比较器由 Comparator 实例调用,以使用一组排序键对项目进行排序。当此比较器比较两个相等的元素时,thenComparing 方法确定顺序。我们可以多次使用 Comparator.thenComparing。当我们想通过排序键组确定元素的顺序时,它很有用。对于 intlongdouble 数据类型排序键,Comparator 分别具有 thenComparingIntthenComparingLongthenComparingDouble 默认方法。

Comparator.thenComparing

thenComparing 有以下形式。
1.

default Comparator<T> thenComparing(Comparator<? super T> other)

它返回一个字典顺序比较器和另一个比较器。找到代码片段。

Comparator<Student> compByStdName = Comparator.comparing(Student::getName);
Comparator<Student> schoolComparator1 = Comparator.comparing(Student::getAge) //sort by student age
	.thenComparing(compByStdName); //then sort by student name

首先,比较器将按学生年龄对 Student 的集合进行排序,如果对于某些学生,年龄相同,那么它将按他们的姓名排序。
2.

default <U extends Comparable<? super U>> Comparator<T> thenComparing(Function<? super T,? extends U> keyExtractor)

它返回一个字典顺序比较器,其中包含一个提取 Comparable 排序键的函数。找到代码片段。

Comparator<Student> schoolComparator2 = Comparator.comparing(Student::getSchool) //sort by school natural ordering i.e. city
	.thenComparing(Student::getAge) //then sort by student age
	.thenComparing(Student::getName); //then sort by student name

首先,Student 的集合将按其各自的 School 及其自然顺序排序,如果一些学生根据他们的 School 排序相等,那么这些学生将按他们各自的年龄排序,如果年龄也相等,那么他们将按他们的排序姓名。
3.

default <U> Comparator<T> thenComparing(Function<? super T,? extends U> keyExtractor, Comparator<? super U> keyComparator)

它返回一个字典顺序比较器,其函数提取要与给定 Comparator 比较的键。找到代码片段。

Comparator<Student> schoolComparator3 = Comparator.comparing(Student::getSchool) //sort by school natural ordering i.e. city
	.thenComparing(Student::getSchool, (school1, school2) -> school1.getSname().compareTo(school2.getSname())) //then sort by school name 
	.thenComparing(Student::getAge) //then sort by student age
	.thenComparing(Student::getName); //then sort by student name

首先,Student 的集合将按其各自的 School 及其自然顺序(即在我们的演示中按学校城市)排序,然后如果学生在同一个学校城市,他们将按各自的学校名称排序,如果学生与学校名称相同,按年龄排序,如果学生年龄相同,则按姓名排序。

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

package com.concretepage;
public class School implements Comparable<School> {
  private String sname;
  private String city;  
  public School(String sname, String city) {
	this.sname = sname;
	this.city = city;
  }
  public String getSname() {
        return sname;
  }
  public String getCity() {
        return city;
  }
  @Override
  public int compareTo(School s) {
	return s.getCity().compareTo(city);
  }
}

Student.java

package com.concretepage;
import java.util.Arrays;
import java.util.List;
public class Student {
  private String name;
  private int age;
  private long homeDistance;
  private double weight;
  private School school;

  public Student(String name, int age, long homeDistance, double weight, School school) {
	this.name = name;
	this.age = age;
	this.homeDistance = homeDistance;
	this.weight = weight;
	this.school = school;
  }
  public String getName() {
	return name;
  }
  public int getAge() {
	return age;
  }
  public long getHomeDistance() {
	return homeDistance;
  }
  public double getWeight() {
	return weight;
  }
  public School getSchool() {
	return school;
  }
  public static List<Student> getStudentList() {
	Student s1 = new Student("Ram", 18, 3455, 60.75, new School("AB College", "Noida"));
	Student s2 = new Student("Shyam", 22, 3252, 65.80, new School("RS College", "Gurugram"));
	Student s3 = new Student("Mohan", 18, 1459, 65.20, new School("AB College", "Noida"));
	Student s4 = new Student("Mahesh", 22, 4450, 70.25, new School("RS College", "Gurugram"));
	List<Student> list = Arrays.asList(s1, s2, s3, s4);
	return list;
  }
}

ThenComparingDemo.java

package com.concretepage;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ThenComparingDemo {
  public static void main(String[] args) {
    List<Student> list = Student.getStudentList();
    
    System.out.println("--------Example-1---------"); 
    
    Comparator<Student> compByStdName = Comparator.comparing(Student::getName);
    Comparator<Student> schoolComparator1 = Comparator.comparing(Student::getAge) //sort by student age
    	.thenComparing(compByStdName); //then sort by student name   
    
    Collections.sort(list, schoolComparator1);
    list.forEach(s->System.out.println(s.getName() + "-" + s.getAge()));
    
    System.out.println("--------Example-2---------");   
    
    Comparator<Student> schoolComparator2 = Comparator.comparing(Student::getSchool) //sort by school natural ordering i.e. city
    	.thenComparing(Student::getAge) //then sort by student age
    	.thenComparing(Student::getName); //then sort by student name   
    
    Collections.sort(list, schoolComparator2);
    list.forEach(s->System.out.println(s.getName() + "-" + s.getAge()+ "-" + s.getSchool().getCity()));
    
    System.out.println("--------Example-3---------");    
    
    Comparator<Student> schoolComparator3 = Comparator.comparing(Student::getSchool) //sort by school natural ordering i.e. city
    	.thenComparing(Student::getSchool, (school1, school2) -> school1.getSname().compareTo(school2.getSname())) //then sort by school name 
    	.thenComparing(Student::getAge) //then sort by student age
    	.thenComparing(Student::getName); //then sort by student name 
    
    Collections.sort(list, schoolComparator3);
    list.forEach(s->System.out.println(s.getName() + "-" + s.getAge()+ "-" + s.getSchool().getSname() + "-" + s.getSchool().getCity()));
  }
}

输出

--------Example-1---------
Mohan-18
Ram-18
Mahesh-22
Shyam-22
--------Example-2---------
Mohan-18-Noida
Ram-18-Noida
Mahesh-22-Gurugram
Shyam-22-Gurugram
--------Example-3---------
Mohan-18-AB College-Noida
Ram-18-AB College-Noida
Mahesh-22-RS College-Gurugram
Shyam-22-RS College-Gurugram

Comparator.thenComparingInt

找到 thenComparingInt 方法声明。

default Comparator<T> thenComparingInt(ToIntFunction<? super T> keyExtractor)

它返回一个字典顺序比较器,其中包含一个提取 int 排序键的函数。找到例子。
**ThenComparingIntDemo.java **

package com.concretepage;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ThenComparingIntDemo {
  public static void main(String[] args) {
    List<Student> list = Student.getStudentList();
    
    Comparator<Student> comparator = Comparator.comparing(Student::getName, (s1, s2) -> s1.charAt(0) - s2.charAt(0))
    	.thenComparingInt(Student::getAge);
    
    Collections.sort(list, comparator);
    list.forEach(s->System.out.println(s.getName() + "-" + s.getAge()));    
  }
}

输出

Mohan-18
Mahesh-22
Ram-18
Shyam-22

Comparator.thenComparingLong

找到 thenComparingLong 方法声明。

default Comparator<T> thenComparingLong(ToLongFunction<? super T> keyExtractor)

它返回一个字典顺序比较器,其中包含一个提取 long 排序键的函数。找到例子。
ThenComparingLongDemo.java

package com.concretepage;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ThenComparingLongDemo {
  public static void main(String[] args) {
    List<Student> list = Student.getStudentList();
    
    Comparator<Student> comparator = Comparator.comparing(Student::getName, (s1, s2) -> s1.charAt(0) - s2.charAt(0))
    	.thenComparingLong(Student::getHomeDistance);
    
    Collections.sort(list, comparator);
    list.forEach(s->System.out.println(s.getName() + "-" + s.getHomeDistance()));  
  }
}

输出

Mohan-1459
Mahesh-4450
Ram-3455
Shyam-3252

Comparator.thenComparingDouble

找到 thenComparingDouble 方法声明。

default Comparator<T> thenComparingDouble(ToDoubleFunction<? super T> keyExtractor)

它返回一个字典顺序比较器,其中包含一个提取 double 排序键的函数。找到例子。
ThenComparingDoubleDemo.java

package com.concretepage;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ThenComparingDoubleDemo {
  public static void main(String[] args) {
    List<Student> list = Student.getStudentList();
    
    Comparator<Student> comparator = Comparator.comparing(Student::getName, (s1, s2) -> s1.charAt(0) - s2.charAt(0))
    	.thenComparingDouble(Student::getWeight);
    
    Collections.sort(list, comparator);
    list.forEach(s->System.out.println(s.getName() + "-" + s.getWeight()));         
  }
}

输出

Mohan-65.2
Mahesh-70.25
Ram-60.75
Shyam-65.8

相关文章

微信公众号

最新文章

更多