使用Guava的排序工具类, 快速实现对象的单变量排序和多变量排序, 让你的开发效率爆炸...

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.Iterator;
import java.util.List;public class CityByPopluation implements Comparator<City> {@Overridepublic int compare(City city1, City city2) {return Ints.compare(city1.getPopulation(), city2.getPopulation());}public static void main(String[] args) {CityByPopluation cityByPopluation = new CityByPopluation();CityByRainfall cityByRainfall = new CityByRainfall();// 根据第二个参数排序City city1 = new City("Beijing", 100000, 55.0);City city2 = new City("Shanghai", 100000, 45.0);City city3 = new City("ShenZhen", 100000, 33.8);List<City> cities = Lists.newArrayList(city1, city2, city3);/*** 单参数排序*/// 排序反转Ordering<City> firstOrdering = Ordering.from(cityByRainfall).reverse();Collections.sort(cities, firstOrdering);Iterator<City> cityByRainfallIterator = cities.iterator();while (cityByRainfallIterator.hasNext()) {System.out.println(cityByRainfallIterator.next().getCityName());}System.out.println("I was evil dividing line");/*** 多参数排序*/Ordering<City> secondaryOrdering = Ordering.from(cityByPopluation).compound(cityByRainfall);Collections.sort(cities, secondaryOrdering);Iterator<City> cityIterator = cities.iterator();while (cityIterator.hasNext()) {System.out.println(cityIterator.next().getCityName());}/*** 取得最小最大值*/Ordering<City> ordering = Ordering.from(cityByRainfall);// 降雨量最高的2个城市List<City> topTwo = ordering.greatestOf(cities, 2);Iterator<City> topTwoIterator = topTwo.iterator();while (topTwoIterator.hasNext()) {System.out.println("降雨量最高城市" + topTwoIterator.next().getCityName());}// 降雨量最低的一个城市List<City> bottomOne = ordering.leastOf(cities, 1);Iterator<City> bottomOneIterator = bottomOne.iterator();while (bottomOneIterator.hasNext()) {System.out.println("降雨量最低的城市" + bottomOneIterator.next().getCityName());}}
}

City类:

/*** Created by wenniuwuren on 2015/6/4.*/
public class City {private String cityName;private Integer population;private Double averageRainfall;public City(String cityName, Integer population, Double averageRainfall) {this.cityName = cityName;this.population = population;this.averageRainfall = averageRainfall;}public String getCityName() {return cityName;}public void setCityName(String cityName) {this.cityName = cityName;}public Integer getPopulation() {return population;}public void setPopulation(Integer population) {this.population = population;}public Double getAverageRainfall() {return averageRainfall;}public void setAverageRainfall(Double averageRainfall) {this.averageRainfall = averageRainfall;}}

CityByRainfall类:

import com.google.common.primitives.Doubles;import java.util.Comparator;public class CityByRainfall implements Comparator<City> {@Overridepublic int compare(City city1, City city2) {return Doubles.compare(city1.getAverageRainfall(), city2.getAverageRainfall());}
}

输出结果:

参考资料:

《Getting Started with Google Guava》

Guava ---- Ordering排序工具相关推荐

  1. Ordering排序工具一

    使用Guava的排序工具类, 快速实现对象的单变量排序和多变量排序, 让你的开发效率爆炸... import com.google.common.collect.Lists; import com.g ...

  2. Guava Ordering 排序类

    前言: Guava工程是Google开发的Java核心依赖库,例如:集合 [collections] .缓存 [caching] .原生类型支持 [primitives support] .并发库 [ ...

  3. Ordering 排序类 - Google Guava

    一.Ordering排序类简介 排序器 Ordering 是 Guava流畅风格比较器 Comparator 的实现,它可以用来为构建复杂的比较器,以完成集合排序的功能. 从实现上说,Ordering ...

  4. Guava:Ordering对象多属性排序

    Guava:Ordering对象多属性排序 Person多字段排序结果 Person多字段排序结果 针对Person对象,根据年龄升序,字段为空放最前面,再根据名称进行升序,空字符放最后面,最后根据排 ...

  5. 实现一个通用的中英文排序工具

    前言 利用Collator类可以轻松实现排序,但是我们可能有各种model都需要进行排序,这样就会有一个问题,如果单独为每个model写一段排序代码,代码重复量很大. 所以我打算写一个通用的工具,使用 ...

  6. Guava Ordering

    Ordering是Guava基于比较器[Comparator]的实现,它可以用来为构建复杂的比较器,以完成集合排序的功能.从实现上说,Ordering实例就是一个特殊的Comparator实例.Ord ...

  7. wireshark和tcpdump抓包TCP乱序和重传怎么办?PCAP TCP排序工具分享

    点击上方↑↑↑蓝字[协议分析与还原]关注我们 " 介绍TCP排序方法,分享一个Windows版的TCP排序工具." 在分析协议的过程中,不可避免地需要抓包. 无论抓包条件如何优越, ...

  8. 活动排序工具之双代号网络(AOA)与单代号网络(AON)[cont.]

    箭线图ADM/双代号网络AOA 图示 箭线表示活动 节点表示一个活动的开始或结束 三要素:结点.箭线.线路 唯一使用虚活动的活动排序工具,虚活动用虚线箭头表示,没有历时,不需资源,只表达活动关系的需要 ...

  9. Case study:在数据库网页中设计数据排序工具

    一.目的 该笔记的目的是引导读者在已搭建的数据库网页的基础上,利用JS设计数据排序工具.其效果如图1所示."Order by"下拉列表框由一系列字段组成,如"Locati ...

最新文章

  1. NO.7 今天我们是实用派,看看业务选择和部署以及常用故障解决方案是怎么做的...
  2. 新一届最强预训练模型上榜,出于BERT而胜于BERT
  3. 这7款实用windows软件,太让人惊喜了!
  4. Linux 操作系统原理 — 网络 I/O 虚拟化
  5. 心智图/思维导图(Mind Map/Mind Mapping),思维导图介绍
  6. PHP根据地址 获取坐标 thinkphp根据地址 获取坐标(百度地图)
  7. 编写Web前端代码的注意事项
  8. uuid生成_php如何生成 uuid(总结)
  9. SpringCloud+OAuth2+Redis实现的微服务统一认证授权
  10. component、constituent、element、ingredient的区别
  11. springcloud之gateway服务网关
  12. ubuntu 安装go语言编译器 liteide
  13. 一次Full GC分析
  14. Markdown - 推荐一个markdown在线编辑器
  15. 【小程序从0到1】网络数据请求——request合法域名|GET|POST|跨域?Ajax?
  16. redisCluster之主观下线与客观下线
  17. EDIUS 9 Setup Failed 0x80070666解决方法
  18. kubernetes使用过程中问题汇总
  19. boundRect.tl和boundRect.br什么意思
  20. 从深交所2018年创新课题看券商的数智化布局

热门文章

  1. DB-Engines 9月数据库排名:SnowFlake坐上了火箭
  2. 微信小程序应用都有哪些
  3. 【生活】已经从官网购买iPad,单独购买AppleCare+服务
  4. 抖音SEO排名优化源代码开发搭建技术分析
  5. VxWorks 操作系统简介
  6. uniapp小程序鼠标按下抬起,@input=“down“轻松解决
  7. iframe.contentWindow 介绍
  8. 免费的IP地址查询API接口
  9. 通过dockerfile创建镜像启动容器
  10. QT之窗口背景颜色和图片设置