Sorting an ArrayList by using a custom Comparator

I am quite sure this is an old topic, but may serve to help some guys. Its easy to under stand and clears some basics on sorting collections using custom Comparators.
First lets create a class which will serve as the objects to our generic list, here we are creating a class called City, as habit i have generated the hashcode, equals, tostring and a parameterized constructor for the class.We have name and population as two fields and the custom comparator will use population to sort the City objects in the list.
  1. class City{
  2. private String name;
  3. private Long population;
  4. public String getName() {
  5. return name;
  6. }
  7. public void setName(String name) {
  8. this.name = name;
  9. }
  10. public Long getPopulation() {
  11. return population;
  12. }
  13. public void setPopulation(Long population) {
  14. this.population = population;
  15. }
  16. @Override
  17. public int hashCode() {
  18. final int prime = 31;
  19. int result = 1;
  20. result = prime * result + ((name == null) ? 0 : name.hashCode());
  21. result = prime * result
  22. + ((population == null) ? 0 : population.hashCode());
  23. return result;
  24. }
  25. @Override
  26. public boolean equals(Object obj) {
  27. if (this == obj)
  28. return true;
  29. if (obj == null)
  30. return false;
  31. if (getClass() != obj.getClass())
  32. return false;
  33. City other = (City) obj;
  34. if (name == null) {
  35. if (other.name != null)
  36. return false;
  37. } else if (!name.equals(other.name))
  38. return false;
  39. if (population == null) {
  40. if (other.population != null)
  41. return false;
  42. } else if (!population.equals(other.population))
  43. return false;
  44. return true;
  45. }
  46. @Override
  47. public String toString() {
  48. return "City [name=" + name + ", population=" + population + "]";
  49. }
  50. public City(String name, Long population) {
  51. super();
  52. this.name = name;
  53. this.population = population;
  54. }
  55. }
Now lets write the PopulationComparator which will take the City objects and Compare them based on population.
  1. class PopulationComparator implements Comparator<City>{
  2. @Override
  3. public int compare(City o1, City o2) {
  4. return Long.compare(o1.getPopulation(),o2.getPopulation());
  5. }
  6. }
Here we use java.util.Comparator, which is generic and thus displays Generic collections inheritance. It takes a City parameter type thus making it type safe. It uses the Long.compare to compare the population.
Now lets get the main method working and call the utility method of Collections class sort to sort the ArrayList,
  1. public class SortArrayList {
  2. public static void main(String[] args) {
  3. List<City> cityList = new ArrayList<>();
  4. cityList.add(new City("NewYork",10000l));
  5. cityList.add(new City("London",20000l));
  6. cityList.add(new City("Mumbai",30000l));
  7. cityList.add(new City("Moscow",20000l));
  8. cityList.add(new City("Tokyo",10000l));
  9. Collections.sort(cityList,new PopulationComparator());
  10. for(City city : cityList){
  11. System.out.println(city.getName());
  12. }
  13. }
  14. }
And After sorting the List we get sorted List as result;
  1. NewYork
  2. Tokyo
  3. London
  4. Moscow
  5. Mumbai

Thanks for reading, comment if helpful.

Comments

Popular posts from this blog

Java Interview : Threads

Spring Framework Interview Notes : Part Two Wiring

Card Dealer In Java in Less than 5 minutes