Home  >  Core Java

Example of Collections.binarySearch in Java

By Arvind Rai, May 18, 2013
Java Collections.binarySearch() searches a sublist in the given list using binary search algorithm and returns the index of sublist. The given list must be in ascending sorted order before binary search. The ordering will be achieved by Comparable and Comparator. Here we will provide example using Comparable and Comparator.

binarySearch() with Comparable

According to java doc, the method syntax of binarySearch() with Comparable is given below.
public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key) 
 
Now find the example
BinarySearchWithComparable.java
package com.concretepage.util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class BinarySearchWithComparable {
    public static void main(String[] args) {
        Person a1 = new Person("Ram");
        Person a2 = new Person("Shayam");
        Person a3 = new Person("Rahim");
        List<Person> list = new ArrayList<Person>();
        list.add(a1);
        list.add(a2);
        list.add(a3);
        Collections.sort(list);
        for (Person a : list) {
            System.out.print(a.getName() + " ");
        }
        Person a = new Person("Shayam");
        int index = Collections.binarySearch(list, a);
        System.out.println("\nIndex of Shayam:"+index);
    }
}
class Person implements Comparable<Person> {
    private String name;
    public Person(String name) {
        this.name = name;
    }
    @Override
    public int compareTo(Person o) {
        return name.compareTo(o.name);
    }
    public String getName() {
	return name;
    }
}
Output
Rahim Ram Shayam 
Index of Shayam:2 

binarySearch() with Comparator

According to java doc, the method syntax of binarySearch() with Comparator is given below.
public static <T> int binarySearch(List<? extends T> list, T key, Comparator<? super T> c) 
 
Now find the example
BinarySearchWithComparator.java
package com.concretepage.util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class BinarySearchWithComparator {
    public static void main(String[] args) {
        User a1 = new User("Ram");
        User a2 = new User("Shayam");
        User a3 = new User("Rahim");
        List<User> list = new ArrayList<User>();
        list.add(a1);
        list.add(a2);
        list.add(a3);
        Collections.sort(list, new UserComparator());
        for (User a : list) {
            System.out.print(a.getName() + " ");
        }
        User a = new User("Shayam");
        int index = Collections.binarySearch(list, a, new UserComparator());
        System.out.println("\nIndex of Shayam:"+index);
    }
}
class User {
    private String name;
    public User(String name) {
        this.name = name;
    }
    public String getName() {
	return name;
    }
}
Output
package com.concretepage.util;
import java.util.Comparator;
public class UserComparator implements Comparator<User> {
	@Override
	public int compare(User o1, User o2) {
		return o1.getName().compareTo(o2.getName());
	}
}  
Output
Rahim Ram Shayam 
Index of Shayam:2 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us