使用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》
时间: 2024-12-29 18:00:01