@ GwtIncompatible ( "incompatible method" ) @ SafeVarargs public static < T extends Comparable < ? super T > > T median ( final T ... items ) { Validate . notEmpty ( items ) ; Validate . noNullElements ( items ) ; final TreeSet < T > sort = new TreeSet <> ( ) ; Collections . addAll ( sort , items ) ; @ SuppressWarnings ( "unchecked" ) //we know all items added were T instances final T result = ( T ) sort . toArray ( ) [ ( sort . size ( ) - 1 ) / 2 ] ; return result ; }
Find the best guess middle value among comparables . If there is an even number of total values the lower of the two middle values will be returned .