Find the max of 3 numbers in Java with different data types

前端 未结 12 1890
情深已故
情深已故 2020-12-03 00:33

Say I have the following three constants:

final static int MY_INT1 = 25;
final static int MY_INT2 = -10;
final static double MY_DOUBLE1 = 15.5;
相关标签:
12条回答
  • 2020-12-03 01:29

    you can use this:

     Collections.max(Arrays.asList(1,2,3,4));
    

    or create a function

    public static int max(Integer... vals) {
        return Collections.max(Arrays.asList(vals)); 
    }
    
    0 讨论(0)
  • 2020-12-03 01:29
    int first = 3;  
    int mid = 4; 
    int last = 6;
    
    //checks for the largest number using the Math.max(a,b) method
    //for the second argument (b) you just use the same method to check which  //value is greater between the second and the third
    int largest = Math.max(first, Math.max(last, mid));
    
    0 讨论(0)
  • 2020-12-03 01:32

    Math.max only takes two arguments, no more and no less.

    Another different solution to the already posted answers would be using DoubleStream.of:

    double max = DoubleStream.of(firstValue, secondValue, thirdValue)
                             .max()
                             .getAsDouble();
    
    0 讨论(0)
  • 2020-12-03 01:35

    If possible, use NumberUtils in Apache Commons Lang - plenty of great utilities there.

    https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/math/NumberUtils.html#max(int[])

    NumberUtils.max(int[])
    
    0 讨论(0)
  • 2020-12-03 01:35

    Simple way without methods

    int x = 1, y = 2, z = 3;
    
    int biggest = x;
    if (y > biggest) {
        biggest = y;
    }
    if (z > biggest) {
        biggest = z;
    }
    System.out.println(biggest);
    //    System.out.println(Math.max(Math.max(x,y),z));
    
    0 讨论(0)
  • 2020-12-03 01:37

    Like mentioned before, Math.max() only takes two arguments. It's not exactly compatible with your current syntax but you could try Collections.max().

    If you don't like that you can always create your own method for it...

    public class test {
        final static int MY_INT1 = 25;
        final static int MY_INT2 = -10;
        final static double MY_DOUBLE1 = 15.5;
    
        public static void main(String args[]) {
            double maxOfNums = multiMax(MY_INT1, MY_INT2, MY_DOUBLE1);
        }
    
        public static Object multiMax(Object... values) {
            Object returnValue = null;
            for (Object value : values)
                returnValue = (returnValue != null) ? ((((value instanceof Integer) ? (Integer) value
                        : (value instanceof Double) ? (Double) value
                                : (Float) value) > ((returnValue instanceof Integer) ? (Integer) returnValue
                        : (returnValue instanceof Double) ? (Double) returnValue
                                : (Float) returnValue)) ? value : returnValue)
                        : value;
            return returnValue;
        }
    }
    

    This will take any number of mixed numeric arguments (Integer, Double and Float) but the return value is an Object so you would have to cast it to Integer, Double or Float.

    It might also be throwing an error since there is no such thing as "MY_DOUBLE2".

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