Monday, 11 February 2013

How to sort an ArrayList

ArrayList can be sort using any of the to ways:
  1. By implementing the Comparable Interface
  2. By implementing the Comparator Interface. 
Comparable interface is implemented when we specify the sorting inside the class itself. This type of sorting is required in Wrapper classes. I have shown sorting implementation below for an Employee Object.

First we need to have an Object in place; for which we need to implement Sorting. This Object implements Comparable interface. We have to override compareTo(Object obj) here, just to defining the sorting rule.  Here I am implementing sorting basis of employeeName.
package classes;
public class Employee implements Comparable{
public Employee(Long empId, String empName){
this.empId =empId;
this.empName=empName;
}
private String empName;
private Long empId;
private String empAddress;
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Long getEmpId() {
return empId;
}
public void setEmpId(Long empId) {
this.empId = empId;
}
public String getEmpAddress() {
return empAddress;
}
public void setEmpAddress(String empAddress) {
this.empAddress = empAddress;
}
public boolean equals(Object o){
if (this == o ){
return true;
}
if (o instanceof Employee){
if(((Employee) o).getEmpId().equals(this.empId)){
return true;
}else{
return false;
}
}else{
return false;
}
}
public int hashCode(){
return empId.intValue();
}
public String toString(){
return empId+ "  " +empName;
}

       @Override
public int compareTo(Object obj) {
Employee emp = (Employee)obj;
return empName.compareTo(emp.getEmpName());
}
}


Now we can sort the List which contains employee Objects. The important part of the below class is Collections.sort(empList). Here we are using sort() method of Collections class. 

package collections;

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

import classes.Employee;

public class ArrayListSorting {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
List<Employee> empList= empList();
                Collections.sort(empList);
for(Employee emp:empList){
System.out.println("List Values are ::: "+emp.getEmpId());
System.out.println("List Values are ::: "+emp);
}
}
private static ArrayList<Employee> empList(){
ArrayList<Employee> listData = new ArrayList<Employee>(); 
listData.add(new Employee(new Long(4),"Forth"));
listData.add(new Employee(new Long(5),"Fift"));
listData.add(new Employee(new Long(6),"Six"));
listData.add(new Employee(new Long(1),"First"));
listData.add(new Employee(new Long(2),"Second"));
listData.add(new Employee(new Long(3),"Third"));
return listData;
}
}

OutPut is :

List Values are ::: 5  Fift
List Values are ::: 1  First
List Values are ::: 4  Forth
List Values are ::: 2  Second
List Values are ::: 6  Six
List Values are ::: 3  Third


Comparator interface is required to be implemented where we can not touch the class and class has not implemented Comparable interface. Below are the steps we have to follow to achieve the sorting.

First we have to have create a class which does not implement Comparable Interface

package classes;

public class Employee {
public Employee(Long empId, String empName){
this.empId =empId;
this.empName=empName;
}

private String empName;
private Long empId;
private String empAddress;

public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Long getEmpId() {
return empId;
}
public void setEmpId(Long empId) {
this.empId = empId;
}
public String getEmpAddress() {
return empAddress;
}
public void setEmpAddress(String empAddress) {
this.empAddress = empAddress;
}

public boolean equals(Object o){

if (this == o ){
return true;
}
if (o instanceof Employee){
if(((Employee) o).getEmpId().equals(this.empId)){
return true;
}else{
return false;
}
}else{
return false;
}
}

public int hashCode(){
return empId.intValue();
}

public String toString(){

return empId+ "  " +empName;
}
}

Now the backbone of this way of sorting is required to be implement that's a class which implements Comparator interface.


package collections;
import java.util.Comparator;
import classes.Employee;

public class EmployeeComparator implements Comparator<Employee>{
@Override
public int compare(Employee emp1, Employee emp2) {
return (emp1.getEmpName()).compareTo(emp2.getEmpName());
        }
}

Here we are sorting List which contains Employee Objects.


package collections;

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

import classes.Employee;

public class ArrayListSorting {

public static void main(String[] args) {

EmployeeComparator eComp = new EmployeeComparator();

List<Employee> empList= empList();
Collections.sort(empList, eComp);

for(Employee emp:empList){
System.out.println("List Values are ::: "+emp);
}
}
private static ArrayList<Employee> empList(){
ArrayList<Employee> listData = new ArrayList<Employee>();

listData.add(new Employee(new Long(4),"Forth"));
listData.add(new Employee(new Long(5),"Fift"));
listData.add(new Employee(new Long(6),"Six"));
listData.add(new Employee(new Long(1),"First"));
listData.add(new Employee(new Long(2),"Second"));
listData.add(new Employee(new Long(3),"Third"));

return listData;
}
}


OutPut is :

List Values are ::: 5  Fift
List Values are ::: 1  First
List Values are ::: 4  Forth
List Values are ::: 2  Second
List Values are ::: 6  Six
List Values are ::: 3  Third



No comments:

Post a Comment