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 <T> Comparator<T> inverseComparator(final Comparator<T> comp) {
    return new Comparator<T>() {
        public int compare(T o1, T o2) {
            return comp.compare(o2, o1);
        }/*w w  w .ja  v  a 2s. co m*/
    };
}

From source file:Main.java

public static <T> List range(List<? extends T> list, Object min, Object max, Comparator comparator) {
    List<T> tList = new ArrayList<>();
    for (int i = 0; i < list.size(); i++) {
        if (comparator.compare(list.get(i), min) >= 0 && comparator.compare(list.get(i), max) <= 0) {
            tList.add(list.get(i));//from ww w . j  a v  a 2 s. c  om
        }
    }
    return tList;
}

From source file:Main.java

/**
 * Answer whether or not coll contains Object o, 
 * but not based on equal(), but on compareTo()==0
 * @param coll//w w  w  .  j  a va 2s .co m
 * @param o
 * @param c
 * @return
 */
public static boolean contains(Collection coll, Object o, Comparator c) {
    boolean answer = false;
    for (Iterator i = coll.iterator(); i.hasNext() && !answer;) {
        answer = 0 == c.compare(o, i.next());
    }
    return answer;
}

From source file:Main.java

/**
 * //from w ww  .j av  a2 s . c  om
 * @param <T>
 * @param array
 * @param key
 * @param c
 * @param start inclusive
 * @param end exclusive
 * @return 
 */
public static <T> int ceil(T[] array, T key, Comparator<? super T> c, int start, int end) {
    if (c.compare(array[start], key) >= 0) {
        return start;
    }
    end--;
    if (c.compare(array[end], key) < 0) {
        return -1;
    }
    int res;
    T mid;
    while (start < end - 1) {
        mid = array[(start + end) / 2];
        res = c.compare(mid, key);
        //            System.out.println("res = " + res);
        //            System.out.println("mid = " + mid);
        if (res >= 0) {
            end = (start + end) / 2;
        } else {
            start = (start + end) / 2;
        }
        //            System.out.println("start = " + start);
        //            System.out.println("end = "+ end);
    }
    res = c.compare(array[start], key);
    if (res < 0) {
        return end;
    } else {
        if (res == 0) {
            return start;
        } else {
            return -1;
        }
    }
}

From source file:Main.java

/**
 * Checks if the given object (or range lower bound) lies above another
 * lower bound./*from  w ww  . ja  va 2 s .c  o  m*/
 * 
 * <p>Generally, this is used to test if an object is above the specified
 * lower bound (in which case, the parameter {@code oIncluded} will be true).
 * But we can also tell if the lower bound of one range (specified by the
 * parameters {@code o} and {@code oIncluded}) lies above the lower bound
 * of another range (specified by {@code from} and {@code fromInclusive}).
 *
 * @param <T> the type of the element
 * @param o the object
 * @param oIncluded true if {@code o} needs to be included in the range
 * @param from the lower bound of the range
 * @param fromInclusive true if {@code from} is included in the range; false
 *       otherwise
 * @param comp the comparator used to compare {@code o} to {@code from}
 *       (cannot be null)
 * @return true if the specified object is greater than the bound and the
 *       bound is exclusive; true if the object is greater than or equal
 *       to the bound and the bound is inclusive; false otherwise
 */
public static <T> boolean isInRangeLow(T o, boolean oIncluded, T from, boolean fromInclusive,
        Comparator<? super T> comp) {
    if (from != null) {
        int c = comp.compare(o, from);
        if (c < 0 || (c == 0 && oIncluded && !fromInclusive)) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

private static <T> void eqBrute(List<T> list, int lo, int hi, Comparator<? super T> c) {
    if ((hi - lo) == 1) {
        if (c.compare(list.get(hi), list.get(lo)) < 0) {
            T e = list.get(lo);//from ww w. j a  v  a2  s . c o m
            list.set(lo, list.get(hi));
            list.set(hi, e);
        }
    } else if ((hi - lo) == 2) {
        int pmin = c.compare(list.get(lo), list.get(lo + 1)) < 0 ? lo : lo + 1;
        pmin = c.compare(list.get(pmin), list.get(lo + 2)) < 0 ? pmin : lo + 2;
        if (pmin != lo) {
            T e = list.get(lo);
            list.set(lo, list.get(pmin));
            list.set(pmin, e);
        }
        eqBrute(list, lo + 1, hi, c);
    } else if ((hi - lo) == 3) {
        int pmin = c.compare(list.get(lo), list.get(lo + 1)) < 0 ? lo : lo + 1;
        pmin = c.compare(list.get(pmin), list.get(lo + 2)) < 0 ? pmin : lo + 2;
        pmin = c.compare(list.get(pmin), list.get(lo + 3)) < 0 ? pmin : lo + 3;
        if (pmin != lo) {
            T e = list.get(lo);
            list.set(lo, list.get(pmin));
            list.set(pmin, e);
        }
        int pmax = c.compare(list.get(hi), list.get(hi - 1)) > 0 ? hi : hi - 1;
        pmax = c.compare(list.get(pmax), list.get(hi - 2)) > 0 ? pmax : hi - 2;
        if (pmax != hi) {
            T e = list.get(hi);
            list.set(hi, list.get(pmax));
            list.set(pmax, e);
        }
        eqBrute(list, lo + 1, hi - 1, c);
    }
}

From source file:Main.java

static <E> Optional<E> max(List<E> elements, Comparator<E> comparator) {
    //TODO Implement me
    E maxValue = elements.get(0);// www.  j  a va 2s  .  c o m
    int i = 0;
    while (i <= elements.size()) {
        maxValue = comparator.compare(maxValue, elements.get(i)) > 0 ? maxValue : elements.get(i);
        i++;
    }
    return Optional.of(maxValue);
}

From source file:Main.java

public static <T> void sortWithIndexesComparator(final List<T> list, final Comparator<Integer> comparator) {
    Collections.sort(list, new Comparator<T>() {
        @Override/*from  www. j  av  a 2  s. c o  m*/
        public int compare(T lhs, T rhs) {
            return comparator.compare(list.indexOf(lhs), list.indexOf(rhs));
        }
    });
}

From source file:Main.java

public static <T> Collection<T> subtract(Collection<T> list1, Collection<T> list2, Comparator<? super T> comp) {
    Collection<T> removedList = new ArrayList<T>();

    for (T item1 : list1) {

        for (T item2 : list2) {
            if (comp.compare(item1, item2) == 0) {
                removedList.add(item1);/*w  w w .  ja  v  a2  s.co m*/
            }
        }
    }

    list1.removeAll(removedList);

    return list1;
}

From source file:Main.java

public static <E> Optional<E> min(List<E> elements, Comparator<E> comparator) {
    if (elements.size() != 0) {
        E minElement = elements.get(0);/*from w  w  w.  j  ava2 s .  com*/
        for (int i = 1; i < elements.size(); i++) {
            minElement = comparator.compare(minElement, elements.get(i)) < 0 ? minElement : elements.get(i);
        }
        return Optional.of(minElement);
    } else
        return Optional.empty();
}