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 int binarySearch(Object[] a, Object key) 

Source Link

Document

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

Usage

From source file:Main.java

public static boolean contains(int[] array, int value) {
    return Arrays.binarySearch(array, value) >= 0 ? true : false;
}

From source file:Main.java

public static <T> boolean containArray(T[] source, T[] target) {
    if (target == null || target.length == 0)
        return false;

    Arrays.sort(source);// w  w  w .  j  a v a 2s  .c  om
    Arrays.sort(target);
    for (T tech : target) {
        if (Arrays.binarySearch(source, tech) < 0) {
            continue;
        } else {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static int getLowestInsertionPoint(final double[] list, final double key) {
    int index = Arrays.binarySearch(list, key);
    if (index >= 0x00) {
        return index;
    } else {//from   w w w.  j a  va 2s .  c  o  m
        return ~index;
    }
}

From source file:fastcall.FArrayUtils.java

/**
 * Return the indices of unique string from input string array,
 * @param input//from   w w  w  .j av  a 2 s .  co  m
 * @return 
 */
public static int[] getUniqueStringIndices(String[] input) {
    String[] unique = getUniqueStringArray(input);
    int[] index = new int[unique.length];
    boolean[] ifMatch = new boolean[index.length];
    int cnt = 0;
    for (int i = 0; i < input.length; i++) {
        int hit = Arrays.binarySearch(unique, input[i]);
        if (ifMatch[hit])
            continue;
        index[cnt] = i;
        ifMatch[hit] = true;
        cnt++;
    }
    return index;
}

From source file:com.carlomicieli.jtrains.validation.ISOValidationUtils.java

/**
 * Returns {@code true} if the value is a valid 2-letter language code as defined in ISO 639.
 * @param lang the language code/*from   w  w  w  .j a  v a  2s .co m*/
 * @return {@code true} if the value is a valid language; {@code false} otherwise
 */
public static boolean languageIsValid(String lang) {
    if (StringUtils.isBlank(lang)) {
        return true;
    }
    return Arrays.binarySearch(Locale.getISOLanguages(), lang.toLowerCase()) >= 0;
}

From source file:Main.java

/**
 * Returns the index of the largest value in an array that is less than (or optionally equal to)
 * a specified key./*w  ww.ja  va 2  s  .  com*/
 * <p>
 * The search is performed using a binary search algorithm, and so the array must be sorted.
 *
 * @param a The array to search.
 * @param key The key being searched for.
 * @param inclusive If the key is present in the array, whether to return the corresponding index.
 *     If false then the returned index corresponds to the largest value in the array that is
 *     strictly less than the key.
 * @param stayInBounds If true, then 0 will be returned in the case that the key is smaller than
 *     the smallest value in the array. If false then -1 will be returned.
 */
public static int binarySearchFloor(long[] a, long key, boolean inclusive, boolean stayInBounds) {
    int index = Arrays.binarySearch(a, key);
    index = index < 0 ? -(index + 2) : (inclusive ? index : (index - 1));
    return stayInBounds ? Math.max(0, index) : index;
}

From source file:Main.java

/**
 * Returns the index of the smallest value in an array that is greater than (or optionally equal
 * to) a specified key./*from ww  w.j  a v a  2 s  .c  om*/
 * <p>
 * The search is performed using a binary search algorithm, and so the array must be sorted.
 *
 * @param a The array to search.
 * @param key The key being searched for.
 * @param inclusive If the key is present in the array, whether to return the corresponding index.
 *     If false then the returned index corresponds to the smallest value in the array that is
 *     strictly greater than the key.
 * @param stayInBounds If true, then {@code (a.length - 1)} will be returned in the case that the
 *     key is greater than the largest value in the array. If false then {@code a.length} will be
 *     returned.
 */
public static int binarySearchCeil(long[] a, long key, boolean inclusive, boolean stayInBounds) {
    int index = Arrays.binarySearch(a, key);
    index = index < 0 ? ~index : (inclusive ? index : (index + 1));
    return stayInBounds ? Math.min(a.length - 1, index) : index;
}

From source file:com.carlomicieli.jtrains.validation.ISOValidationUtils.java

/**
 * Returns {@code true} if the value is a valid 2-letter country code as defined in ISO 3166.
 * @param country the country code/*from   w  w w  .  jav a  2s  .  c  om*/
 * @return {@code true} if the value is a valid country; {@code false} otherwise
 */
public static boolean countryIsValid(String country) {
    if (StringUtils.isBlank(country)) {
        return true;
    }
    return Arrays.binarySearch(Locale.getISOCountries(), country.toUpperCase()) >= 0;
}

From source file:Main.java

/**
 * This method will compare the attributes of a given src element with the attributes of a given dest element.
 * Both must contain the same attributes, but you can specify a String array of attribute names whose values
 * are ignored. Both elements must have the ignore attributes, their contents can be different and they will
 * still be considered equal.// w  ww.j  a  v a2 s . co  m
 * @param src - the src element whose attributes are to be compared
 * @param dest - the dest element whose attributes are to be compared
 * @param ignore - the string array of attributes whose values are to be ignored during the compare.
 * @return true if the attributes of both nodes meet the criteria of being equal, false otherwise.
 */
private static boolean compareAttributes(Element src, Element dest, String[] ignore) {
    NamedNodeMap srcAttrs = src.getAttributes();

    if (srcAttrs.getLength() != dest.getAttributes().getLength())
        return false;

    for (int ctr = 0; ctr < srcAttrs.getLength(); ctr++) {
        Node srcAttr = srcAttrs.item(ctr);

        String name = srcAttr.getNodeName();
        if (Arrays.binarySearch(ignore, name) < 0) {
            Node destAttr = dest.getAttributeNode(name);
            if (destAttr == null || !srcAttr.isEqualNode(destAttr)) {
                return false;
            }
        }
    }

    return true;
}

From source file:edu.umd.cfar.lamp.chronicle.ChronicleRuler.java

private static int findChronicleWidthsApproximation(long toApprox) {
    int w = Arrays.binarySearch(ChronicleRuler.widths, toApprox);
    if (w < 0) {
        w = -w - 1;/*from  ww  w.  j  av  a  2  s.c o  m*/
    }
    assert w >= 0;
    assert w < ChronicleRuler.widths.length;
    return w;
}