public static Optional < Number > max ( List < Object > args ) { if ( args == null || args . size ( ) == 0 ) { return Optional . empty ( ) ; } Integer maxInt = Integer . MIN_VALUE ; Double maxDouble = - ( Double . MAX_VALUE ) ; Long maxLong = Long . MIN_VALUE ; boolean found = false ; for ( Object arg : args ) { if ( arg instanceof Integer ) { maxInt = java . lang . Math . max ( maxInt , ( Integer ) arg ) ; found = true ; } else if ( arg instanceof Double ) { maxDouble = java . lang . Math . max ( maxDouble , ( Double ) arg ) ; found = true ; } else if ( arg instanceof Long ) { maxLong = java . lang . Math . max ( maxLong , ( Long ) arg ) ; found = true ; } else if ( arg instanceof String ) { Optional < ? > optional = Objects . toNumber ( arg ) ; if ( optional . isPresent ( ) ) { arg = optional . get ( ) ; if ( arg instanceof Integer ) { maxInt = java . lang . Math . max ( maxInt , ( Integer ) arg ) ; found = true ; } else if ( arg instanceof Double ) { maxDouble = java . lang . Math . max ( maxDouble , ( Double ) arg ) ; found = true ; } else if ( arg instanceof Long ) { maxLong = java . lang . Math . max ( maxLong , ( Long ) arg ) ; found = true ; } } } } if ( ! found ) { return Optional . empty ( ) ; } // explicit getter method calls to avoid runtime autoboxing // autoBoxing will cause it to return the different type // check MathTest#testAutoBoxingIssue for example if ( maxInt . longValue ( ) >= maxDouble . longValue ( ) && maxInt . longValue ( ) >= maxLong ) { return Optional . < Number > of ( maxInt ) ; } else if ( maxLong >= maxDouble . longValue ( ) ) { return Optional . < Number > of ( maxLong ) ; } else { return Optional . < Number > of ( maxDouble ) ; } }
Given a list of objects returns the max value in its appropriate type also interprets String as Number and returns appropriately