Example usage for java.util Arrays binarySearch

List of usage examples for java.util Arrays binarySearch

Introduction

In this page you can find the example usage for java.util Arrays binarySearch.

Prototype

public static <T> int binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c) 

Source Link

Document

Searches a range of the specified array for the specified object using the binary search algorithm.

Usage

From source file:Main.java

public static void main(String[] args) {

    // initializing sorted short array
    Short shortArr[] = new Short[] { 1, 2, 15, 52, 110 };

    // use comparator as null, sorting as natural ordering
    Comparator<Short> comp = null;

    // entering the value to be searched 
    short searchVal = 15;

    // search between index 1 and 4
    int retVal = Arrays.binarySearch(shortArr, 1, 4, searchVal, comp);
    System.out.println("The index of element 15 is : " + retVal);

}

From source file:mondrian.util.UtilCompatibleJdk16.java

public <T extends Comparable<T>> int binarySearch(T[] ts, int start, int end, T t) {
    return Arrays.binarySearch(ts, start, end, t, RolapUtil.ROLAP_COMPARATOR);
}

From source file:com.unister.semweb.drums.bucket.hashfunction.RangeHashFunction.java

/**
 * Searches for the given <code>key</code> in {@link #maxRangeValues} and returns the index of the corresponding
 * range. Remember: this may not be the bucketId
 *///from w w  w  .  j a  va  2s .c  o m
protected int searchBucketIndex(byte[] key, int leftIndex, int rightIndex) {
    if (KeyUtils.compareKey(key, maxRangeValues[rightIndex]) > 0) {
        return 0;
    }
    int idx = Arrays.binarySearch(maxRangeValues, leftIndex, rightIndex, key, new ByteArrayComparator());
    idx = idx < 0 ? -idx - 1 : idx;
    if (idx > rightIndex) {
        return -1;
    } else {
        return idx;
    }
}