Monday 19 January 2015

Java : Collection Framework : TreeSet (Employee objs in Desc order based on name - Comparator)


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

Employee.java
public class Employee
{
    private int    employeeId;
    private String name;

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

    public int getEmployeeId()
    {
        return employeeId;
    }

    public void setEmployeeId( int employeeId )
    {
        this.employeeId = employeeId;
    }

    public String getName()
    {
        return name;
    }

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

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

}
EmployeeNameComparator.java
import java.util.Comparator;

public class EmployeeNameComparator implements Comparator<Employee>
{

    /*
     * This method is used to arrange the employee objects in Descending order based
     * on employee name.
     */
    @Override
    public int compare( Employee employee1, Employee employee2 )
    {

        System.out
                .println("\nCompare method has been called in EmployeeNameComparator,\nto arrange the"
                        + " employee objects in Descending order based on employee name : ");

        System.out.println("employee1 = " + employee1 + ", employee2 = "
                + employee2 + "\n");

        return employee2.getName().compareTo(employee1.getName());

    }

}
TreeSetExample.java
import java.util.TreeSet;

/*
 * Example of  TreeSet(Comparator<? super E> comparator) Constructor.
 */
public class TreeSetExample
{
    public static void main(String[] args)
    {

        EmployeeNameComparator employeeNameComparator = new EmployeeNameComparator();

        /*
         * Constructs a new, empty tree set, sorted according to the specified
         * comparator.
         * 
         * All elements inserted into the set must be mutually comparable by the
         * specified comparator: comparator.compare(e1, e2) must not throw a
         * ClassCastException for any elements e1 and e2 in the set.
         * 
         * If the user attempts to add an element to the set that violates this
         * constraint, the add call will throw a ClassCastException.
         */

        TreeSet<Employee> treeSet = new TreeSet<Employee>(
                employeeNameComparator);

        Employee carol = new Employee(3, "Carol");
        Employee balu = new Employee(5, "Balu");
        Employee ajay = new Employee(4, "Ajay");
        Employee dave = new Employee(1, "Dave");

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

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

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

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

        System.out.println("treeSet : " + treeSet + "\n");

        for (Employee employee : treeSet)
        {
            System.out.println("Name : " + employee.getName());
            System.out.println("Employee ID : " + employee.getEmployeeId());
            System.out.println("-----------------------------------");
        }

    }
}
Output
Employee [employeeId=3, name=Carol] is going to be add in treeSet

Compare method has been called in EmployeeNameComparator,
to arrange the employee objects in Descending order based on employee name : 
employee1 = Employee [employeeId=3, name=Carol], employee2 = Employee [employeeId=3, name=Carol]

Employee [employeeId=5, name=Balu] is going to be add in treeSet

Compare method has been called in EmployeeNameComparator,
to arrange the employee objects in Descending order based on employee name : 
employee1 = Employee [employeeId=5, name=Balu], employee2 = Employee [employeeId=3, name=Carol]

Employee [employeeId=4, name=Ajay] is going to be add in treeSet

Compare method has been called in EmployeeNameComparator,
to arrange the employee objects in Descending order based on employee name : 
employee1 = Employee [employeeId=4, name=Ajay], employee2 = Employee [employeeId=3, name=Carol]


Compare method has been called in EmployeeNameComparator,
to arrange the employee objects in Descending order based on employee name : 
employee1 = Employee [employeeId=4, name=Ajay], employee2 = Employee [employeeId=5, name=Balu]

Employee [employeeId=1, name=Dave] is going to be add in treeSet

Compare method has been called in EmployeeNameComparator,
to arrange the employee objects in Descending order based on employee name : 
employee1 = Employee [employeeId=1, name=Dave], employee2 = Employee [employeeId=5, name=Balu]


Compare method has been called in EmployeeNameComparator,
to arrange the employee objects in Descending order based on employee name : 
employee1 = Employee [employeeId=1, name=Dave], employee2 = Employee [employeeId=3, name=Carol]

treeSet : [Employee [employeeId=1, name=Dave], Employee [employeeId=3, name=Carol], Employee [employeeId=5, name=Balu], Employee [employeeId=4, name=Ajay]]

Name : Dave
Employee ID : 1
-----------------------------------
Name : Carol
Employee ID : 3
-----------------------------------
Name : Balu
Employee ID : 5
-----------------------------------
Name : Ajay
Employee ID : 4
-----------------------------------
To Download TreeSetDemoEmployeesAscComparatorDescName Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/TreeSetDemoEmployeesAscComparatorDescName.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