Comparable Max Function
Developer's Cave April 22nd. 2010, 11:01am
Here's a helpful method for returning the maximum of two Comparable objects.
Do the Java APIs provide this somewhere?
/** * Return the <i>maximum</i> of the two arguments. If the arguments * are <i>equal</i> (as defined by the Comparable contract), the * <i>left-hand-side</i> argument is returned. */ public static <T extends Comparable> T max(T lhs, T rhs) { return rhs.compareTo(lhs) > 0 ? rhs : lhs; }
Leave a Reply
You must be logged in to post a comment.













