6 Kasım 2019 Çarşamba

Java Comparator - Comparable Interface

Ornegin elimizde bir liste olsun ve biz bu listenin elemanlarini siralamak isteyelim. Bunu nasil yapacagimiz asagidaki ornekte gosterdim.

public class CompareElements {
 public static void main(String[] args) {
  List<String> nameList = new ArrayList<String>();
  nameList.add("helen");
  nameList.add("yasmin");
  nameList.add("andrew");
  nameList.add("craine");
  Collections.sort(nameList);
  System.out.println(nameList);
 }
}
Output:
[andrew, craine, helen, yasmin]

Ornekteki elemanlari gordugumuz gibi alfabetik siraya gore siraladik. Burada isimiz kolaydi cunku listede sadece tek tipte elemanimiz vardi. O da isim.

Simdi soyle bir case dusunelim. String tipinde isim yerine Person adinda bir classimiz olsun ve bu class in hem name hem de age attribute lari olsun. Ve ben istedigim zaman istedigim parametreye gore sirayalabileyim.

Oncelikle isme gore siramak istiyorum.


public class ComparableEx {

 public static class Person implements Comparable{
  private String name;
  private int age;
  public Person(String name, int age) {
   super();
   this.name = name;
   this.age = age;
  }
  public String getName() {
   return name;
  }
  public void setName(String name) {
   this.name = name;
  }
  public int getAge() {
   return age;
  }
  public void setAge(int age) {
   this.age = age;
  }  
  @Override
  public int compareTo(Person o) {
   return this.name.compareTo(o.name);
  }
 } 
 public static void main(String[] args) {  
  List personList = new ArrayList<>();
  personList.add(new Person("craine", 21));
  personList.add(new Person("yasmin", 12));
  personList.add( new Person("andrew", 15));
  personList.add(new Person("helen", 10));
  
  Collections.sort(personList);
  for(Person person:personList) {
   System.out.println(person.getName());
  }
 }}
Output:
andrew
craine
helen
yasmin


Gordugumuz gibi modelimiz Comparable Interface ini implement ediyor ve compareTo methodu icinde name'e gore siraliyoruz. Age icin yapmak istersek compareTo icinde age kullaniriz.

Simdi ise boyle tek tek degil de, yani her seferinde compareTo icini degistirerek degil de, istedigimiz an istedigimiz sekilde siralamaya bakalim. Bu durumda ise Comparator interface ini kullanacagiz.
public class ComparatorEx {

 public static class Person{
  private String name;
  private int age;
  public Person(String name, int age) {
   super();
   this.name = name;
   this.age = age;
  }
  public String getName() {
   return name;
  }
  public void setName(String name) {
   this.name = name;
  }
  public int getAge() {
   return age;
  }
  public void setAge(int age) {
   this.age = age;
  }
  
 }
 
 public static class NameComparator implements Comparator{

  @Override
  public int compare(Person o1, Person o2) {
   return o1.getName().compareTo(o2.getName());
  }  
 }
 
 public static class AgeComparator implements Comparator{

  @Override
  public int compare(Person o1, Person o2) {
   if(o1.getAge()>o2.getAge()) {
    return 1;
   } else if(o2.getAge() >o1.getAge()){
    return -1;
   }else {
    return 0;
   }   
  }  
 }
 public static void main(String[] args) {
  List personList = new ArrayList<>();
  personList.add(new Person("craine", 21));
  personList.add(new Person("yasmin", 12));
  personList.add( new Person("andrew", 15));
  personList.add(new Person("helen", 10));
  
  Collections.sort(personList, new NameComparator());
  for(Person p: personList) {
   System.out.println(p.getName()+ "->"+p.getAge());
  }
                System.out.println("--------");
  Collections.sort(personList, new AgeComparator());
  for(Person p: personList) {
   System.out.println(p.getName()+ "->"+p.getAge());
  }
 }
}
Output:
andrew->15
craine->21
helen->10
yasmin->12
--------
helen->10
yasmin->12
andrew->15
craine->21

Gordugumuz gibi her bir attribute icin bir class olusturup Comparator interface ini implement ettik ve ona gore siraladik.


Hiç yorum yok:

Yorum Gönder