Java Collection Max max(Collection values)

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

Description

max

License

Apache License

Declaration

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

Method Source Code

//package com.java2s;
/*/*from   w ww . ja v  a2s .  c o  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 max(double[] values) {
        int i = 0;
        double max = Double.NaN;
        // find the first actuall number
        for (; !Double.isNaN(values[i]); i++)
            ;
        if (i < values.length) {
            max = values[i];
        }
        for (++i; i < values.length; i++) {
            if (!Double.isNaN(values[i]) && values[i] > max) {
                max = values[i];
            }
        }
        return max;
    }

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

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

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

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

Related

  1. max(Collection collection)
  2. max(Collection list)
  3. max(Collection strs)
  4. max(Collection col)
  5. max(Collection elems)
  6. max(Collection values)
  7. max(final Collection values)
  8. max(final Collection collection)
  9. maxLength(Collection strings)