public static < T > T max ( final Collection < ? extends T > c , final int from , final int to , Comparator < ? super T > cmp ) { checkFromToIndex ( from , to , size ( c ) ) ; if ( N . isNullOrEmpty ( c ) || to - from < 1 || from >= c . size ( ) ) { throw new IllegalArgumentException ( "The size of collection can not be null or empty" ) ; } cmp = cmp == null ? NULL_MIN_COMPARATOR : cmp ; T candidate = null ; T e = null ; if ( c instanceof List && c instanceof RandomAccess ) { final List < T > list = ( List < T > ) c ; candidate = list . get ( from ) ; for ( int i = from + 1 ; i < to ; i ++ ) { e = list . get ( i ) ; if ( cmp . compare ( e , candidate ) > 0 ) { candidate = e ; } if ( candidate == null && cmp == NULL_MAX_COMPARATOR ) { return null ; } } } else { final Iterator < ? extends T > it = c . iterator ( ) ; for ( int i = 0 ; i < to ; i ++ ) { if ( i < from ) { it . next ( ) ; continue ; } else if ( i == from ) { candidate = it . next ( ) ; } else { e = it . next ( ) ; if ( cmp . compare ( e , candidate ) > 0 ) { candidate = e ; } } if ( candidate == null && cmp == NULL_MAX_COMPARATOR ) { return null ; } } } return candidate ; }
Returns the maximum element in the collection .