java

How to find the largest and smallest element in a list in Java

To find the largest and smallest element in a list in Java it is not necessary to program a loop and compare all elements individually.

In order to to find the largest and smallest element in a list, we should first sort it, so the utility class java.util.Collections help us here.
 

Find the minimum element in a list

The class Collections contains two min() methods:

  • min(Collection<? extends T> coll): Finds the smallest object of the list. Either according to the so-called “natural order”. If the type of T has implemented the interface Comparable, its compareTo() method specifies the order.
  • min(Collection<? extends T> coll, Comparator<? super T> comp): Alternatively, you can pass your own comparator, which decides whether an element is smaller or larger than another.

 

 

Example : Collections.min()
List names = Arrays.asList("Thomas", "Emily", "alex", "Bob");
Collections.min(names);  // Bob

The result of the min() method seems a bit strange, since according to the alphabet “alex” should be the smallest element. The “natural order” for strings is the order of the Unicode codes, according to which the uppercase letters come before the lowercase letters. That’s why in this case “Bob” is the smallest element.

The second min() method to which a comparator can be passed is intended for such cases, in this case as a lambda expression:

List names = Arrays.asList("Thomas", "Emily", "alex", "Bob");
Collections.min(names, (a, b) -> a.toLowerCase().compareTo(b.toLowerCase())); 
// alex

The Comparator function now converts the strings to lowercase before the comparison, so that the correct order is used.
java-mcq-multiple-choice-questions-and-answersJava MCQ – Multiple Choice Questions and Answers – OOPsThis collection of Java Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Java OOPs”.   1. Which of the…Read More

Find the maximum element in a list

Similar to min(), the Collections class also has two max() methods:

  • max(Collection<? extends T> coll): Finds the smallest object of the list. Either according to the so-called “natural order”. If the type of T has implemented the interface Comparable, its compareTo() method specifies the order.
  • max(Collection<? extends T> coll, Comparator<? super T> comp): Alternatively, you can pass your own comparator, which decides whether an element is smaller or larger than another.

 

 

Example : Collections.max()
List names = Arrays.asList("Thomas", "Emily", "alex", "Bob");
Collections.min(names);  // alex

The result of the max() method is wrong, because according to the alphabet “Thomas” should be the largest element. The “natural order” for strings is the order of the Unicode codes, according to which the uppercase letters come before the lowercase letters. That’s why in this case “alex” is the largest element.

The second max() method to which a comparator can be passed is intended for such cases, in this case as a lambda expression:

List names = Arrays.asList("Thomas", "Emily", "alex", "Bob");
Collections.max(names, (a, b) -> a.toLowerCase().compareTo(b.toLowerCase())); 
// Thomas

The Comparator function now converts the strings to lowercase before the comparison, so that the correct order is used.
Difference between == and .equalsDifference between == and .equals()In short: .equals() is used to compare objects, and the equal-to operator (==) is used to compare references and simple types such as int and…Read More

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *