Finding the max/min value in an array of primitives using Java

前端 未结 15 1809
遥遥无期
遥遥无期 2020-11-22 05:09

It\'s trivial to write a function to determine the min/max value in an array, such as:

/**
 * 
 * @param chars
 * @return the max value in the array of chars         


        
相关标签:
15条回答
  • 2020-11-22 05:15

    Here is a solution to get the max value in about 99% of runs (change the 0.01 to get a better result):

    public static double getMax(double[] vals){
        final double[] max = {Double.NEGATIVE_INFINITY};
    
        IntStream.of(new Random().ints((int) Math.ceil(Math.log(0.01) / Math.log(1.0 - (1.0/vals.length))),0,vals.length).toArray())
                .forEach(r -> max[0] = (max[0] < vals[r])? vals[r]: max[0]);
    
        return max[0];
    }
    

    (Not completely serious)

    0 讨论(0)
  • 2020-11-22 05:16

    You could easily do it with an IntStream and the max() method.

    Example

    public static int maxValue(final int[] intArray) {
      return IntStream.range(0, intArray.length).map(i -> intArray[i]).max().getAsInt();
    }
    

    Explanation

    1. range(0, intArray.length) - To get a stream with as many elements as present in the intArray.

    2. map(i -> intArray[i]) - Map every element of the stream to an actual element of the intArray.

    3. max() - Get the maximum element of this stream as OptionalInt.

    4. getAsInt() - Unwrap the OptionalInt. (You could also use here: orElse(0), just in case the OptionalInt is empty.)

    0 讨论(0)
  • 2020-11-22 05:17

    The Google Guava library has min and max methods in its Chars, Ints, Longs, etc. classes.

    So you can simply use:

    Chars.min(myarray)
    

    No conversions are required and presumably it's efficiently implemented.

    0 讨论(0)
  • 2020-11-22 05:17

    Here's a utility class providing min/max methods for primitive types: Primitives.java

    int [] numbers= {10,1,8,7,6,5,2};
        int a=Integer.MAX_VALUE;
        for(int c:numbers) {
            a=c<a?c:a;
            }
            
        System.out.println("Lowest value is"+a);
    
    0 讨论(0)
  • 2020-11-22 05:20

    I have a little helper class in all of my applications with methods like:

    public static double arrayMax(double[] arr) {
        double max = Double.NEGATIVE_INFINITY;
    
        for(double cur: arr)
            max = Math.max(max, cur);
    
        return max;
    }
    
    0 讨论(0)
  • 2020-11-22 05:21

    Using Commons Lang (to convert) + Collections (to min/max)

    import java.util.Arrays;
    import java.util.Collections;
    
    import org.apache.commons.lang.ArrayUtils;
    
    public class MinMaxValue {
    
        public static void main(String[] args) {
            char[] a = {'3', '5', '1', '4', '2'};
    
            List b = Arrays.asList(ArrayUtils.toObject(a));
    
            System.out.println(Collections.min(b));
            System.out.println(Collections.max(b));
       }
    }
    

    Note that Arrays.asList() wraps the underlying array, so it should not be too memory intensive and it should not perform a copy on the elements of the array.

    0 讨论(0)
提交回复
热议问题