JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: How to search user defined object from a List by using binary search using comparator?


Description:

The Collections.binarySearch() method searches the specified list for the specified user defined 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.


Code:
package com.java2novice.collections;

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

public class MyListBinarySearch {

	public static void main(String a[]){
		
		List<Emp> empList = new ArrayList<Emp>();
		empList.add(new Emp(12,"Dinesh",50000));
		empList.add(new Emp(146,"Tom",20000));
		empList.add(new Emp(201,"John",40000));
		empList.add(new Emp(302,"Krish",44500));
		empList.add(new Emp(543,"Abdul",10000));
		
		Emp searchKey = new Emp(201,"John",40000);
		int index = Collections.binarySearch(empList, searchKey, new EmpComp());
		System.out.println("Index of the searched key: "+index);
	}
}

class EmpComp implements Comparator<Emp>{

	public int compare(Emp e1, Emp e2) {
		if(e1.getEmpId() == e2.getEmpId()){
			return 0;
		} else {
			return -1;
		}
	}
}

class Emp {
	
	private int empId;
	private String empName;
	private int empSal;
	
	public Emp(int id, String name, int sal){
		this.empId = id;
		this.empName = name;
		this.empSal = sal;
	}
	
	public int getEmpId() {
		return empId;
	}
	
	public void setEmpId(int empId) {
		this.empId = empId;
	}
	
	public String getEmpName() {
		return empName;
	}
	
	public void setEmpName(String empName) {
		this.empName = empName;
	}
	
	public int getEmpSal() {
		return empSal;
	}
	public void setEmpSal(int empSal) {
		this.empSal = empSal;
	}
	
	public String toString(){
		return empId+" : "+empName+" : "+empSal;
	}
}

Output:
Index of the searched key: 2
<< Previous Program | Next Program >>

List Of All Collections Class Sample Programs:

  1. How to add all elements to the given collection object?
  2. Write an example for Collections.asLifoQueue() method.
  3. How to search user defined object from a List by using binary search?
  4. Write an example for Collections.checkedCollection() method.
  5. Write an example for Collections.checkedList() method.
  6. Write an example for Collections.checkedSet() method.
  7. Write an example for Collections.checkedMap() method.
  8. How to check there in no common element between two list objects by using Collections.disjoint() method?
  9. How to create empty list using Collections class?
  10. How to create empty set using Collections class?
  11. How to create empty map using Collections class?
  12. How to Enumeration for ArrayList object?
  13. How to fill or replace elements of a List or ArrayList?
  14. How to find repeated element cound (frequency) of a given collection?
  15. How to convert Enumeration to List object?
  16. How to get index of a sub list from another list?
  17. How to get last index of a sub list from another list?
  18. How to get max element from the given list?
  19. How to get min element from the given list?
  20. How to get max element of a list of user defined objects?
  21. How to get min element of a list of user defined objects?
  22. How to get max element of a list of user defined objects using Comparator?
  23. How to get min element of a list of user defined objects using Comparator?
  24. How to create multiple copies of a given object?
  25. How to replace all occurances of a given object in the list?
  26. How to rotate elements in the list by specified distance?
  27. How to create synchronized list?
  28. How to create synchronized set?
  29. How to create synchronized map?
Knowledge Centre
When to use LinkedList or ArrayList?
Accessing elements are faster with ArrayList, because it is index based. But accessing is difficult with LinkedList. It is slow access. This is to access any element, you need to navigate through the elements one by one. But insertion and deletion is much faster with LinkedList, because if you know the node, just change the pointers before or after nodes. Insertion and deletion is slow with ArrayList, this is because, during these operations ArrayList need to adjust the indexes according to deletion or insetion if you are performing on middle indexes. Means, an ArrayList having 10 elements, if you are inserting at index 5, then you need to shift the indexes above 5 to one more.
Famous Quotations
Do not confuse motion and progress. A rocking horse keeps moving but does not make any progress.
-- Alfred A. Montapert

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.