Java Collection Max minOrMax(int sign, Collection values)

Here you can find the source of minOrMax(int sign, Collection values)

Description

min Or Max

License

Apache License

Declaration

private static <T extends Comparable> T minOrMax(int sign, Collection<T> values) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    private static <T extends Comparable> T minOrMax(int sign, Collection<T> values) {
        if (values.isEmpty()) {
            return null;
        }/*from   ww  w  .  j av a2 s .  co  m*/
        T result = values.iterator().next();
        for (T value : values) {
            if (value == null) {
                continue;
            }
            if ((result == null) || (result.compareTo(value) * sign < 0)) {
                result = value;
            }
        }
        return result;
    }

    private static <T extends Comparable> T minOrMax(int sign, T... values) {
        if (values.length == 0) {
            return null;
        }
        T result = values[0];
        for (T value : values) {
            if (value == null) {
                continue;
            }
            if ((result == null) || (result.compareTo(value) * sign < 0)) {
                result = value;
            }
        }
        return result;
    }

    private static int minOrMax(int sign, int... values) {
        if (values.length == 0) {
            return 0;
        }
        int result = values[0];
        for (int value : values) {
            if ((result - value) * sign < 0) {
                result = value;
            }
        }
        return result;
    }

    public static boolean isEmpty(Object[] array) {
        return array == null || array.length == 0;
    }
}

Related

  1. max(Collection values)
  2. max(final Collection values)
  3. max(final Collection collection)
  4. maxLength(Collection strings)
  5. maxOr(Collection values, T defaultVal)
  6. partitionFixed(int maxNumChunks, Collection coll)
  7. toString(Collection collection, int maxLen)
  8. toString(Collection entries, int max)
  9. transfer(Collection source, Collection dest, int maxElems)