The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java : Collection Framework : Collections (BinarySearch Comparator)

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ram N

Posts: 2777
Nickname: ramram
Registered: Jul, 2014

Ram N is Java Programmer
Java : Collection Framework : Collections (BinarySearch Comparator) Posted: Jul 9, 2015 8:20 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Ram N.
Original Post: Java : Collection Framework : Collections (BinarySearch Comparator)
Feed Title: JAVA EE
Feed URL: http://ramj2ee.blogspot.com/feeds/posts/default?alt=rss
Feed Description: This blog has viedo tutorials and Sample codes related to below Technologies. 1.J2EE 2.Java 3.Spring 4.Hibernate 5.Database 6.Oracle 7.Mysql 8.Design Patterns
Latest Java Buzz Posts
Latest Java Buzz Posts by Ram N
Latest Posts From JAVA EE

Advertisement

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

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

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

public Integer 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 order the employee objects in Ascending based on employeeId.
*/

@Override
public int compare(Employee employee1, Employee employee2)
{

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

}

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

/*
Method:

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

Parameters:

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.

Returns:

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");

employeeList.add(john);
employeeList.add(david);
employeeList.add(peter);
employeeList.add(juli);
employeeList.add(ram);

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

EmployeeIdComparator employeeIdComparator = new EmployeeIdComparator();

Collections.sort(employeeList, employeeIdComparator);

System.out
.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());

}
}
Output
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
https://sites.google.com/site/javaee4321/java-collections/CollectionsDemo-BinarySearchComparator.zip?attredirects=0&d=1

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
  • Read: Java : Collection Framework : Collections (BinarySearch Comparator)

    Topic: Casting In Java 8 (And Beyond?) Previous Topic   Next Topic Topic: Measuring Allocations Programmatically

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use