Comparable
1) Comparable is an interface.
2) Comparable is located in the java.lang package
3) It is in the collections framework of java since java 1.2.
4) Its author is Josh Bloch.
5) Comparable interface contains one methos i.e "int compareTo(Object o)" to implement.
6) It compares this object with the argument object for sorting order. and it returns the
negative integer or zero or positive integer value based on this object is greater than
or equal to the specified argument object.
7) It throws ClassCastException if the specified object prevents it from being compare to this object.
8) Except Number class most of the
Interface
package java.lang;
import java.util.*;
public interface Comparable {
public int compareTo(T o);
}
Example:
/**
* @author raj
*
*/
public class Planet implements Comparable{
private int size;
private String name;
public Planet(String name, int size) {
this.setName(name);
this.setSize(size);
}
public int compareTo(Planet planet) {
return this.getSize() == planet.getSize() ? 0
: this.getSize() < planet.getSize() ? -1
: 1;
}
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(getName());
builder.append(" : ");
builder.append(getSize());
return builder.toString();
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class PlanetEngine {
public static void main(String[] args) {
Planet p0 = new Planet("sun",0);
Planet p1 = new Planet("mercury",1);
Planet p2 = new Planet("venus",2);
Planet p3 = new Planet("earth",3);
Planet p4 = new Planet("mars",4);
Planet p5 = new Planet("saturn",5);
Planet p6 = new Planet("jupiter",6);
Planet p7 = new Planet("uranus",7);
Planet p8 = new Planet("neptune",8);
Planet p9 = new Planet("pluto",9);
List list = new ArrayList();
list.add(p0);
list.add(p1);
list.add(p2);
list.add(p3);
list.add(p4);
list.add(p5);
list.add(p6);
list.add(p7);
list.add(p8);
list.add(p9);
System.out.println("\n\nSORGING BYE SIZE USING COMPARABLE ");
Collections.sort(list);
for(Planet p : list) {
System.out.println(p);
}
}
}
In this example the compare to method returns the negative , zero, or positive integer values based on the size of the planet. means based on the planet size the sorting will be done.
Comparator
1) Comparator is an Interface
2) It is resides into the java.util package
3) It is in the java since java 1.2
4) Its author is Josh Bloch and Neal Gafter.
5) Comparator contains two methods 1) int compare(Object o1, Object o2) 2) boolean equals(Object objt).
6) The compare method compares two objects. It compares the first object with
the second object and based on the comparision it returs negative, zero or positive integer value.
7) It throws ClassCastException if the object
8) One can create multiple comparator based on the required sorting types.
Interface code
package java.util;
public interface Comparator {
int compare(T o1, T o2);
boolean equals(Object obj);
}
Example
import java.util.Comparator;
public class PlanetComparatorByName implements Comparator{
public int compare(Planet p1, Planet p2) {
if(p1.getName()==null && p2.getName()==null) {
return 0;
} else if(p1.getName()==null && p2.getName()!=null) {
return -1;
} else if(p1.getName()!=null && p2.getName()==null) {
return 1;
} else if(p1.getName().length()>p2.getName().length()) {
return 1;
} else if(p1.getName().length() return -1;
} else {
return p1.compareTo(p2);
}
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class PlanetEngine {
public static void main(String[] args) {
Planet p0 = new Planet("sun",0);
Planet p1 = new Planet("mercury",1);
Planet p2 = new Planet("venus",2);
Planet p3 = new Planet("earth",3);
Planet p4 = new Planet("mars",4);
Planet p5 = new Planet("saturn",5);
Planet p6 = new Planet("jupiter",6);
Planet p7 = new Planet("uranus",7);
Planet p8 = new Planet("neptune",8);
Planet p9 = new Planet("pluto",9);
List list = new ArrayList();
list.add(p0);
list.add(p1);
list.add(p2);
list.add(p3);
list.add(p4);
list.add(p5);
list.add(p6);
list.add(p7);
list.add(p8);
list.add(p9);
System.out.println("\n\nSORGING BYE NAME LENGTH USING COMPARATOR ");
Collections.sort(list, new PlanetComparatorByName());
for(Planet p : list) {
System.out.println(p);
}
}
}
1) Comparable is an interface.
2) Comparable is located in the java.lang package
3) It is in the collections framework of java since java 1.2.
4) Its author is Josh Bloch.
5) Comparable interface contains one methos i.e "int compareTo(Object o)" to implement.
6) It compares this object with the argument object for sorting order. and it returns the
negative integer or zero or positive integer value based on this object is greater than
or equal to the specified argument object.
7) It throws ClassCastException if the specified object prevents it from being compare to this object.
8) Except Number class most of the
Interface
package java.lang;
import java.util.*;
public interface Comparable
public int compareTo(T o);
}
Example:
/**
* @author raj
*
*/
public class Planet implements Comparable
private int size;
private String name;
public Planet(String name, int size) {
this.setName(name);
this.setSize(size);
}
public int compareTo(Planet planet) {
return this.getSize() == planet.getSize() ? 0
: this.getSize() < planet.getSize() ? -1
: 1;
}
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(getName());
builder.append(" : ");
builder.append(getSize());
return builder.toString();
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class PlanetEngine {
public static void main(String[] args) {
Planet p0 = new Planet("sun",0);
Planet p1 = new Planet("mercury",1);
Planet p2 = new Planet("venus",2);
Planet p3 = new Planet("earth",3);
Planet p4 = new Planet("mars",4);
Planet p5 = new Planet("saturn",5);
Planet p6 = new Planet("jupiter",6);
Planet p7 = new Planet("uranus",7);
Planet p8 = new Planet("neptune",8);
Planet p9 = new Planet("pluto",9);
List
list.add(p0);
list.add(p1);
list.add(p2);
list.add(p3);
list.add(p4);
list.add(p5);
list.add(p6);
list.add(p7);
list.add(p8);
list.add(p9);
System.out.println("\n\nSORGING BYE SIZE USING COMPARABLE ");
Collections.sort(list);
for(Planet p : list) {
System.out.println(p);
}
}
}
In this example the compare to method returns the negative , zero, or positive integer values based on the size of the planet. means based on the planet size the sorting will be done.
Comparator
1) Comparator is an Interface
2) It is resides into the java.util package
3) It is in the java since java 1.2
4) Its author is Josh Bloch and Neal Gafter.
5) Comparator contains two methods 1) int compare(Object o1, Object o2) 2) boolean equals(Object objt).
6) The compare method compares two objects. It compares the first object with
the second object and based on the comparision it returs negative, zero or positive integer value.
7) It throws ClassCastException if the object
8) One can create multiple comparator based on the required sorting types.
Interface code
package java.util;
public interface Comparator
int compare(T o1, T o2);
boolean equals(Object obj);
}
Example
import java.util.Comparator;
public class PlanetComparatorByName implements Comparator
public int compare(Planet p1, Planet p2) {
if(p1.getName()==null && p2.getName()==null) {
return 0;
} else if(p1.getName()==null && p2.getName()!=null) {
return -1;
} else if(p1.getName()!=null && p2.getName()==null) {
return 1;
} else if(p1.getName().length()>p2.getName().length()) {
return 1;
} else if(p1.getName().length()
} else {
return p1.compareTo(p2);
}
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class PlanetEngine {
public static void main(String[] args) {
Planet p0 = new Planet("sun",0);
Planet p1 = new Planet("mercury",1);
Planet p2 = new Planet("venus",2);
Planet p3 = new Planet("earth",3);
Planet p4 = new Planet("mars",4);
Planet p5 = new Planet("saturn",5);
Planet p6 = new Planet("jupiter",6);
Planet p7 = new Planet("uranus",7);
Planet p8 = new Planet("neptune",8);
Planet p9 = new Planet("pluto",9);
List
list.add(p0);
list.add(p1);
list.add(p2);
list.add(p3);
list.add(p4);
list.add(p5);
list.add(p6);
list.add(p7);
list.add(p8);
list.add(p9);
System.out.println("\n\nSORGING BYE NAME LENGTH USING COMPARATOR ");
Collections.sort(list, new PlanetComparatorByName());
for(Planet p : list) {
System.out.println(p);
}
}
}
No comments:
Post a Comment