Finding the Min and Max element
Let's look at how we can find the min and max elements in a Collection.
Finding the minimum element in a Collection
The min(Collection c)
method can be used to find the minimum element in a Collection. The elements present in the Collection must implement the Comparable interface. If the elements do not implement the Comparable interface, we can use another overloaded method, min(Collection c, Comparator comp)
. This method takes a Comparator as an argument that is used to compare the elements. This method iterates over the entire collection; hence it requires time proportional to the size of the collection.
Get hands-on with 1400+ tech skills courses.