12:51 am - Thursday July 27, 2017

Search a particular list in collection using binarySearch(List,T,Comparator)

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

                          The method is used to search 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.

Example Program

import java.util.Collections;

import java.util.ArrayList;

public class collectionSearch

{

	public static void main(String[] args)

	{

		ArrayList v = new ArrayList();

		v.add("Apple");

		v.add("Orange");

		v.add("Pineapple");

		int n = Collections.binarySearch(v, "Orange");

		System.out.println("'Orange' is available at index: " + n);

	}
}

Output

  ‘Orange’ is available at index: 1

BACK

Filed in: Java Examples

Comments are closed.