List of usage examples for java.util Comparator compare
int compare(T o1, T o2);
From source file:de.tudarmstadt.lt.utilities.ArrayUtils.java
public static <T> int[] sortIdsByValue(final T[] values, final Comparator<T> comparator) { Integer[] ids = new Integer[values.length]; for (int id = 0; id < ids.length; ids[id] = id++) ;/*from ww w . j a v a2 s.c om*/ Arrays.sort(ids, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return comparator.compare(values[o1], values[o2]); } }); return org.apache.commons.lang.ArrayUtils.toPrimitive(ids); }
From source file:Main.java
/** * Comparator function suitable for use with reduce. * * <p>Use with reduce to obtain a min or max value.</p> * // w w w .j a v a 2s . com * @param comparator The comparator to use * @param <T> The compared type * @return The comparator function. * @see com.google.common.collect.Ordering */ public static <T> Function<T, Function<T, T>> comparator(final Comparator<T> comparator) { return new Function<T, Function<T, T>>() { @Override public Function<T, T> apply(final T t1) { return new Function<T, T>() { @Override public T apply(final T t2) { return comparator.compare(t1, t2) < 0 ? t1 : t2; } }; } }; }
From source file:QuickSort.java
/** * Shuffle elements around to partially-sorted low/pivot/high order * // www . j av a2 s .c om * @param <T> * @param array * @param comp * @param lo * @param hi * @param pivotIndex * the starting index of the pivot element * @return The new index of the pivot element */ private static <T> int partition(T[] array, Comparator<T> comp, int lo, int hi, int pivotIndex) { T pivot = array[pivotIndex]; // send pivot to the back swap(array, pivotIndex, hi); // index of low/high boundary int index = lo; for (int i = lo; i < hi; i++) { if (comp.compare(array[i], pivot) <= 0) { // element is lower or // equal to the pivot // swap it to the low // region swap(array, i, index); index++; } } swap(array, hi, index); return index; }
From source file:Main.java
/** * Return a List containing all the elements of the specified Iterable that compare as being * equal to the maximum element./*from w ww.ja v a 2 s.co m*/ * * @throws NoSuchElementException if the Iterable is empty. */ public static <T> List<T> maxList(Iterable<T> iterable, Comparator<? super T> comp) { Iterator<T> itr = iterable.iterator(); T max = itr.next(); List<T> maxes = new ArrayList<T>(); maxes.add(max); if (itr.hasNext()) { do { T elem = itr.next(); int cmp = comp.compare(max, elem); if (cmp <= 0) { if (cmp < 0) { max = elem; maxes.clear(); } maxes.add(elem); } } while (itr.hasNext()); } else if (0 != comp.compare(max, max)) { // The main point of this test is to compare the sole element to something, // in case it turns out to be incompatible with the Comparator for some reason. // In that case, we don't even get to this IAE, we've probably already bombed out // with an NPE or CCE. For example, the Comparable version could be called with // a sole element of null. (Collections.max() gets this wrong.) throw new IllegalArgumentException(); } return maxes; }
From source file:Main.java
public static <T> int compareAsList(Comparator<? super T> elementComparator, Collection<? extends T> list1, Collection<? extends T> list2) { int res = Ints.compare(list1.size(), list2.size()); if (res != 0) return res; Iterator<? extends T> elements2 = list2.iterator(); for (T element1 : list1) { res = elementComparator.compare(element1, elements2.next()); if (res != 0) return res; }//from w ww . ja v a2s.c o m return 0; }
From source file:Main.java
private static <E> E getSmallest(final Comparator<? super E> comparator, final List<? extends E> randomAccessList) { E result = randomAccessList.get(0);//from w w w.ja va2 s. c o m for (int i = 1; i < randomAccessList.size(); i++) { final E element = randomAccessList.get(i); if (comparator.compare(element, result) < 0) { result = element; } } return result; }
From source file:Main.java
private static <E> E getGreatest(final Comparator<? super E> comparator, final List<? extends E> randomAccessList) { E result = randomAccessList.get(0);/* www .ja v a 2s. c o m*/ for (int i = 1; i < randomAccessList.size(); i++) { final E element = randomAccessList.get(i); if (comparator.compare(element, result) > 0) { result = element; } } return result; }
From source file:org.springside.modules.utils.collection.CollectionUtil.java
/** * ???/*from ww w . j a v a 2 s . c o m*/ */ public static <T> Pair<T, T> minAndMax(Collection<? extends T> coll, Comparator<? super T> comp) { Iterator<? extends T> i = coll.iterator(); T minCandidate = i.next(); T maxCandidate = minCandidate; while (i.hasNext()) { T next = i.next(); if (comp.compare(next, minCandidate) < 0) { minCandidate = next; } else if (comp.compare(next, maxCandidate) > 0) { maxCandidate = next; } } return new ImmutablePair<T, T>(minCandidate, maxCandidate); }
From source file:Main.java
public static <T> int compareAsList(@Nonnull Comparator<? super T> elementComparator, @Nonnull Collection<? extends T> list1, @Nonnull Collection<? extends T> list2) { int res = Ints.compare(list1.size(), list2.size()); if (res != 0) return res; Iterator<? extends T> elements2 = list2.iterator(); for (T element1 : list1) { res = elementComparator.compare(element1, elements2.next()); if (res != 0) return res; }//from ww w. j av a2 s . c o m return 0; }
From source file:Main.java
/** * Finds the first index where the value is located or * the index where it could be inserted, similar to the regular * binarySearch() methods, but it works with duplicate elements. * @param <T> the element type//from w ww . jav a2s. co m * @param list the list to search * @param fromIndex the starting index, inclusive * @param toIndex the end index, exclusive * @param value the value to search * @param comparator the comparator * @return if positive, the exact index where the value is first * encountered, or -(insertion point - 1) if not in the array. */ public static <T> int findFirstIterator(List<? extends T> list, int fromIndex, int toIndex, T value, Comparator<? super T> comparator) { int a = fromIndex; int b = toIndex; ListIterator<? extends T> it = list.listIterator(); while (a <= b) { int mid = a + (b - a) / 2; T midVal = get(it, mid); int c = comparator.compare(midVal, value); if (c < 0) { a = mid + 1; } else if (c > 0) { b = mid - 1; } else { if (mid > 0) { if (comparator.compare(get(it, mid - 1), value) != 0) { return mid; } else { b = mid - 1; } } else { return 0; } } } return -(a + 1); }