Monday, June 28, 2010

The Comparable Interface

1-The Comparable Interface Is Used by the Collections.sort()method and java.utils.Arrays.sort() method to sort Lists and arrays of objects.
2-int x=thisObject.compareTo(anotherObject)
3-The sort() method used compareTo() method to determine which the List or Object Array should be sorted.
4-All element in the list must be implement the Comparable interface .
5-All element are must mutually comparable
6-It means i1.compareTo(i2) must be throw a ClassCastException for any element of i1 and i2.
7-compareTo() method return negative integer,zero or positive integer as this object is less than,equal to, or greater than the specifies object .
8-ClassCastException if the specified object's type prevents it from being compared to this object.
9-it is paramount that when you going to override equals() you must tske an argument of type Object but that when you override compareTo() you should take an argument of the type you are shorting .

Example:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class ArrayListSorting implements Comparable < ArrayListSorting > {

String firstName;
String middeleName;
String lastName;
String collegeName;
Long mobileNumber;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMiddeleName() {
return middeleName;
}
public void setMiddeleName(String middeleName) {
this.middeleName = middeleName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getCollegeName() {
return collegeName;
}


public void setCollegeName(String collegeName) {
this.collegeName = collegeName;
}


public Long getMobileNumber() {
return mobileNumber;
}


public void setMobileNumber(Long mobileNumber) {
this.mobileNumber = mobileNumber;
}


public ArrayListSorting(String firstName1,String middelName1,String lastName1,String collegeName1,Long mobileNumber1) {
firstName=firstName1;
middeleName=middelName1;
lastName=lastName1;
collegeName=collegeName1;
mobileNumber=mobileNumber1;
}

public String toString(){
return firstName +" "+middeleName+" "+lastName+" "+collegeName+" "+mobileNumber;
}

public static void main(String[] args) {
List< ArrayListSorting > example=new ArrayList< ArrayListSorting >();
ArrayListSorting prateekc=new ArrayListSorting("prateek", "kumar", "shaw", "ABES", 9891445027L);
ArrayListSorting anoopc=new ArrayListSorting("anoop", "chandra", "verma", "ABES", 9891445026L);
ArrayListSorting loljeetc=new ArrayListSorting("laljeet", "", "yadav", "ABES", 9891445029L);
ArrayListSorting jamalc=new ArrayListSorting("jamal", "ur", "rahman", "ABES", 9891445030L);
ArrayListSorting hariomc=new ArrayListSorting("hari", "om", "patel", "ABES", 9891445031L);
example.add(prateekc);
example.add(anoopc);
example.add(loljeetc);
example.add(jamalc);
example.add(hariomc);
System.out.println("unsorted name "+example);
Collections.sort(example);
System.out.println("sorted name "+example);
ArrayListSorting1 example1=new ArrayListSorting1();
Collections.sort(example,example1 );
System.out.println("sorte name is "+example1);
}
public int compareTo(ArrayListSorting listsorting){
return firstName.compareTo(listsorting.getFirstName());
}
}