Sunday, January 12, 2014

Sorting and Searching the Employee Object by using Collection

In this post, we can learn how to sort the Employee object by using age and search the Employee object by using age.

Employee.java
----------------------------------------

package com.ranga;

import java.io.Serializable;

/**
 * Created by ranga on 1/12/14.
 */

public class Employee implements Serializable {
  private long serialVersionUID = 31462223600l;

  private long id;
  private String firstName;
  private String lastName;
  private int age;
  private String address;

  public Employee() {
    super();
  }

  public Employee(long id, String firstName, String lastName, int age, String address) {
    this.id = id;
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
    this.address = address;
  }
  public Employee(int age) {
    this.age = age;
  }

  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }

  public String getAddress() {
    return address;
  }

  public void setAddress(String address) {
    this.address = address;
  }

  @Override
  public String toString() {
    return "Employee{" +
        "id=" + id +
        ", firstName='" + firstName + '\'' +
        ", lastName='" + lastName + '\'' +
        ", age=" + age +
        ", address='" + address + '\'' +
        '}';
  }

}

Application.java

package com.ranga;

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

/**
 * Created by ranga on 1/12/14.
 */
public class Application {
  public static void main(String[] args) {
    Employee employee = new Employee();
    employee.setId(1);
    employee.setFirstName("Ranga");
    employee.setLastName("Reddy");
    employee.setAge(25);
    employee.setAddress("MPL, Tirupathi, AP");

    Employee employee2 = new Employee();
    employee2.setId(2);
    employee2.setFirstName("Ranga");
    employee2.setLastName("Reddy");
    employee2.setAge(27);
    employee2.setAddress("BTM, Bangalore, KA");

    Employee employee3 = new Employee();
    employee3.setId(3);
    employee3.setFirstName("Ranga");
    employee3.setLastName("Reddy");
    employee3.setAge(22);
    employee3.setAddress("Bommanahalli, Bangalore, KA");

    List<Employee> employees = new ArrayList<Employee>();
    employees.add(employee);
    employees.add(employee2);
    employees.add(employee3);

    System.out.println("Before Sorting...");
    for(Employee emp : employees) {
      System.out.println(emp);
    }
    System.out.println("*******************************************************");
    Collections.sort(employees, new Comparator<Employee>() {
      @Override
      public int compare(Employee o1, Employee o2) {
        return o1.getAge() - o2.getAge();
      }
    });

    System.out.println("After Sorting...");
    for(Employee emp : employees) {
      System.out.println(emp);
    }
    System.out.println("*******************************************************");
    int index = Collections.binarySearch(employees, new Employee(25), new Comparator<Employee>() {
      @Override
      public int compare(Employee o1, Employee o2) {
        return o1.getAge() - o2.getAge();
      }
    });

    if(index != -1) {
      System.out.println("Employee found at location : "+index);
      System.out.println(employees.get(index));
    } else {
      System.out.println("Employee not found..");
    }
    System.out.println("*******************************************************");

  }
}

Output:

Before Sorting...
Employee{id=1, firstName='Ranga', lastName='Reddy', age=25, address='MPL, Tirupathi, AP'}
Employee{id=2, firstName='Ranga', lastName='Reddy', age=27, address='BTM, Bangalore, KA'}
Employee{id=3, firstName='Ranga', lastName='Reddy', age=22, address='Bommanahalli, Bangalore, KA'}
*******************************************************
After Sorting...
Employee{id=3, firstName='Ranga', lastName='Reddy', age=22, address='Bommanahalli, Bangalore, KA'}
Employee{id=1, firstName='Ranga', lastName='Reddy', age=25, address='MPL, Tirupathi, AP'}
Employee{id=2, firstName='Ranga', lastName='Reddy', age=27, address='BTM, Bangalore, KA'}
*******************************************************
Employee found at location : 1
Employee{id=1, firstName='Ranga', lastName='Reddy', age=25, address='MPL, Tirupathi, AP'}
*******************************************************

0 comments: