Java Collections max() MethodThe max() is a Java Collections class method which returns the maximum value for the given inputs. All elements in the collection must implement the Comparable interface. There is two different types of Java max() method which can be differentiated depending on its parameter. These are:
Java Collections max(coll) MethodThe max() method of Java Collections class is used to get the maximum element of the given collection, according to the natural ordering of its elements. Java Collections max(coll, comp) MethodThe max() method of Java Collections class is used to get the maximum element of the given collection, according to the order induced by the specified comparator. SyntaxFollowing is the declaration of max() method: Parameter
Returns
ExceptionsThe max() method throws the following exceptions- ClassCastException- It throws this exception if the collection contains elements that are not mutually comparable (for example, strings and integers). NoSuchElementException- It throws this exception if the collection is empty. Compatibility VersionJava 1.4 and above Example 1Test it NowOutput: Max val: S Example 2Test it NowOutput: Maximum element is: 250 Example 3Test it NowOutput: Output: 101 Example 4Test it NowOutput: Maximum Element is: XML Maximum Value is: -10 Next TopicJava Collections Class |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India