Tuesday, 13 January 2015

Java : Collection Framework : TreeSet (Arrange Employee objects in descending order based on name)

Employee.java
public class Employee implements Comparable<Object>
{

    private String name;
    private int age;
    private int salary;

    public Employee(String name, int age, int salary)
    {
        super();
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public int getAge()
    {
        return age;
    }

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

    public int getSalary()
    {
        return salary;
    }

    public void setSalary(int salary)
    {
        this.salary = salary;
    }

    @Override
    public String toString()
    {
        return "Employee [name=" + name + ", age=" + age + ", salary=" + salary
                + "]";
    }

    /*
     * This method has logic to arrange the employee objects in descending order
     * based on the Name.
     */
    @Override
    public int compareTo(Object object)
    {

        Employee employee = (Employee) object;

        System.out
                .print("CompareTo method has been called to arrange the employee objects in                        descending order based on the Name, ");

        System.out.println("this.getName()= " + this.getName()
                + ", employee.getName()=" + employee.getName() + "\n");

        return employee.getName().compareTo(this.getName());
    }

}
TreeSetExample.java
import java.util.TreeSet;

/*
 * Storing user-defined class objects.
 */
public class TreeSetExample
{
    public static void main( String[] args )
    {
        TreeSet<Employee> treeSet = new TreeSet<Employee>();

        Employee john = new Employee("Balu", 10, 40000);
        Employee david = new Employee("Ajay", 20, 80000);
        Employee peter = new Employee("Carol", 30, 150000);

        System.out.println(john + " is going to be add in treeSet");
        treeSet.add(john);
        
        System.out.println(david + " is going to be add in treeSet");
        treeSet.add(david);
        
        System.out.println(peter + " is going to be add in treeSet");
        treeSet.add(peter);
        

        System.out.println("treeSet : " + treeSet + "\n");
        System.out.println("--------------------------------------------");
        /*
         * Using for each loop getting each employee object from the treeSet
         */
        for( Employee employee : treeSet )
        {
            System.out.println("Employee Name : "+employee.getName());
            System.out.println("Employee Age : "+employee.getAge());
            System.out.println("Employee Salary :"+ employee.getSalary());
            System.out.println("--------------------------------------------");
        }

    }
}
Output
Employee [name=Balu, age=10, salary=40000] is going to be add in treeSet
CompareTo method has been called to arrange the employee objects in descending order based on the Name, this.getName()= Balu, employee.getName()=Balu

Employee [name=Ajay, age=20, salary=80000] is going to be add in treeSet
CompareTo method has been called to arrange the employee objects in descending order based on the Name, this.getName()= Ajay, employee.getName()=Balu

Employee [name=Carol, age=30, salary=150000] is going to be add in treeSet
CompareTo method has been called to arrange the employee objects in descending order based on the Name, this.getName()= Carol, employee.getName()=Balu

treeSet : [Employee [name=Carol, age=30, salary=150000], Employee [name=Balu, age=10, salary=40000], Employee [name=Ajay, age=20, salary=80000]]

--------------------------------------------
Employee Name : Carol
Employee Age : 30
Employee Salary :150000
--------------------------------------------
Employee Name : Balu
Employee Age : 10
Employee Salary :40000
--------------------------------------------
Employee Name : Ajay
Employee Age : 20
Employee Salary :80000
--------------------------------------------
To Download TreeSetDemoEmployeeNamedescComparable Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/TreeSetDemoEmployeeNamedescComparable.zip?attredirects=0&d=1

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge