Guava ---- Ordering排序工具

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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排序工具的主要内容,如果未能解决你的问题,请参考以下文章

[Google Guava] 1-4 排序: Guava强大的”流畅风格比较器”

Guava源码学习Ordering

scala排序——ordering vs ordered

按对象属性对对象的 ArrayList 进行排序

如何在排序列中插入 max(ordering) + 1 的表?

UVa 872 - Ordering 输出全拓扑排序