Example usage for java.util Collections binarySearch

List of usage examples for java.util Collections binarySearch

Introduction

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

Prototype

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

Source Link

Document

Searches the specified list for the specified object using the binary search algorithm.

Usage

From source file:Main.java

public static void main(String args[]) {
    // create arraylist       
    ArrayList<String> arlst = new ArrayList<String>();

    arlst.add("A");
    arlst.add("B");
    arlst.add("C");
    arlst.add("D");

    // search for key 
    int index = Collections.binarySearch(arlst, "D", Collections.reverseOrder());

    System.out.println(index);/*from   w w  w .jav a2  s.  co  m*/

    index = Collections.binarySearch(arlst, "D");

    System.out.println(index);
}

From source file:Main.java

public static <V, Compare extends Comparator<V>> int insertionPoint(List<V> list, V item, Compare compare) {
    int i = Collections.binarySearch(list, item, compare);
    if (i < 0) {
        return -1 - i;
    } else//  w ww  .j a v a 2s. c o m
        return i;
}

From source file:Main.java

/**
 * Returns the first position in 'list' where 'key' could be inserted without violating ordering according to 'comparator'.
 * (Collections.binarySearch makes no guarantee about which index will be returned if the list contains multiple elements equal
 * to the key.)/*from  w ww  .  j  a  v a  2 s. c  om*/
 * 
 * As with Collections.binarySearch, results are undefined if 'list' is not sorted into ascending order according to
 * 'comparator'.
 */
public static <T> int lowerBound(List<? extends T> list, T key, Comparator<? super T> comparator) {
    int index = Collections.binarySearch(list, key, comparator);
    if (index < 0) {
        return -index - 1;
    }
    // FIXME: O(n) is distressing on a sorted list.
    while (index > 0 && comparator.compare(list.get(index - 1), key) == 0) {
        --index;
    }
    return index;
}

From source file:Main.java

/**
 * Returns the last position in 'list' where 'key' could be inserted without violating ordering according to 'comparator'.
 * (Collections.binarySearch makes no guarantee about which index will be returned if the list contains multiple elements equal
 * to the key.)/*from www.j  ava 2 s .  c om*/
 * 
 * As with Collections.binarySearch, results are undefined if 'list' is not sorted into ascending order according to
 * 'comparator'.
 */
public static <T> int upperBound(List<? extends T> list, T key, Comparator<? super T> comparator) {
    int index = Collections.binarySearch(list, key, comparator);
    if (index < 0) {
        return -index - 1;
    }
    // FIXME: O(n) is distressing on a sorted list.
    while (index + 1 < list.size() && comparator.compare(list.get(index + 1), key) == 0) {
        ++index;
    }
    // We return the first index *past* the [run of] value[s].
    return index + 1;
}

From source file:Main.java

public static <T> int binarySearch(List<T> list, T target, Comparator<T> comparator) {
    Collections.sort(list, comparator);
    return Collections.binarySearch(list, target, comparator);
}

From source file:Main.java

public static final <T> List<T> collect(Iterator<? extends T> it, Comparator<? super T> cmp) {
    ArrayList<T> r = new ArrayList<T>();
    while (it.hasNext()) {
        T i = it.next();/* ww w.  j a v a  2s.  co  m*/
        int position = Collections.binarySearch(r, i, cmp);
        if (position < 0)
            position = -(position + 1);
        r.add(position, i);
    }
    return Collections.unmodifiableList(r);
}

From source file:Main.java

/**
 * Finds the index where an object should be inserted in a collection based
 * on the following algorithm: if the list is empty the insertion index is 0 <br/>
 * If equal element exists, the insertion index is the index of the existing
 * element+1. If multiple equal elements exist, the insertion index is after
 * the elements.//www  .  j  a  v  a  2s.c  o  m
 * 
 * @param <E>
 *            type of the object
 * @param list
 *            list where the element should be inserted.
 * @param value
 *            element that should be inserted.
 * @param comparator
 *            comparator used to compare objects
 * @return index (place) where the element should be inserted.
 */
public static <E> int findInsertionIndex(List<E> list, E object, Comparator<E> comparator) {
    // call binary search
    int binarySearchResult = Collections.binarySearch(list, object, comparator);

    int index;
    if (binarySearchResult < 0) {
        // if the result is negative turn it to positive and decrease it by
        // one (see binarySearch doc)
        index = Math.abs(binarySearchResult) - 1;
    } else {
        // if the result is positive, increase it by one
        index = binarySearchResult + 1;

        // if there are multiple equal elements iterate to find the latest
        while (index < list.size() && comparator.compare(list.get(index), object) == 0) {
            index++;
        }
    }

    return index;
}

From source file:Main.java

public static <T extends Object> int binarySearch(final List<? extends T> list, final T element,
        final Comparator<? super T> comparator) {
    return Collections.binarySearch(list, element, comparator);
}

From source file:Main.java

/**
 * Insert an element into an ascending/descending array, and keep the order. 
 * @param ascending/* w w  w .  j  a  va2  s. c  o m*/
 *            if true, the array is sorted in ascending order,
 *            otherwise it is in descending order.
 * 
 */
static <T extends Comparable<T>> void binaryInsert(List<T> list, T value, boolean ascending) {

    int position = Collections.binarySearch(list, value, getComparator(ascending, (T) null));
    if (position < 0) {
        position = (-position) - 1;
    }
    list.add(position, value);
}

From source file:Main.java

/**
 * Returns a three-element array of mutable {@code List}s:
 * <ol>/*from  w  w  w . j a va2  s .  com*/
 *    </li>
 *      the intersection of {@code a} and {@code b}
 *    <li>
 *    </li>
 *      the {@code a} - {@code b} difference
 *    <li>
 *    </li>
 *      the {@code b} - {@code a} difference
 *    <li>
 * </ol>
 *
 * @param       comparator
 *          a comparator to sort results, or {@code null} to remain
 *          unsorted
 */
public static <T> List<T>[] getIntersectAndDiffs(Collection<? extends T> a, Collection<? extends T> b,
        Comparator<T> comparator) {

    int aSize = a.size();
    List<T> aDiff = new ArrayList<T>(aSize);
    aDiff.addAll(a);

    int bSize = b.size();
    List<T> bDiff = new ArrayList<T>(bSize);
    bDiff.addAll(b);

    if (comparator != null) {
        Collections.sort(aDiff, comparator);
        Collections.sort(bDiff, comparator);
    }

    List<T> abInter = new ArrayList<T>(Math.min(aSize, bSize));

    for (int i = aDiff.size() - 1; i >= 0; i--) {
        T element = aDiff.get(i);

        int index = comparator == null ? bDiff.indexOf(element)
                : Collections.binarySearch(bDiff, element, comparator);

        if (index != -1) {
            bDiff.remove(index);
            aDiff.remove(i);
            abInter.add(element);
        }
    }

    @SuppressWarnings({ "unchecked" })
    List<T>[] array = (List<T>[]) new List[] { abInter, aDiff, bDiff };

    return array;
}