Example usage for java.util Comparator compare

List of usage examples for java.util Comparator compare

Introduction

In this page you can find the example usage for java.util Comparator compare.

Prototype

int compare(T o1, T o2);

Source Link

Document

Compares its two arguments for order.

Usage

From source file:Main.java

public static <E> Optional<E> max(List<E> elements, Comparator<E> comparator) {
    if (elements.size() != 0) {
        E maxElement = elements.get(0);/*from  ww  w  . j av  a2s .c o m*/
        for (int i = 1; i < elements.size(); i++) {
            maxElement = comparator.compare(maxElement, elements.get(i)) > 0 ? maxElement : elements.get(i);
        }
        return Optional.of(maxElement);
    } else
        return Optional.empty();
}

From source file:Main.java

public static <T extends Object> boolean isEqual(Collection<? extends T> coll, Comparator<? super T> comp) {

    Iterator<? extends T> i = coll.iterator();
    T first = i.next();//w  w  w . j  ava 2s  . com
    while (i.hasNext()) {
        T next = i.next();
        if (comp.compare(first, next) < 0) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> Collection<T> substract(Collection<T> oldList, Collection<T> newList,
        java.util.Comparator comparator) {
    Collection<T> result = new ArrayList<T>(oldList);
    for (T oldValue : oldList) {
        for (T newValue : newList) {
            if (comparator.compare(oldValue, newValue) == 0) {
                result.remove(oldValue);
            }// w  w w .  j a  v  a2  s . c  o  m
        }
    }
    return result;
}

From source file:Main.java

public static <T> T qnth(List<T> sample, int n, Comparator<T> comp) {
    T pivot = sample.get(0);//  w  w  w  . ja  va  2  s.  com
    List<T> below = new ArrayList<T>(), above = new ArrayList<T>();
    for (T s : sample) {

        int flag = comp.compare(s, pivot);
        if (flag < 0) {
            above.add(s);
        } else if (flag > 0) {
            below.add(s);
        }
    }
    int i = below.size(), j = sample.size() - above.size();
    if (n < i)
        return qnth(below, n, comp);
    else if (n >= j)
        return qnth(above, n - j, comp);
    else
        return pivot;
}

From source file:Main.java

public static <T extends Comparable<T>> int compare(Collection<T> lhs, Collection<T> rhs,
        Comparator<T> comparator) {
    int cmp;/*from  w  w  w. j a  v  a 2s.  co  m*/
    Iterator<T> lit = lhs.iterator();
    Iterator<T> rit = rhs.iterator();
    while (lit.hasNext() && rit.hasNext()) {
        cmp = comparator.compare(lit.next(), rit.next());
        if (cmp != 0) {
            return cmp;
        }
    }
    return lhs.size() - rhs.size();
}

From source file:Quicksort.java

private static int median(Object[] a, int left, int right, Comparator c) {
    int center = (left + right) / 2;
    if (c.compare(a[left], a[center]) > 0)
        swap(a, left, center);//from   w w  w.  j a  v  a 2  s.  co m
    if (c.compare(a[left], a[right]) > 0)
        swap(a, left, right);
    if (c.compare(a[center], a[right]) > 0)
        swap(a, center, right);
    swap(a, center, right - 1);
    return right - 1;
}

From source file:Main.java

/**
 * Search the position of the given object in the list of ordered objects.
 * To compare objects between them the given comparator is used. This method
 * returns the position <code>pos</code> of the object in the list or if
 * there is no such an object in the list then this method returns
 * <code>-(pos+1)</code> where <code>pos</code> is the position where
 * the object should be inserted.//from  ww w  .  java 2 s.  co m
 * 
 * <pre>
 * This code shows how this class can be used:
 * 
 * List list = ...
 * int pos = CollectionsUtil.search(list, "abc", new Comparator() {
 *     public int compare(T o1, T o2) {
 *          String s1 = (String) o1;
 *          String s2 = (String) o2;
 *          return s1.compare(s2);
 *     }
 * });
 * if (pos >= 0) {
 *      System.out.println("The object was found in the " 
 *          + pos 
 *          + " position");
 * } else {
 *      pos = -(pos + 1);
 *      System.out.println("The object should be inserted in the " 
 *          + pos 
 *          + " position");
 * }
 * </pre>
 * 
 * @param list the sorted list of objects
 * @param value the value to check
 * @param c the comparator used to compare objects from the list and the
 *        given object
 * @return the position of the object in the list or <code>-(pos+1)</code>
 *         where <code>pos</code> is the position where the object should
 *         be inserted
 */
public static int search(List list, Object value, Comparator c) {
    int low = 0;
    int high = list.size() - 1;
    while (low <= high) {
        int mid = (low + high) >>> 1;
        Object midVal = list.get(mid);
        int cmp = c.compare(midVal, value);
        if (cmp < 0)
            low = mid + 1;
        else if (cmp > 0)
            high = mid - 1;
        else
            return mid; // key found
    }
    return -(low + 1); // key not found
}

From source file:Main.java

public static <T> void testComparator(List<? extends T> list, Comparator<T> comparator) {
    for (int i = 0; i < list.size(); i++) {
        for (int j = i; j < list.size(); j++) {
            T oi = list.get(i);//from   ww  w  . j  av  a  2s  . com
            T oj = list.get(j);

            int ci = comparator.compare(oi, oj);
            int cj = comparator.compare(oj, oi);

            if (Math.signum(ci) != -Math.signum(cj)) {
                throw new RuntimeException("Bad comparator: " + comparator + "\n\tlist.get(" + i + "): " + oi
                        + "\n\tlist.get(" + j + "): " + oj + "\ncompare(list.get(" + i + "), list.get(" + j
                        + ")): " + ci + "\ncompare(list.get(" + j + "), list.get(" + i + ")): " + cj);
            }
        }
    }
}

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.)/* w ww  . j  ava 2 s  . co m*/
 * 
 * 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.)/*  w  w  w. j ava  2 s.  co  m*/
 * 
 * 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;
}