Java Program to Return the Largest Element in a List - GeeksforGeeks

04 Jan, 2021
Given a List, find the largest element in it. There are multiple approaches to tackle this problem like 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 ForEach Loop
Create List object and store multiple elements in it.Create one variable and initialize it with Integer.MAX_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.
Create List object and store multiple elements in it.
Create one variable and initialize it with Integer.MAX_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 Listimport 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 0        int maxValue = 0;          // Check maximum element using for loop        for (Integer integer : arrayList) {            if (integer > maxValue)                maxValue = integer;        }        System.out.println("The maximum value is "                           + maxValue);    }}
The maximum value is 1540
Approach 2: Using Iterators
Create List object and store multiple elements in it.Create one variable and initialize it with Integer.MAX_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.
Create List object and store multiple elements in it.
Create one variable and initialize it with Integer.MAX_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 Listimport 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 0        Integer maxValue = 0;          // 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);    }}
The maximum value is  1540
Approach 3: Using Indexing
Create List object and store multiple elements in it.Create one variable and initialize it with Integer.MAX_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.
Create List object and store multiple elements in it.
Create one variable and initialize it with Integer.MAX_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 Listimport 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);    }}
The maximum value is 1540
Approach 4: Using JDK 8
Java
// create maxValue variable and initialize with 0import 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);    }}
The maximum value is 1540
java-list
Picked
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Functional Interfaces in Java
Stream In Java
Constructors in Java
Different ways of Reading a text file in Java
Exceptions in Java
Convert a String to Character array in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?