使用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. poj2117 Electricity

      试题描述 求一个图删除一个点之后,联通块最多有多少. 输入 多组数据.第一行两个整数 P,C 表示点数和边数.接下来 C 行每行两个整数 p1,p2,表示 p1 与 p2 有边连接,保证无重边.读 ...

  2. python开发_tarfile_文档归档压缩|解压缩

    ''' python中的tarfile模块实现文档的归档压缩和解压缩 功能: 把工作空间下面的所有文件,打包生成一个tar文件 同时提供一个方法把该tar文件中的一些文件解压缩到 指定的目录中 ''' ...

  3. Codeforces Beta Round #10 C. Digital Root 数学

    C. Digital Root 题目连接: http://www.codeforces.com/contest/10/problem/C Description Not long ago Billy ...

  4. April Fools Day Contest 2016 D. Rosetta Problem

    D. Rosetta Problem 题目连接: http://www.codeforces.com/contest/656/problem/D Description ++++++++[>+& ...

  5. 用python slearning类库实现数据挖掘(python3.x)

    Summary of test0 data : source data source code : in test0 file reference : - Reference Website / - ...

  6. vscode 插件设置

    VS Code 安装插件 prettier Beautify vscode 首选项 --> 设置 "editor.detectIndentation": false, &qu ...

  7. TPS61040/61041 开关电源稳压器(DC-DC) ADJUST

    Variable Control Voltage Output Voltage Adjust This method is accomplished by connecting a variable ...

  8. 解决office2007-安装程序找不到office.zh-cn\Setup.xml

    安装Microsoft Office Project Standard 2007时出现了小问题,经过百度google一番后才发现安装office2007与安装vs2008有着紧密的联系,参见:http ...

  9. 用CSS3来代替JS实现交互

    [CSS3和JS] 对于CSS了解的同学都知道,CSS的实现是最底层的,在实现方式和性能上都不是,JS这种提供接口的脚本可比的:从CSS3的动画和JS动画对比角度来看两者,会更清晰:而且随着前端框架的 ...

  10. 菜鸟的mongoDB学习---(七)MongoDB 备份(mongodump)与恢复(mongorerstore)

    MongoDB数据备份 在Mongodb中我们使用mongodump命令来备份MongoDB数据. 该命令能够导出全部数据到指定文件夹中. mongodump命令能够通过參数指定导出的数据量级转存的s ...