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
import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String[] args)
{
List<Integer> arrayList
= Arrays.asList( 5 , 3 , 15 , 234 , 114 , 1540 );
int maxValue = Integer.MIN_VALUE;
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
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class Test {
public static void main(String[] args)
{
List<Integer> arrayList
= Arrays.asList( 5 , 3 , 15 , 234 , 114 , 1540 );
Iterator listIterator = arrayList.iterator();
Integer maxValue = Integer.MIN_VALUE;
while (listIterator.hasNext()) {
Integer integer = (Integer)listIterator.next();
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
import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String[] args)
{
List<Integer> arrayList
= Arrays.asList( 5 , 3 , 15 , 234 , 114 , 1540 );
Integer maxValue = 0 ;
for ( int i = 0 ; i < arrayList.size(); i++) {
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<Integer> arrayList
= Arrays.asList( 5 , 3 , 15 , 234 , 114 , 1540 );
int maxValue = arrayList.stream()
.max(Integer::compareTo)
.get();
System.out.println( "The maximum value is "
+ maxValue);
}
}
|
Output
The maximum value is 1540
Last Updated :
18 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...