public static < T extends Comparable < T > > Pair < T > minMax ( Iterable < T > items ) { Iterator < T > iterator = items . iterator ( ) ; if ( ! iterator . hasNext ( ) ) { return null ; } T min = iterator . next ( ) ; T max = min ; while ( iterator . hasNext ( ) ) { T item = iterator . next ( ) ; if ( item . compareTo ( min ) < 0 ) { min = item ; } if ( item . compareTo ( max ) > 0 ) { max = item ; } } return new Pair < T > ( min , max ) ; }
Identifies the minimum and maximum elements from an iterable according to the natural ordering of the elements .