public static Optional < Double > avg ( List < Object > args ) { double sum = 0d ; int count = 0 ; for ( Object arg : args ) { Optional < ? extends Number > numberOptional = Objects . toNumber ( arg ) ; if ( numberOptional . isPresent ( ) ) { sum = sum + numberOptional . get ( ) . doubleValue ( ) ; count = count + 1 ; } } return count == 0 ? Optional . < Double > empty ( ) : Optional . of ( sum / count ) ; }
Given a list of numbers returns their avg as double any value in the list that is not a valid number is ignored