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:at.gridtec.lambda4j.operator.binary.ThrowableLongBinaryOperator.java

/**
 * Returns a {@link ThrowableLongBinaryOperator} which returns the lesser of two elements according to the specified
 * {@code Comparator}.//from   w w w . java 2s .c o  m
 *
 * @param <X> The type of the throwable to be thrown by this operator
 * @param comparator A {@code Comparator} for comparing the two values
 * @return A {@code ThrowableLongBinaryOperator} which returns the lesser of its operands, according to the supplied
 * {@code Comparator}.
 * @throws NullPointerException If given argument is {@code null}
 * @see BinaryOperator#minBy(Comparator)
 */
@Nonnull
static <X extends Throwable> ThrowableLongBinaryOperator<X> minBy(@Nonnull final Comparator<Long> comparator) {
    Objects.requireNonNull(comparator);
    return (value1, value2) -> comparator.compare(value1, value2) <= 0 ? value1 : value2;
}

From source file:at.gridtec.lambda4j.operator.binary.ThrowableLongBinaryOperator.java

/**
 * Returns a {@link ThrowableLongBinaryOperator} which returns the greater of two elements according to the
 * specified {@code Comparator}.//w  w  w  .  j  ava 2  s  .  c  o  m
 *
 * @param <X> The type of the throwable to be thrown by this operator
 * @param comparator A {@code Comparator} for comparing the two values
 * @return A {@code ThrowableLongBinaryOperator} which returns the greater of its operands, according to the
 * supplied {@code Comparator}.
 * @throws NullPointerException If given argument is {@code null}
 * @see BinaryOperator#maxBy(Comparator)
 */
@Nonnull
static <X extends Throwable> ThrowableLongBinaryOperator<X> maxBy(@Nonnull final Comparator<Long> comparator) {
    Objects.requireNonNull(comparator);
    return (value1, value2) -> comparator.compare(value1, value2) >= 0 ? value1 : value2;
}

From source file:at.gridtec.lambda4j.operator.binary.ThrowableIntBinaryOperator.java

/**
 * Returns a {@link ThrowableIntBinaryOperator} which returns the lesser of two elements according to the specified
 * {@code Comparator}.// w  ww .j a va  2 s.c om
 *
 * @param <X> The type of the throwable to be thrown by this operator
 * @param comparator A {@code Comparator} for comparing the two values
 * @return A {@code ThrowableIntBinaryOperator} which returns the lesser of its operands, according to the supplied
 * {@code Comparator}.
 * @throws NullPointerException If given argument is {@code null}
 * @see BinaryOperator#minBy(Comparator)
 */
@Nonnull
static <X extends Throwable> ThrowableIntBinaryOperator<X> minBy(
        @Nonnull final Comparator<Integer> comparator) {
    Objects.requireNonNull(comparator);
    return (value1, value2) -> comparator.compare(value1, value2) <= 0 ? value1 : value2;
}

From source file:at.gridtec.lambda4j.operator.binary.ThrowableIntBinaryOperator.java

/**
 * Returns a {@link ThrowableIntBinaryOperator} which returns the greater of two elements according to the specified
 * {@code Comparator}.//from w ww.  j  a v  a 2  s.c om
 *
 * @param <X> The type of the throwable to be thrown by this operator
 * @param comparator A {@code Comparator} for comparing the two values
 * @return A {@code ThrowableIntBinaryOperator} which returns the greater of its operands, according to the supplied
 * {@code Comparator}.
 * @throws NullPointerException If given argument is {@code null}
 * @see BinaryOperator#maxBy(Comparator)
 */
@Nonnull
static <X extends Throwable> ThrowableIntBinaryOperator<X> maxBy(
        @Nonnull final Comparator<Integer> comparator) {
    Objects.requireNonNull(comparator);
    return (value1, value2) -> comparator.compare(value1, value2) >= 0 ? value1 : value2;
}

From source file:at.gridtec.lambda4j.operator.binary.ThrowableDoubleBinaryOperator.java

/**
 * Returns a {@link ThrowableDoubleBinaryOperator} which returns the lesser of two elements according to the
 * specified {@code Comparator}.// w w  w . j  a  v a  2 s  .  c o  m
 *
 * @param <X> The type of the throwable to be thrown by this operator
 * @param comparator A {@code Comparator} for comparing the two values
 * @return A {@code ThrowableDoubleBinaryOperator} which returns the lesser of its operands, according to the
 * supplied {@code Comparator}.
 * @throws NullPointerException If given argument is {@code null}
 * @see BinaryOperator#minBy(Comparator)
 */
@Nonnull
static <X extends Throwable> ThrowableDoubleBinaryOperator<X> minBy(
        @Nonnull final Comparator<Double> comparator) {
    Objects.requireNonNull(comparator);
    return (value1, value2) -> comparator.compare(value1, value2) <= 0 ? value1 : value2;
}

From source file:at.gridtec.lambda4j.operator.binary.ThrowableDoubleBinaryOperator.java

/**
 * Returns a {@link ThrowableDoubleBinaryOperator} which returns the greater of two elements according to the
 * specified {@code Comparator}./*from  www  .ja  v a2  s.  com*/
 *
 * @param <X> The type of the throwable to be thrown by this operator
 * @param comparator A {@code Comparator} for comparing the two values
 * @return A {@code ThrowableDoubleBinaryOperator} which returns the greater of its operands, according to the
 * supplied {@code Comparator}.
 * @throws NullPointerException If given argument is {@code null}
 * @see BinaryOperator#maxBy(Comparator)
 */
@Nonnull
static <X extends Throwable> ThrowableDoubleBinaryOperator<X> maxBy(
        @Nonnull final Comparator<Double> comparator) {
    Objects.requireNonNull(comparator);
    return (value1, value2) -> comparator.compare(value1, value2) >= 0 ? value1 : value2;
}

From source file:com.evolveum.midpoint.util.MiscUtil.java

/**
 * Only zero vs non-zero value of comparator is important. 
 *///from  w w  w .j a  va 2  s .c  o  m
public static <T> boolean unorderedArrayEquals(T[] a, T[] b, Comparator<T> comparator) {
    if (a == null && b == null) {
        return true;
    }
    if (a == null || b == null) {
        return false;
    }
    if (a.length != b.length) {
        return false;
    }
    List<T> outstanding = Arrays.asList(b);
    for (T ao : a) {
        boolean found = false;
        Iterator<T> iterator = outstanding.iterator();
        while (iterator.hasNext()) {
            T oo = iterator.next();
            if (comparator.compare(ao, oo) == 0) {
                iterator.remove();
                found = true;
            }
        }
        if (!found) {
            return false;
        }
    }
    if (!outstanding.isEmpty()) {
        return false;
    }
    return true;
}

From source file:Main.java

/**
 * A shell sort//from  w w  w . j  a v  a2  s .  c o m
 * 
 * @author Jason Harrison
 */
public static <T> void shellSort(List<T> list, Comparator<? super T> c) {
    int h = 1; /* find the largest h value possible */
    while ((h * 3 + 1) < list.size()) {
        h = 3 * h + 1;
    } /* 
       * while h remains larger than 0 
       */
    while (h > 0) { /* 
                     * for each set of elements (there are h sets) 
                     */
        for (int i = h - 1; i < list.size(); i++) { /*
                                                     * pick the last element in the set 
                                                     */
            T A;
            T B = list.get(i);
            int j = i;
            /* 
             * compare the element at B to the one before it in the set 
             * if they are out of order continue this loop, moving
             * elements "back" to make room for B to be inserted. 
             */
            for (j = i; (j >= h) && (c.compare((A = list.get(j - h)), B) > 0); j -= h) {
                list.set(j, A);
            } /* 
               * insert B into the correct place
               */
            list.set(j, B);
        } /*
           * all sets h-sorted, now decrease set size
           */
        h = h / 3;
    }
}

From source file:com.evolveum.midpoint.util.MiscUtil.java

/**
 * Only zero vs non-zero value of comparator is important. 
 *///from  w w  w.  java 2s.c  o m
public static boolean unorderedCollectionEquals(Collection a, Collection b, Comparator comparator) {
    if (a == null && b == null) {
        return true;
    }
    if (a == null || b == null) {
        return false;
    }
    if (a.size() != b.size()) {
        return false;
    }
    Collection outstanding = new ArrayList(b.size());
    outstanding.addAll(b);
    for (Object ao : a) {
        boolean found = false;
        Iterator iterator = outstanding.iterator();
        while (iterator.hasNext()) {
            Object oo = iterator.next();
            if (comparator.compare(ao, oo) == 0) {
                iterator.remove();
                found = true;
            }
        }
        if (!found) {
            return false;
        }
    }
    if (!outstanding.isEmpty()) {
        return false;
    }
    return true;
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.scheduler.policy.CompoundComparator.java

@Override
public int compare(final SchedulableEntity r1, final SchedulableEntity r2) {
    for (Comparator<SchedulableEntity> comparator : comparators) {
        int result = comparator.compare(r1, r2);
        if (result != 0)
            return result;
    }//from  ww  w . jav  a 2 s  .  c  o  m
    return 0;
}