Monday 19 January 2015

Java : Collection Framework : TreeSet (Employee objs in asc order based on employeeId - Comparator)


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=YAZ9hncb4Lw&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 + "]";
    }

}
EmployeeIdComparator.java
import java.util.Comparator;

public class EmployeeIdComparator implements Comparator<Employee>
{

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

        System.out
                .println("\nCompare method has been called in EmployeeIdComparator,\nto arrange"
                        + " employee objects in ascending order based on employeeId : ");

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

        if( employee1.getEmployeeId() < employee2.getEmployeeId() )
        {
            return -1;
        }
        else
        {
            return 1;
        }

    }

}
TreeSetExample.java
import java.util.TreeSet;

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

        EmployeeIdComparator employeeIdComparator = new EmployeeIdComparator();

        /*
         * 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>(employeeIdComparator);

        Employee john = new Employee(3, "John");
        Employee david = new Employee(2, "David");
        Employee peter = new Employee(1, "Peter");
        Employee juli = new Employee(4, "Juli");
        

        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(juli + " is going to be add in treeSet");
        treeSet.add(juli);
        
        System.out.println("treeSet : " + treeSet + "\n");
        
        for( Employee employee : treeSet )
        {
            System.out.println("Employee ID : "+employee.getEmployeeId());
            System.out.println("Name : "+employee.getName());
            System.out.println("-----------------------------------");
        }

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

Compare method has been called in EmployeeIdComparator,
to arrange employee objects in ascending order based on employeeId : 
employee1 = Employee [employeeId=3, name=John], employee2 = Employee [employeeId=3, name=John]

Employee [employeeId=2, name=David] is going to be add in treeSet

Compare method has been called in EmployeeIdComparator,
to arrange employee objects in ascending order based on employeeId : 
employee1 = Employee [employeeId=2, name=David], employee2 = Employee [employeeId=3, name=John]

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

Compare method has been called in EmployeeIdComparator,
to arrange employee objects in ascending order based on employeeId : 
employee1 = Employee [employeeId=1, name=Peter], employee2 = Employee [employeeId=3, name=John]


Compare method has been called in EmployeeIdComparator,
to arrange employee objects in ascending order based on employeeId : 
employee1 = Employee [employeeId=1, name=Peter], employee2 = Employee [employeeId=2, name=David]

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

Compare method has been called in EmployeeIdComparator,
to arrange employee objects in ascending order based on employeeId : 
employee1 = Employee [employeeId=4, name=Juli], employee2 = Employee [employeeId=2, name=David]


Compare method has been called in EmployeeIdComparator,
to arrange employee objects in ascending order based on employeeId : 
employee1 = Employee [employeeId=4, name=Juli], employee2 = Employee [employeeId=3, name=John]

treeSet : [Employee [employeeId=1, name=Peter], Employee [employeeId=2, name=David], Employee [employeeId=3, name=John], Employee [employeeId=4, name=Juli]]

Employee ID : 1
Name : Peter
-----------------------------------
Employee ID : 2
Name : David
-----------------------------------
Employee ID : 3
Name : John
-----------------------------------
Employee ID : 4
Name : Juli
-----------------------------------
To Download TreeSetDemoEmployeesAscComparatorAscId Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/TreeSetDemoEmployeesAscComparatorAscId.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