Java Program to Return the Largest Element in a List
Given a List, find the largest element in it. There are multiple approaches to tackle this problem, such as iterating through the List or using various inbuilt functions.
Input : List = [5, 3, 234, 114, 154] Output : 234 Input : List = {10, 20, 4} Output : 20
Approach 1: Using a ForEach Loop
- Create List object and store multiple elements in it.
- Create a variable and initialize it with Integer.MIN_VALUE.
- Start iterating through the List using for each loop and compare each element with the variable.
- If the current element is greater than variable then update the variable.
- At the end of the iteration, print the variable.
Below is the implementation of the above approach:
Java
// Java Program to Return the Largest Element in a List import java.util.Arrays; import java.util.List; public class Test { public static void main(String[] args) { // List input List<Integer> arrayList = Arrays.asList( 5 , 3 , 15 , 234 , 114 , 1540 ); // Create maxValue variable and initialize with // minimum value int maxValue = Integer.MIN_VALUE; // Check maximum element using for loop for (Integer integer : arrayList) { if (integer > maxValue) maxValue = integer; } System.out.println( "The maximum value is " + maxValue); } } |
Output
The maximum value is 1540
Approach 2: Using Iterators
- Create List object and store multiple elements in it.
- Create a variable and initialize it with Integer.MIN_VALUE.
- Start iterating through the List using List iterator and compare each element with the variable.
- If the current element is greater than variable then update the variable.
- At the end of the iteration, print the variable.
Below is the implementation of the above approach:
Java
// Java Program to Return the Largest Element in a List import java.util.Arrays; import java.util.Iterator; import java.util.List; public class Test { public static void main(String[] args) { // List as input List<Integer> arrayList = Arrays.asList( 5 , 3 , 15 , 234 , 114 , 1540 ); // List iterator Iterator listIterator = arrayList.iterator(); // Create maxValue variable and initialize with // minimum value Integer maxValue = Integer.MIN_VALUE; // Iterate list while (listIterator.hasNext()) { Integer integer = (Integer)listIterator.next(); // If value is greater then update maxValue if (integer > maxValue) maxValue = integer; } System.out.println( "The maximum value is " + maxValue); } } |
Output
The maximum value is 1540
Approach 3: Using Indexing
- Create List object and store multiple elements in it.
- Create a variable and initialize it with Integer.MIN_VALUE.
- Start iterating through the List and compare each element with the variable.
- If the current element is greater than variable then update the variable.
- At the end of the iteration, print the variable.
Below is the implementation of the above approach:
Java
// Java Program to Return the Largest Element in a List import java.util.Arrays; import java.util.List; public class Test { public static void main(String[] args) { // List as input List<Integer> arrayList = Arrays.asList( 5 , 3 , 15 , 234 , 114 , 1540 ); // Create maxValue variable and initialize with 0 Integer maxValue = 0 ; // Iterate List using for each loop for ( int i = 0 ; i < arrayList.size(); i++) { // If element is greater the update maxValue if (arrayList.get(i) > maxValue) maxValue = arrayList.get(i); } System.out.println( "The maximum value is " + maxValue); } } |
Output
The maximum value is 1540
Approach 4: Using JDK 8
Java
import java.util.Arrays; import java.util.List; public class Test { public static void main(String[] args) { // List as input List<Integer> arrayList = Arrays.asList( 5 , 3 , 15 , 234 , 114 , 1540 ); // Initialize with inbuilt max function int maxValue = arrayList.stream() .max(Integer::compareTo) .get(); System.out.println( "The maximum value is " + maxValue); } } |
Output
The maximum value is 1540