zoukankan      html  css  js  c++  java
  • 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》

  • 相关阅读:
    ABS
    Windows Internals 6th Security
    Windows Internals 6th chap4 services
    Windows Internals 6th chap5 Thread
    Python 腾讯云短信,发送手机验证码
    Python 阿里大于发送手机验证码
    爬虫新手学习2-爬虫进阶(urllib和urllib2 的区别、url转码、爬虫GET提交实例、批量爬取贴吧数据、fidder软件安装、有道翻译POST实例、豆瓣ajax数据获取)
    爬虫新手学习1-爬虫基础
    Python Django CMDB项目实战之-3创建form表单,并在前端页面上展示
    Python Django CMDB项目实战之-2创建APP、建模(models.py)、数据库同步、高级URL、前端页面展示数据库中数据
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/6772427.html
Copyright © 2011-2022 走看看