Monday, April 6, 2009

Sorting Objects in Java

Today we are talking about sorting.

What is sorting ?

Putting collection of data in some order is called sorting.


Luckily Java provides a Collections.sort method to sort a set of arraylist objects .Lets take a small example for sorting a collection of string objects.

package app;

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

public class SortingList
{

/**
* @param args
*/
public static void main(String[] args)
{
System.out.println("Sorting String Objects");
ArrayList Emp_list = new ArrayList();
Emp_list.add("Uday");
Emp_list.add("Mani");
Emp_list.add("Raja");
Emp_list.add("Yuva");
Emp_list.add("Abi");
Emp_list.add("Brindha");
System.out.println(Emp_list);
Collections.sort(Emp_list);
System.out.println(Emp_list);
}

}
Output :

Sorting String Objects
[Uday, Mani, Raja, Yuva, Abi, Brindha]
[Abi, Brindha, Mani, Raja, Uday, Yuva]

If you pass some objects other than string it will throw classcastException.

Why ?

String class implements comparable interface and overridden the compare method
thats why we can sort an array of String objects.if you want sort an object other than string
you should implement either comparator or comparable intefaces .

See the below program for sorting a Customer Objects

package app;
import java.util.*;

class Customer
{
int Cust_ID;
String Cust_Name;
String Ac_type;
int Balance_amt;

Customer()
{
}

Customer(int Id,
String Name,
String Type,
int amt)
{
this.Cust_ID = Id;
this.Cust_Name = Name;
this.Ac_type = Type;
this.Balance_amt = amt;
}

public Integer getId()
{
return Cust_ID;
}

public String getName()
{
return Cust_Name;
}

public Integer getBalance()
{
return Balance_amt;
}

@Override
public String toString()
{
// TODO Auto-generated method stub
return "EMPID :" + Cust_ID + "EMPNAME :" + Cust_Name + "A/C Type :"
+ Ac_type + "AMT :" + Balance_amt + "\n";
}
}
class Bank
{

final static Comparator EMP_ID =new Comparator(){
public int compare(Customer cust1, Customer cust2) {
return cust1.getId().compareTo(cust2.getId());
}
};
final static Comparator EMP_NAME =new Comparator(){
public int compare(Customer cust1, Customer cust2) {
return cust1.getName().compareTo(cust2.getName());
}
};
final static Comparator EMP_AMOUNT =new Comparator(){
public int compare(Customer cust1, Customer cust2) {
return cust1.getBalance().compareTo(cust2.getBalance());
}
};
public static void main(String st[])
{
ArrayList Emp_List = new ArrayList();
Emp_List.add(new Customer(118, "Raja", "Saving", 5000));
Emp_List.add(new Customer(136, "mani", "Saving", 8000));
Emp_List.add(new Customer(173, "Selva", "Saving", 15000));
Emp_List.add(new Customer(108, "kanna", "Saving", 56000));
Emp_List.add(new Customer(18, "Abi", "Saving", 12000));
System.out.println("Original List of Customer Objects");
System.out.println(Emp_List);
System.out.println("Original Customer Objects by Emp ID");
Collections.sort(Emp_List, EMP_ID);
System.out.println(Emp_List);
System.out.println("Original Customer Objects by Amount");
Collections.sort(Emp_List, EMP_AMOUNT);
System.out.println(Emp_List);


}
}

Output :

Original List of Customer Objects
[EMPID :118EMPNAME :RajaA/C Type :SavingAMT :5000
, EMPID :136EMPNAME :maniA/C Type :SavingAMT :8000
, EMPID :173EMPNAME :SelvaA/C Type :SavingAMT :15000
, EMPID :108EMPNAME :kannaA/C Type :SavingAMT :56000
, EMPID :18EMPNAME :AbiA/C Type :SavingAMT :12000
]
Original Customer Objects by Emp ID
[EMPID :18EMPNAME :AbiA/C Type :SavingAMT :12000
, EMPID :108EMPNAME :kannaA/C Type :SavingAMT :56000
, EMPID :118EMPNAME :RajaA/C Type :SavingAMT :5000
, EMPID :136EMPNAME :maniA/C Type :SavingAMT :8000
, EMPID :173EMPNAME :SelvaA/C Type :SavingAMT :15000
]
Original Customer Objects by Amount
[EMPID :118EMPNAME :RajaA/C Type :SavingAMT :5000
, EMPID :136EMPNAME :maniA/C Type :SavingAMT :8000
, EMPID :18EMPNAME :AbiA/C Type :SavingAMT :12000
, EMPID :173EMPNAME :SelvaA/C Type :SavingAMT :15000
, EMPID :108EMPNAME :kannaA/C Type :SavingAMT :56000
]

Friday, April 3, 2009

Sample Java Program - explaining each words

Sample Java Program - explaining each words
Lets take sample program in java and will research each and everthing
Class Test{

public static void main(String args[])
{

System.out.println("Welcome to Java program");

}

}
Now thing about following question :
1 Why the main method inside a Class rather than outside of class?
2 Why they decared main method as public ?
3 Will it compile if I declare private or other access specifier ?
4 why main class declared as static method? what is the purpose for static here ?
5 What happened if I decalre 'int' instead of void and return 0? Will it compile ?
6 Can I change the argument args[] to s[]?
7 Shall I call a main method from other class ?
8 what is System ?what is out ? what is println?

Understand the question and answer it .