Thursday, 9 July 2015

Java : Collection Framework : Collections (BinarySearch Comparator)

Click here to watch in Youtube :

Click the below Image to Enlarge
Java : Collection Framework : Collections (BinarySearch Comparator) 
Java : Collection Framework : Collections (BinarySearch Comparator)
public class Employee
    private Integer    employeeId;
    private String name;

    public Employee( Integer employeeId, String name )
        this.employeeId = employeeId; = name;

    public Integer getEmployeeId()
        return employeeId;

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

    public String getName()
        return name;

    public void setName( String name )
    { = name;

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

import java.util.Comparator;

public class EmployeeIdComparator implements Comparator<Employee>

     * This method used to order the employee objects in Ascending based on employeeId.
    public int compare(Employee employee1, Employee employee2)

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


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


 public static <T> int binarySearch(List<? extends T> list,
                                         T key,
                                         Comparator<? super T> c)


 list - the list to be searched.
 key - the key to be searched for.
 c - the comparator by which the list is ordered. 
     A null value indicates that the elements' natural ordering should be used.

 the index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). 
 The insertion point is defined as the point at which the key would be inserted into the list: 
 the index of the first element greater than the key, or list.size() 
 if all elements in the list are less than the specified key. 
 Note that this guarantees that the return value will be >= 0 if and only if the key is found.


public class CollectionsExample

    public static void main(String[] args)

        List<Employee> employeeList = new ArrayList<Employee>();

        Employee john = new Employee(300, "John");
        Employee david = new Employee(103, "David");
        Employee peter = new Employee(208, "Peter");
        Employee juli = new Employee(202, "Juli");
        Employee ram = new Employee(102, "Ram");


        System.out.println("employeeList before sort: \n" + employeeList + "\n");

        EmployeeIdComparator employeeIdComparator = new EmployeeIdComparator();

        Collections.sort(employeeList, employeeIdComparator);

                .println("employeeList after sort : \n" + employeeList + "\n");

         * Searches the specified list for the specified object using the binary
         * search algorithm. The list must be sorted into ascending order
         * according to the specified comparator (as by the sort(List,
         * Comparator) method), prior to making this call.
         * If it is not sorted,the results are undefined. If the list contains
         * multiple elements equal to the specified object, there is no
         * guarantee which one will be found.
        Employee searchKey = new Employee(202, null); 
        int index = Collections.binarySearch(employeeList,
                searchKey, employeeIdComparator);

        System.out.println("index : " + index);
        Employee employee = employeeList.get(index);        
        System.out.println("employee name in  '"+index+"' index Position : "
                + employee.getName());

employeeList before sort: 
[Employee [employeeId=300, name=John], Employee [employeeId=103, name=David], Employee [employeeId=208, name=Peter], Employee [employeeId=202, name=Juli], Employee [employeeId=102, name=Ram]]

employeeList after sort : 
[Employee [employeeId=102, name=Ram], Employee [employeeId=103, name=David], Employee [employeeId=202, name=Juli], Employee [employeeId=208, name=Peter], Employee [employeeId=300, name=John]]

index : 2
employee name in  '2' index Position : Juli

To Download CollectionsDemo-BinarySearchComparator Project Click the below link

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge