Thursday 6 July 2017

comparable vs comparator part-2

comparable vs comparator


Comparator In java :- comparator is an interface in java which has only two method to compare the objects in a user defined class known as compare() method and other one is equals method which is used to somapare order of one object to another.

Compare method has following syntax :- public int compare(Object obj1,Object2 obj2) it compares object1 to object2.

Public void sort(List list,Comparator c):- is used to sort the element of List by the given Comparator.

Now we are going to see the brief example of Comparator interface.

Link For Comparable : https://programmersthing.blogspot.in/2017/07/comparable-vs-comparator.html



Comparator Interface


1.AgeComparator.java Class :

package comparator;

import java.util.Comparator;

public class AgeComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
Student s1 = (Student) o1;
Student s2 = (Student) o2;

if (s1.age == s2.age)
return 0;
else if (s1.age > s2.age)
return 1;
else
return -1;
}
}


2.NameComparator.java Class :

package comparator;

import java.util.Comparator;

public class NameComparator implements Comparator<Student> {

@Override
public int compare(Student o1, Student o2) {
Student s1 = (Student) o1;
Student s2 = (Student) o2;

return s1.name.compareTo(s2.name);
}
}


3.Student.java Class :

package comparator;

public class Student {
int rollno;
int standard;
String name;
String address;
int age;

Student(int rollno, String name, int age) {
this.rollno = rollno;
this.name = name;
this.age = age;
}

public int getRollno() {
return rollno;
}

public void setRollno(int rollno) {
this.rollno = rollno;
}

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;
}

}


4.ComparatorTest.java Class :

package comparator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

public class ComparatorTest {

public static void main(String[] args) {
// TODO Auto-generated method stub

ArrayList al = new ArrayList();
al.add(new Student(101, "Vijay", 23));
al.add(new Student(102, "Ajay", 27));
al.add(new Student(103, "Jai", 21));
al.add(new Student(104, "sanket", 21));
al.add(new Student(105, "santosh", 21));
al.add(new Student(106, "manish", 21));
al.add(new Student(107, "kanan", 21));
al.add(new Student(108, "pravin", 21));
al.add(new Student(109, "rishabh", 21));
al.add(new Student(110, "santosh", 21));
al.add(new Student(111, "dev", 21));
al.add(new Student(112, "rahul", 21));
al.add(new Student(113, "Jai", 21));
al.add(new Student(114, "deepak", 21));

System.err.println("Sorting by Name...");

Collections.sort(al, new NameComparator());
Iterator itr = al.iterator();
while (itr.hasNext()) {
Student st = (Student) itr.next();
System.out.println(st.rollno + " " + st.name + " " + st.age);
}

System.err.println("sorting by age...");

Collections.sort(al, new AgeComparator());
Iterator itr2 = al.iterator();
while (itr2.hasNext()) {
Student st = (Student) itr2.next();
System.out.println(st.rollno + " " + st.name + " " + st.age);
}

}

}



Output :-
Sorting by Name...

102 Ajay 27
103 Jai 21
113 Jai 21
101 Vijay 23
114 deepak 21
111 dev 21
107 kanan 21
106 manish 21
108 pravin 21
112 rahul 21
109 rishabh 21
104 sanket 21
105 santosh 21
110 santosh 21

sorting by age...

103 Jai 21
113 Jai 21
114 deepak 21
111 dev 21
107 kanan 21
106 manish 21
108 pravin 21
112 rahul 21
109 rishabh 21
104 sanket 21
105 santosh 21
110 santosh 21
101 Vijay 23
102 Ajay 27




These is one more small class example

public class InstrumentSpec {

  private Map properties;

  public InstrumentSpec(Map properties) {
    if (properties == null) {
      this.properties = new HashMap();
    } else {
      this.properties = new HashMap(properties);
    }
  }

  public Object getProperty(String propertyName) {
    return properties.get(propertyName);
  }

  public Map getProperties() {
    return properties;
  }

  public boolean matches(InstrumentSpec otherSpec) {
    for (Iterator i = otherSpec.getProperties().keySet().iterator(); 
         i.hasNext(); ) {
      String propertyName = (String)i.next();
      if (!properties.get(propertyName).equals(
           otherSpec.getProperty(propertyName))) {
        return false;
      }
    }
    return true;
  }
}


for Comparable example follow above link of part one



Keep Learning !!!!!

No comments:

Post a Comment

Spring-App

Application In Spring Spring Framework is an open source Java Platform that provides comprehensive infrastructure support for developi...