Guava-流式风格比较器Ordering

2024-04-21 07:08

本文主要是介绍Guava-流式风格比较器Ordering,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Ordering是Guava流畅风格比较器[Comparator]的实现,它可以用来为构建复杂的比较器,以完成集合排序的功能。从实现上说,Ordering实例就是一个特殊的Comparator实例。Ordering把很多基于Comparator的静态方法包装为自己的实例方法,并且提供了链式调用方法,来定制和增强现有的比较器。
创建排序器:

  1. natural() 对可排序类型做自然排序,如数字按大小,日期按先后排序
  2. usingToString() 按对象的字符串形式做字典排序
  3. from(Comparator) 把给定的Comparator转化为排序器

常用方法:

  1. reverse() 获取语义相反的排序器
  2. nullsFirst() 使用当前排序器,但额外把null值排到最前面。
  3. nullsLast() 使用当前排序器,但额外把null值排到最后面。
  4. compound(Comparator) 合成另一个比较器,以处理当前排序器中的相等情况。
  5. lexicographical() 基于处理类型T的排序器,返回该类型的可迭代对象Iterable的排序器。
  6. onResultOf(Function) 对集合中元素调用Function,再按返回值用当前排序器排序。

运用排序器:

  1. greatestOf(Iterable iterable, int k) 获取可迭代对象中最大的k个元素。
  2. isOrdered(Iterable) 判断可迭代对象是否已按排序器排序:允许有排序值相等的元素。
  3. sortedCopy(Iterable) 判断可迭代对象是否已严格按排序器排序:不允许排序值相等的元素.
  4. min(E, E) 返回两个参数中最小的那个。如果相等,则返回第一个参数。
  5. min(E, E, E, E…) 返回多个参数中最小的那个。如果有超过一个参数都最小,则返回第一个最小的参数。
  6. min(Iterable) 返回迭代器中最小的元素。如果可迭代对象中没有元素,则抛出NoSuchElementException。

Demo如下:

import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
import com.google.common.primitives.Ints;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class StudentByAge implements Comparator<Student> {@Overridepublic int compare(Student o1, Student o2) {return Ints.compare(o1.getAge(), o2.getAge());}public static void main(String[] args) {StudentByAge studentByAge = new StudentByAge();StudentByScore studentByScore = new StudentByScore();Student s1 = new Student("Li", 21, 55.0);Student s2 = new Student("Zhang", 22, 65.0);Student s3 = new Student("Wang", 20, 78.5);Student s4 = new Student("Sun", 21, 88.0);Student s5 = new Student("Wang", 20, 75.0);List<Student> students = Lists.newArrayList(s1, s2, s3, s4, s5);/*** 根据年龄排序正序*/Ordering<Student> orderByAge = Ordering.from(studentByAge);Collections.sort(students, orderByAge);for (Student item : students) {System.out.println(item);}System.out.println("-----------------------------------------------------");/*** 根据年龄排序倒叙*/Ordering<Student> orderByAgeReverse = Ordering.from(studentByAge).reverse();Collections.sort(students, orderByAgeReverse);for (Student item : students) {System.out.println(item);}System.out.println("-----------------------------------------------------");/*** 先根据年龄排序,再根据分数排序*/Ordering<Student> oderByAgeAndScore = Ordering.from(studentByAge).compound(studentByScore);Collections.sort(students, oderByAgeAndScore);for (Student item : students) {System.out.println(item);}System.out.println("-----------------------------------------------------");/*** 找出分数最高的两个同学*/Ordering<Student> oderByScore = Ordering.from(studentByScore);List<Student> topTwo = oderByScore.greatestOf(students, 2);for (Student item : topTwo) {System.out.println(item);}}
}
import com.google.common.primitives.Doubles;
import java.util.Comparator;
public class StudentByScore implements Comparator<Student> {@Overridepublic int compare(Student s1, Student s2) {return Doubles.compare(s1.getScore(), s2.getScore());}
}
public class Student {private String name;private Integer age;private Double score;public Student(String name, Integer age, Double score) {this.name = name;this.age = age;this.score = score;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Double getScore() {return score;}public void setScore(Double score) {this.score = score;}@Overridepublic String toString() {final StringBuffer sb = new StringBuffer("Student{");sb.append("name='").append(name).append('\'');sb.append(", age=").append(age);sb.append(", score=").append(score);sb.append('}');return sb.toString();}
}

这篇关于Guava-流式风格比较器Ordering的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/922453

相关文章

Python如何实现高效的文件/目录比较

《Python如何实现高效的文件/目录比较》在系统维护、数据同步或版本控制场景中,我们经常需要比较两个目录的差异,本文将分享一下如何用Python实现高效的文件/目录比较,并灵活处理排除规则,希望对大... 目录案例一:基础目录比较与排除实现案例二:高性能大文件比较案例三:跨平台路径处理案例四:可视化差异报

MyBatis流式查询两种实现方式

《MyBatis流式查询两种实现方式》本文详解MyBatis流式查询,通过ResultHandler和Cursor实现边读边处理,避免内存溢出,ResultHandler逐条回调,Cursor支持迭代... 目录MyBATis 流式查询详解:ResultHandler 与 Cursor1. 什么是流式查询?

MySQL中比较运算符的具体使用

《MySQL中比较运算符的具体使用》本文介绍了SQL中常用的符号类型和非符号类型运算符,符号类型运算符包括等于(=)、安全等于(=)、不等于(/!=)、大小比较(,=,,=)等,感兴趣的可以了解一下... 目录符号类型运算符1. 等于运算符=2. 安全等于运算符<=>3. 不等于运算符<>或!=4. 小于运

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin

SpringBoot中使用Flux实现流式返回的方法小结

《SpringBoot中使用Flux实现流式返回的方法小结》文章介绍流式返回(StreamingResponse)在SpringBoot中通过Flux实现,优势包括提升用户体验、降低内存消耗、支持长连... 目录背景流式返回的核心概念与优势1. 提升用户体验2. 降低内存消耗3. 支持长连接与实时通信在Sp

python编写朋克风格的天气查询程序

《python编写朋克风格的天气查询程序》这篇文章主要为大家详细介绍了一个基于Python的桌面应用程序,使用了tkinter库来创建图形用户界面并通过requests库调用Open-MeteoAPI... 目录工具介绍工具使用说明python脚本内容如何运行脚本工具介绍这个天气查询工具是一个基于 Pyt

使用Python创建一个功能完整的Windows风格计算器程序

《使用Python创建一个功能完整的Windows风格计算器程序》:本文主要介绍如何使用Python和Tkinter创建一个功能完整的Windows风格计算器程序,包括基本运算、高级科学计算(如三... 目录python实现Windows系统计算器程序(含高级功能)1. 使用Tkinter实现基础计算器2.

springboot整合阿里云百炼DeepSeek实现sse流式打印的操作方法

《springboot整合阿里云百炼DeepSeek实现sse流式打印的操作方法》:本文主要介绍springboot整合阿里云百炼DeepSeek实现sse流式打印,本文给大家介绍的非常详细,对大... 目录1.开通阿里云百炼,获取到key2.新建SpringBoot项目3.工具类4.启动类5.测试类6.测

Spring AI集成DeepSeek实现流式输出的操作方法

《SpringAI集成DeepSeek实现流式输出的操作方法》本文介绍了如何在SpringBoot中使用Sse(Server-SentEvents)技术实现流式输出,后端使用SpringMVC中的S... 目录一、后端代码二、前端代码三、运行项目小天有话说题外话参考资料前面一篇文章我们实现了《Spring

Java 8 Stream filter流式过滤器详解

《Java8Streamfilter流式过滤器详解》本文介绍了Java8的StreamAPI中的filter方法,展示了如何使用lambda表达式根据条件过滤流式数据,通过实际代码示例,展示了f... 目录引言 一.Java 8 Stream 的过滤器(filter)二.Java 8 的 filter、fi