使用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> { @Override
public 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> {
@Override
public int compare(City city1, City city2) {
return Doubles.compare(city1.getAverageRainfall(), city2.getAverageRainfall());
}
}

输出结果:

參考资料:

《Getting Started with Google Guava》

Guava ---- Ordering排序工具的更多相关文章

  1. [Guava官方文档翻译] 4. 使用Guava Ordering排序 (Ordering Explained)

    本文地址:http://www.cnblogs.com/hamhog/p/3537233.html 示例 assertTrue(byLengthOrdering.reverse().isOrdered ...

  2. 转载:Hadoop排序工具用法小结

    本文转载自Silhouette的文章,原文地址:http://www.dreamingfish123.info/?p=1102 Hadoop排序工具用法小结 发表于 2014 年 8 月 25 日 由 ...

  3. Atitit apache 和guava的反射工具

    Atitit apache 和guava的反射工具 apache1 Spring的反射工具类 ReflectionUtils1 Guava 反射工具2 apache  34             7 ...

  4. Guava限流工具RateLimiter使用

    公司最近在推一个限流工具接入,提供的功能有单机限流.集群限流等.想了解一下限流的原理和设计,看了一下wiki里面有提到用了guava的ratelimiter工具,查了一些资料了解了一下 主要的限流算法 ...

  5. Linux文件排序工具 sort 命令详解

    sort是排序工具,它完美贯彻了Unix哲学:"只做一件事,并做到完美".它的排序功能极强.极完整,只要文件中的数据足够规则,它几乎可以排出所有想要的排序结果,是一个非常优质的工具 ...

  6. 小米开源文件管理器MiCodeFileExplorer-源码研究(8)-文件排序工具类FileSortHelper

    FileSortHelper的核心功能就是,对文件集合FileInfo排序.FileInfo有若干字段,根据字段定义了4种比较器Comparator.调用示例:Collections.sort(Lis ...

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

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

  8. Shell 编程 排序工具 sort 和 uniq

    本篇主要写一些shell脚本排序工具的使用. sort 概述 sort是一个以行为单位对文件内容进行排序的工具,也可以根据不同的数据类型来排序. 用法 sort [选项] 参数 -f:忽略大小写 -b ...

  9. Guava - Ordering

    guava中Ordering类是对Compartor接口的实现,但它也只是一个抽象类. 当调用Ordering.natural()方法时,它就会返回一个NaturalOrdering的对象,Natur ...

随机推荐

  1. 20162304 实验一《Java开发环境的熟悉》实验报告

    Linux基础与Java开发环境 实验内容 1.熟悉Linux基础操作: 2.使用JDK编译.运行简单的Java程序: 实验要求 1.学习<Linux基础入门(新版)> 2.完成实验.撰写 ...

  2. [AtCoder-ARC073F]Many Moves

    题目大意: 有一排n个格子和2枚硬币. 现在有q次任务,每一次要你把其中一枚硬币移到x的位置上,移动1格的代价是1. 两枚硬币不能同时移动,任务必须按次序完成. 现在告诉你两枚硬币初始状态所在的位置a ...

  3. 通过wifi上网,桥接模式下virtualBox虚拟机无法连上网的解决办法

    https://jingyan.baidu.com/article/948f59242e601dd80ff5f929.html

  4. apose 根据excel 导出模版

    string file = Server.MapPath("~/Excel/ZWxxtj.xls");            DataSet ds = new DataSet(); ...

  5. serial-input, parallel-output (SIPO) chip : TPIC6595 , 74HC164 , 74HC4094 or 74HC595

    http://electronics.stackexchange.com/questions/6676/which-sipo-chip-is-better-74hc4094-or-74hc595-or ...

  6. Mac OS上的远程桌面

    最近在做Mac上面的开发,经常在win7和Mac两台电脑上操作,两个键盘,两个鼠标,搞得头都大了,所以干脆把Mac机器远程到win7上面,统一来做,方便些..哈哈!说实话,Mac键盘那些按键真还有点特 ...

  7. ecmall用户登录后自动退出解决方法

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  8. .NET:CLR via C# The CLR’s Execution Model

    The CLR’s Execution Model The core features of the CLR memory management. assembly loading. security ...

  9. 第十章 OPENWRT安装nohup,因为不明原因nohup没有安装

    Available pacaagess里找了coreutils-nohup包,安装好就行了 运行nohup自己的程序的时候可能会出现一下: root@OpenWrt:~# nohup: ignorin ...

  10. Android ListView工作原理完全解析,带你从源码的角度彻底理解

    版权声明:本文出自郭霖的博客,转载必须注明出处.   目录(?)[+] Adapter的作用 RecycleBin机制 第一次Layout 第二次Layout 滑动加载更多数据   转载请注明出处:h ...