场景
Java核心工具库Guava介绍以及Optional和Preconditions使用进行非空和数据校验:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/127683387
在上面引入Guava的基础上,看一下Guava的常用Ordering的常用方法。
Ordering是Guava流畅风格比较器Comparator的实现,它可以用来构建复杂的比较器,以完成排序的功能。
从实现上说Ordering实例就是一个特殊的Comparator实例。Ordering把很多基于Comparator的静态方法比如
Collections.max包装成自己的实例方法(非静态方法),并且提供了链式调用方法,来定制和增强现有的比较器。
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
创建排序器
常见的排序器可以由下面的静态方法创建
1、natural()
对可排序类型做自然排序,如数字按照大小,日期按照先后顺序
List<Integer> list = Lists.newArrayList(1,5,3,6,9,7,8); list.sort(Ordering.natural()); System.out.println(list.toString());//[1, 3, 5, 6, 7, 8, 9]
2、usingToString()
按对象的字符串形式做字典排序,即使用toString()返回的字符串按字典顺序进行排序。
List<String> stringList = Lists.newArrayList("zhangsan","lisi","wangwu"); Collections.sort(stringList,Ordering.usingToString()); System.out.println(stringList.toString());//[lisi, wangwu, zhangsan] list.sort(Ordering.usingToString()); System.out.println(list.toString());//[1, 3, 5, 6, 7, 8, 9]
3、from()
把给定的Comparator转化为排序器
User zhangsan = new User("张三",20,new Company()); User lisi = new User("李四",30,new Company()); List<User> users = Lists.newArrayList(zhangsan,lisi); //按照age字段进行排序 Ordering<User> ordering1 = Ordering.from(Comparator.comparingInt(u -> u.getAge())); users.sort(ordering1); System.out.println(users.toString());
链式调用方法
1、reverse()
获取语义相反的排序器
List<Integer> list = Lists.newArrayList(1,5,3,6,9,7,8); list.sort(Ordering.natural().reverse()); System.out.println(list.toString());//[9, 8, 7, 6, 5, 3, 1]
2、nullsFirst()
使用当前排序器,但额外把null值排到最前面
List<Integer> list1 = Arrays.asList(1, 5, null, 3, 8, 2); //Collections.sort(list1); // 出现异常... Collections.sort(list1, Ordering.natural().nullsFirst()); System.out.println(list1);//[null, 1, 2, 3, 5, 8]
3、nullsLast()
使用当前排序器,但额外把null值排到最后面
List<Integer> list2 = Arrays.asList(1, 5, null, 3, 8, 2); //Collections.sort(list1); // 出现异常... Collections.sort(list1, Ordering.natural().nullsLast()); System.out.println(list1);//[1, 2, 3, 5, 8, null]
4、compound(Comparator)
成另一个比较器,以处理当前排序器中的相等情况
首先按照年龄进行排序,如果年龄相同则按照薪水排序
Comparator<UserPojo> objectComparator = Comparator.comparingInt(u -> u.getAge()); Comparator<UserPojo> objectComparator2 = Comparator.comparing(u -> u.getSalary()); Ordering<UserPojo> ordering1 = Ordering.from(objectComparator).compound(objectComparator2); UserPojo zhangsan = new UserPojo("张三",20,new Company(),1000); UserPojo lisi = new UserPojo("李四",30,new Company(),2000); UserPojo wangwu = new UserPojo("王五",30,new Company(),3000); UserPojo amao = new UserPojo("杨猫",30,new Company(),4000); List<UserPojo> users = Lists.newArrayList(zhangsan,lisi,wangwu,amao); users.sort(ordering1); //[UserPojo(name=张三, age= 20,company=Company(name=null, address=null), salary=1000), UserPojo(name=李四, age= 30,company=Company(name=null, address=null), salary=2000), UserPojo(name=王五, age= 30,company=Company(name=null, address=null), salary=3000), UserPojo(name=杨猫, age= 30,company=Company(name=null, address=null), salary=4000)] System.out.println(users.toString());
5、onResultOf(Function)
把比较器的元素使用Function函数转化成一个值result,再对这个值应用Ordering的比较方法。result的排序顺序就是最后的排序顺序
List<UserPojo> users2 = Lists.newArrayList(zhangsan,lisi,wangwu,amao,null); Ordering<UserPojo> fOrdering = Ordering.natural().onResultOf(new Function<UserPojo, Comparable>() { @Override public @Nullable Comparable apply(@Nullable UserPojo userPojo) { return userPojo.getSalary(); } }).nullsFirst(); users2.sort(fOrdering); //[null, UserPojo(name=张三, age= 20,company=Company(name=null, address=null), salary=1000), UserPojo(name=李四, age= 30,company=Company(name=null, address=null), salary=2000), UserPojo(name=王五, age= 30,company=Company(name=null, address=null), salary=3000), UserPojo(name=杨猫, age= 30,company=Company(name=null, address=null), salary=4000)] System.out.println(users2.toString());
注意:
注意链式排序器的调用顺序--从右往左的顺序,上面Ordering.natural().onResultOf().nullsFirst();
先调用apply方法获取salary值,并把salary为null的元素放到最前面,然后把剩下的进行自然排序
所以如果将nullsFirst放在onResultOf的左边则会报异常
运用排序器
1、greatestOf(Iterable iterable,int k)
获取可迭代对象中最大的k个元素
List<Integer> result = Ordering.natural().greatestOf(new ArrayList<>(Arrays.asList(1,2,4,5)),2); System.out.println(result);//[5, 4]
2、leastOf(Iterable iterable,int k)
对元素按照从小到大排序,并返回前k个元素
List<Integer> result1 = Ordering.natural().leastOf(new ArrayList<>(Arrays.asList(1,2,4,5)),2); System.out.println(result1);//[1, 2]
3、isOrdered(Iterable)
判断可迭代对象是否已按照排序器排序:允许有排序值相等的元素
List<Integer> list = Arrays.asList(1, 5, 3, 8, 2, 2); Collections.sort(list); boolean order = Ordering.natural().isOrdered(list); System.out.println(order);//true
4、sortedCopy(Iterable)
返回一个新的已经排序的列表,原来的列表顺序不会变
List<Integer> nums = new ArrayList<>(Arrays.asList(4, 1, 3)); List<Integer> resultCopy = Ordering.natural().sortedCopy(nums); System.out.println(resultCopy); // [1, 3, 4] System.out.println(nums); // [4, 1, 3]
5、min(E,E,...)
返回最小值,如果有多个,则返回第一个
Integer min = Ordering.natural().min(1,3,4,1); System.out.println(min);
6、max(E,E,...)
返回最大值,如果有多个,则返回第一个
Integer max = Ordering.natural().max(1,3,4,1); System.out.println(max);
标签:Guava,流畅,name,Ordering,UserPojo,排序,println,null From: https://www.cnblogs.com/badaoliumangqizhi/p/16862496.html