SHARE
TWEET

Comparator

a guest Sep 18th, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Person Class
  2. =========
  3. package Cls;
  4.  
  5. public class Person {
  6.  
  7.     public String name;
  8.     public int height;
  9.     public int weight;
  10.  
  11.     public Person(String name, int height, int weight) {
  12.         this.name = name;
  13.         this.height = height;
  14.         this.weight = weight;
  15.     }
  16.  
  17.     public String getName() {
  18.         return name;
  19.     }
  20.  
  21.     public int getHeight() {
  22.         return height;
  23.     }
  24.  
  25.     public int getWeight() {
  26.         return weight;
  27.     }
  28.  
  29.     @Override
  30.     public String toString() {
  31.         return "Person{" +
  32.                 "name='" + name + '\'' +
  33.                 ", height=" + height +
  34.                 ", weight=" + weight +
  35.                 '}';
  36.     }
  37. }
  38.  
  39. =======
  40. package Cls;
  41.  
  42. import java.util.Comparator;
  43.  
  44. public class ComparePersonByName implements Comparator<Person>{
  45.  
  46.     @Override
  47.     public int compare(Person p1, Person p2) {
  48.  
  49.         return p1.getName().compareTo(p2.getName());
  50.     }
  51. }
  52.  
  53. ========
  54. package Cls;
  55.  
  56. import java.util.Comparator;
  57.  
  58. public class ComparePersonByHeight implements Comparator<Person> {
  59.  
  60.     @Override
  61.     public int compare(Person p1, Person p2) {
  62.  
  63.  
  64.         if(p1.getHeight() < p2.getHeight()){
  65.             return -1;
  66.         }else if(p1.getHeight() > p2.getHeight()){
  67.             return 1;
  68.         }else{
  69.             return 0;
  70.         }
  71.     }
  72. }
  73.  
  74. ========
  75.  
  76. package Cls;
  77.  
  78. import java.util.Comparator;
  79.  
  80. public class ComparePersonByWeight implements Comparator<Person> {
  81.  
  82.     @Override
  83.     public int compare(Person p1, Person p2) {
  84.         if(p1.getWeight() < p2.getWeight()){
  85.             return -1;
  86.         }else if(p1.getWeight() > p2.getWeight()){
  87.             return 1;
  88.         }else{
  89.             return 0;
  90.         }
  91.  
  92.     }
  93.  
  94.  
  95.  
  96. }
  97. ========
  98.  
  99. package Tester;
  100.  
  101. import Cls.ComparePersonByHeight;
  102. import Cls.ComparePersonByName;
  103. import Cls.ComparePersonByWeight;
  104. import Cls.Person;
  105.  
  106. import java.util.LinkedHashSet;
  107. import java.util.Set;
  108. import java.util.TreeSet;
  109.  
  110. public class tester {
  111.     public static void main(String[] args) {
  112.  
  113.         Person p1 = new Person("Mike", 173, 70);
  114.         Person p2 = new Person("Omri", 170, 50);
  115.         Person p3 = new Person("Aya", 165, 75);
  116.         Person p4 = new Person("David", 169, 60);
  117.         Person p5 = new Person("Max", 180, 80);
  118.  
  119.         Set<Person> persons = new LinkedHashSet<Person>();
  120.  
  121.  
  122.         persons.add(p1);
  123.         persons.add(p2);
  124.         persons.add(p3);
  125.         persons.add(p4);
  126.         persons.add(p5);
  127.  
  128.  
  129.         Set[] sortedPersons = new Set[3];
  130.         sortedPersons[0] = new TreeSet<Person>(new ComparePersonByName());
  131.         sortedPersons[1] = new TreeSet<Person>(new ComparePersonByHeight());
  132.         sortedPersons[2] = new TreeSet<Person>(new ComparePersonByWeight());
  133.  
  134.         for (int i=0; i<sortedPersons.length; i++){
  135.  
  136.             sortedPersons[i].add(p1);
  137.             sortedPersons[i].add(p2);
  138.             sortedPersons[i].add(p3);
  139.             sortedPersons[i].add(p4);
  140.             sortedPersons[i].add(p5);
  141.  
  142.         }
  143.  
  144.         System.out.println("sorted by name: " + sortedPersons[0]);
  145.         System.out.println("sorted by height: " + sortedPersons[1]);
  146.         System.out.println("sorted by weight: " + sortedPersons[2]);
  147.  
  148.  
  149.     }
  150. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top