Tuesday, 13 January 2015

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


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=jRpTJO-C96o&list=UUhwKlOVR041tngjerWxVccw

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 Age.
     */
    @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 Age, ");

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

        if (this.getAge() > employee.getAge())
        {
            return -1;
        }
        else
        {
            return 1;
        }
    }

}
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("John", 20, 40000);
        Employee david = new Employee("David", 10, 80000);
        Employee peter = new Employee("Peter", 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 Age : "+employee.getAge());
            System.out.println("Employee Name : "+employee.getName());
            System.out.println("Employee Salary :"+ employee.getSalary());
            System.out.println("--------------------------------------------");
        }

    }
}
Output
Employee [name=John, age=20, 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 Age, this.getAge()= 20, employee.getAge()=20

Employee [name=David, age=10, 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 Age, this.getAge()= 10, employee.getAge()=20

Employee [name=Peter, 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 Age, this.getAge()= 30, employee.getAge()=20

treeSet : [Employee [name=Peter, age=30, salary=150000], Employee [name=John, age=20, salary=40000], Employee [name=David, age=10, salary=80000]]

--------------------------------------------
Employee Age : 30
Employee Name : Peter
Employee Salary :150000
--------------------------------------------
Employee Age : 20
Employee Name : John
Employee Salary :40000
--------------------------------------------
Employee Age : 10
Employee Name : David
Employee Salary :80000
--------------------------------------------
To Download TreeSetDemoEmployeeAgedescComparable Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/TreeSetDemoEmployeeAgedescComparable.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