Guava ---- Ordering排序工具
使用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排序工具的更多相关文章
- [Guava官方文档翻译] 4. 使用Guava Ordering排序 (Ordering Explained)
本文地址:http://www.cnblogs.com/hamhog/p/3537233.html 示例 assertTrue(byLengthOrdering.reverse().isOrdered ...
- 转载:Hadoop排序工具用法小结
本文转载自Silhouette的文章,原文地址:http://www.dreamingfish123.info/?p=1102 Hadoop排序工具用法小结 发表于 2014 年 8 月 25 日 由 ...
- Atitit apache 和guava的反射工具
Atitit apache 和guava的反射工具 apache1 Spring的反射工具类 ReflectionUtils1 Guava 反射工具2 apache 34 7 ...
- Guava限流工具RateLimiter使用
公司最近在推一个限流工具接入,提供的功能有单机限流.集群限流等.想了解一下限流的原理和设计,看了一下wiki里面有提到用了guava的ratelimiter工具,查了一些资料了解了一下 主要的限流算法 ...
- Linux文件排序工具 sort 命令详解
sort是排序工具,它完美贯彻了Unix哲学:"只做一件事,并做到完美".它的排序功能极强.极完整,只要文件中的数据足够规则,它几乎可以排出所有想要的排序结果,是一个非常优质的工具 ...
- 小米开源文件管理器MiCodeFileExplorer-源码研究(8)-文件排序工具类FileSortHelper
FileSortHelper的核心功能就是,对文件集合FileInfo排序.FileInfo有若干字段,根据字段定义了4种比较器Comparator.调用示例:Collections.sort(Lis ...
- wireshark和tcpdump抓包TCP乱序和重传怎么办?PCAP TCP排序工具分享
点击上方↑↑↑蓝字[协议分析与还原]关注我们 " 介绍TCP排序方法,分享一个Windows版的TCP排序工具." 在分析协议的过程中,不可避免地需要抓包. 无论抓包条件如何优越, ...
- Shell 编程 排序工具 sort 和 uniq
本篇主要写一些shell脚本排序工具的使用. sort 概述 sort是一个以行为单位对文件内容进行排序的工具,也可以根据不同的数据类型来排序. 用法 sort [选项] 参数 -f:忽略大小写 -b ...
- Guava - Ordering
guava中Ordering类是对Compartor接口的实现,但它也只是一个抽象类. 当调用Ordering.natural()方法时,它就会返回一个NaturalOrdering的对象,Natur ...
随机推荐
- 在windows上安装和启动Elasticseach、Kibana
写在前面的话:读书破万卷,编码如有神-------------------------------------------------------------------- 参考内容: <Ela ...
- SCOJ 4427: Miss Zhao's Graph dp
4427: Miss Zhao's Graph 题目连接: http://acm.scu.edu.cn/soj/problem.action?id=4427 Description Mr Jiang ...
- Codeforces Beta Round #6 (Div. 2 Only) C. Alice, Bob and Chocolate 水题
C. Alice, Bob and Chocolate 题目连接: http://codeforces.com/contest/6/problem/C Description Alice and Bo ...
- CROC 2016 - Qualification C. Hostname Aliases map
C. Hostname Aliases 题目连接: http://www.codeforces.com/contest/644/problem/C Description There are some ...
- react-native-image-zoom-viewer学习
github原地址 react-native-image-zoom-viewer实现了类似微信朋友圈浏览图片的效果,点击小图片实现浏览原图效果. 安装: npm i react-native-imag ...
- nodejs中间件拦截,express不登录无法进入后台页面
22.设置拦截 只有登录才能进入到后台页面,不登录无法进入 如果登陆成功, 写入session, 参数 uid uid=123dsfjksldfjsl 检测登陆, 请求中 session 是否包含 u ...
- Django 中文显示
Django中文显示: 1.如要在.py文件中显示中文,在文件首行加上:# -*- coding: utf-8 -*- 2.如要在html文件中显示中文,要将文件保存为UTF-8格式 3.在setti ...
- java常用工具方法2
/* * Copyright 2005 Joe Walker * * Licensed under the Apache License, Version 2.0 (the "License ...
- Java常量字符串String理解 String理解
以前关于String的理解仅限于三点:1.String 是final类,不可继承2.String 类比较字符串相等时时不能用“ == ”,只能用 "equals" 3.Strin ...
- Spring工具类ToStringBuilder用法简介
比如说我们需要打印某个方法的User参数对象 package test; /** * * @author zhengtian * @time 2012-6-28 */ public class Use ...