Using the Guava Sorting tool class, the high-speed implementation of the object's single-variable ordering and multivariable sequencing, so that your development efficiency exploded ...
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> {@ overridepublic 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 ();//Sort City City1 by the second parameter = 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);/** * Single-parameter sort *///sort reversal 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");/** * Multi-parameter ordering */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 ());} /** * Obtains the minimum maximum value */ordering<city> ordering = Ordering.from (citybyrainfall);//The 2 cities with the highest rainfall list<city> TopTwo = Ordering.greatestof (cities, 2);iterator<city> toptwoiterator = Toptwo.iterator (); while ( Toptwoiterator.hasnext ()) {System.out.println ("highest rainfall City" + toptwoiterator.next (). Getcityname ());} A city with the lowest rainfall list<city> bottomone = ordering.leastof (cities, 1);iterator<city> bottomoneiterator = Bottomone.iterator (); while (Bottomoneiterator.hasnext ()) {System.out.println ("City with lowest rainfall" + bottomoneiterator.next () . Getcityname ());}}
City class:
/** * 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 class:
Import Com.google.common.primitives.doubles;import Java.util.comparator;public class Citybyrainfall implements comparator<city> {@Overridepublic int compare (city city1, City City2) {return Doubles.compare ( City1.getaveragerainfall (), City2.getaveragerainfall ());}}
Output Result:
References:
"Getting Started with Google guava"
Guava----Ordering Sorting tool