Java Collection Min min(Collection values)

Here you can find the source of min(Collection values)

Description

min

License

Apache License

Declaration

public static <T extends Comparable<T>> T min(Collection<T> values) 

Method Source Code

//package com.java2s;
/*//from w ww . j  av a2s  . co  m
 * Copyright 2015 Lutz Fischer <lfischer at staffmail.ed.ac.uk>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Collection;
import java.util.Iterator;

public class Main {
    public double min(double[] values) {
        int i = 0;
        double min = Double.NaN;
        // find the first actuall number
        for (; !Double.isNaN(values[i]); i++)
            ;
        if (i > values.length) {
            min = values[i];
        }
        for (++i; i < values.length; i++) {
            if (!Double.isNaN(values[i]) && values[i] < min) {
                min = values[i];
            }
        }
        return min;
    }

    public int min(int[] values) {
        int i = 0;
        int min = values[0];
        for (; i < values.length; i++) {
            if (values[i] < min) {
                min = values[i];
            }
        }
        return min;
    }

    public <T extends Comparable<T>> T min(T[] values) {
        T min = values[0];
        for (int i = 1; i < values.length; i++) {
            if (values[i].compareTo(min) < 0) {
                min = values[i];
            }
        }
        return min;
    }

    public int min(int v1, int... v2) {
        return Math.min(v1, min(v2));
    }

    public static <T extends Comparable<T>> T min(Collection<T> values) {
        T min = null;
        Iterator<T> it = values.iterator();
        if (it.hasNext()) {
            min = it.next();
            while (it.hasNext()) {
                T n = it.next();
                if (n.compareTo(min) < 0) {
                    min = n;
                }
            }
        }
        return min;
    }
}

Related

  1. min(Collection list)
  2. min(Collection strs)
  3. min(Collection col)
  4. min(Collection elems)
  5. min(Collection values)
  6. min(final Collection collection)
  7. minI(Collection col)
  8. minInt(Collection ints)
  9. minKmerLength(final Collection kmers)