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> Collection<E> addGreater(Collection<E> collection, E element, Comparator<E> comp) {
    Collection<E> resultColl = new LinkedList<>();
    for (E e : collection) {
        if (comp.compare(e, element) > 0)
            resultColl.add(e);/* ww  w  . j  a v a 2 s .  co  m*/
    }
    return resultColl;
}

From source file:Main.java

/**
 * If comp is not null compares using it. Otherwise assumes o1 is 
 * Comparable<T>./*from   www  . ja  v a  2  s.co m*/
 * @param <T>
 * @param o1
 * @param o2
 * @param comp
 * @return 
 */
public static final <T> int compare(T o1, T o2, Comparator<T> comp) {
    if (comp != null) {
        return comp.compare(o1, o2);
    } else {
        return ((Comparable<T>) o1).compareTo(o2);
    }
}

From source file:Main.java

public static <T> T maxElement(Iterable<T> collection, Comparator<T> comparator) {
    T result = null;/*www  .ja v a  2 s . co  m*/
    for (T element : collection) {
        if (result == null || comparator.compare(result, element) < 0)
            result = element;
    }
    return result;
}

From source file:Main.java

public static <T> T minElement(Iterable<T> collection, Comparator<T> comparator) {
    T result = null;//  w  w  w . ja v a  2s .co m
    for (T element : collection) {
        if (result == null || comparator.compare(result, element) > 0)
            result = element;
    }
    return result;
}

From source file:Main.java

public static <T> int ceil(ArrayList<T> list, T key, Comparator<? super T> c) {
    if (c.compare(list.get(0), key) >= 0) {
        return 0;
    }//from   w w  w .  j  ava 2s .com
    if (c.compare(list.get(list.size() - 1), key) < 0) {
        return -1;
    }
    int start = 0, end = list.size() - 1, res;
    T mid;
    while (start < end - 1) {
        mid = list.get((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(list.get(start), key);
    if (res < 0) {
        return end;
    } else {
        if (res == 0) {
            return start;
        } else {
            return -1;
        }
    }

}

From source file:Main.java

public static <T extends Comparable<? super T>> List range(List<? extends T> list, T min, T max,
        Comparator<T> comparator) {
    List<T> result = new ArrayList<T>();

    for (T temp : list) {
        if (comparator.compare(min, temp) >= 0 && comparator.compare(max, temp) <= 0)
            result.add(temp);//from w w  w.j av a2 s.  c  o  m
    }
    return result;
}

From source file:Main.java

public static <T> T min(Iterator<? extends T> iterator, Comparator<T> comparator) {
    T next, candidate = iterator.next();
    while (iterator.hasNext()) {
        if (comparator.compare(next = iterator.next(), candidate) < 0) {
            candidate = next;//w  ww . j av a  2 s  .  c  om
        }
    }
    return candidate;
}

From source file:Main.java

public static <T> List range(List<T> list, Object min, Object max, Comparator<? super T> comparator) {
    List<T> des = newArrayList();
    for (T o : list) {
        if ((comparator.compare(o, (T) min) >= 0) && comparator.compare(o, (T) max) <= 0) {
            des.add(o);/*from  w  w  w  . j a va 2  s.co  m*/
        }
    }
    return des;
}

From source file:Main.java

public static <T> int floor(T[] array, T key, Comparator<? super T> c, int start, int end) {
    if (c.compare(array[start], key) > 0) {
        return -1;
    }//  w  w  w .  j  a va2 s  . co m
    end--;
    if (c.compare(array[end], key) <= 0) {
        return end;
    }
    int res;
    T mid;
    while (start < end - 1) {
        mid = array[(start + end) / 2];
        res = c.compare(mid, key);
        if (res > 0) {
            end = (start + end) / 2;
        } else {
            start = (start + end) / 2;
        }
    }
    res = c.compare(array[end], key);
    if (res > 0) {
        return start;
    } else {
        if (res == 0) {
            return end;
        } else {
            return -1;
        }
    }
}

From source file:Main.java

public static <T> List<T> range(List<? extends T> list, T min, T max, Comparator<T> comparator) {
    List<T> resultList = new ArrayList<T>();
    for (T elem : list) {
        if (comparator.compare(elem, min) >= 0 && comparator.compare(elem, max) <= 0) {
            resultList.add(elem);//from  www .  ja v a  2 s . co  m
        }
    }
    Collections.sort(resultList, comparator);
    return resultList;
}