Java Program to Return the Largest Element in a List

We can use an array loop to return back the largest element from a list. Primarily this method, known as the comparison model. The maximum number present here in a certain list will be compared with the all elements present in that particular list. The process considers “n” as a number of inputs which will store as data value in an array. After that the program will display the largest element on the output console after refining the loop.

In this article today; we will help you to understand and write some Java code by which you can find the Largest Element from an array List.

How to Pick a Largest Number From an Array Using Java?

We can find a largest number by sorting an array. To define a void ArrayList and add all elements of array to it. Passing the ArrayList to Collections.max() and the entire process will take a run.

  • For this operation, you can declare a set of input as a form of array at the beginning. This creates a base to execute a logic. The algorithm uses this loop to find out the particular result (largest number of that loop).


Let's take an example.

arr[]= {1,10,4,15,9,85,63,108}


Output: 108
  • To find a largest number from an array, two types of functions are generally utilized −

    • Max () – Use to find the max function from the list

    • for Loop - Use to perform iteration for every element.

  • First, you should declare an array and then initialize it. For the iteration we need two loops and then to compare the elements to get the largest number, the data need to be swapped in a descending order.

Algorithm to Find out the Largest Element in a List

Here is the general algorithm for to find out the largest element in a list by using Java −

  • Step 1 − Start

  • Step 2 − Initialize arr[]

  • Step 3 − max=arr[0]

  • Step 4 − i=0;i<arr.length;i++

  • Step 4 − if (arr[i]>max)max=arr[i]

  • Step 5(1) − Print

  • Step 5(2) − Print MAX

  • Step 6 − Terminate


There are two methods to perform the operation. In below syntax these two methods are described.

  • coll means; the total collection from which the maximum element will be filtered out.

  • comp means; the comparator by which the operation can be done.

public static <T extends an Object & make it Comparable<? super T>> T max(Collection of data <? extends T> coll)  
public static <T> T max(Collection of the data <? extends T> coll, Comparator<? super T> comparator)

Below approaches are useful for finding out the largest value in an array list −

  • Approach 1 − The iteration method

  • Approach 2 − Int method by Java 8 stream

  • Approach 3 − max() method

  • Approach 4 − Using ForEach Loop

  • Approach 5 − Using Library Function

By Using the Iteration Method

In this method, the time complexity is 0 based on the size of the given data set. And the auxiliary space is not required.

  • Recursive way to get max value.

  • Basic Condition of the method : if , (m==1) value return arr[0]

  • Else, get return the value of: maximum (arr[n-1], getmax(arr[], n-1))


import java.util.*;  
public class CollectionsofmaxfileARRDD {  
   public static void main (String[] args) {  
      List<Integer> list = Arrays.asList(2010, 1010, 1001, 1400, 2501);  
      Integer max = Collections.max(list, Collections.reverseOrder());  
      System.out.println("Output from the particular string: "+max);  


Output from the particular string: 1001

By Using Int Method in Java 8 Stream

In this method the time complexity is totally 0 and the auxiliary space has no extra space needed because it is constant.


import java.util.Arrays;
public class arbrdd {
   public static void main (String[] args){
      int arr[] = {07, 16, 10, 2001, 1997};
      int max =;
      System.out.println("Largest array is found from the array list" +max);


Largest array is found from the array list2001

By Using the max() Method

By using the max() method, here we will build a Java code by using the below process −

  • Declare variable with a max

  • Initialize with the first element of an array

  • Run the loop

  • array[a]>maximum, set max = array[a]

  • Print the Output


import java.util.*;
public class arbrdd{
   public static void main(String[] args){
      int arr[] = {10, 07, 16, 2001,1997};
      List<Integer> list = new ArrayList<>();
      for(int a=0;a<arr.length;a++){
      System.out.println("Largest array present in the particular array list is " +Collections.max(list));


Largest array present in the particular array list is 2001

By Using a ForEach Loop

By using the ForEach Loop, here we will build a Java code by using the below process −

  • Call recursive say get max

  • Basic Condition for the operation : if,(a==1) return the array[0]

  • Or else, return max(array[a-1], getmax(array, a-1))


import java.util.Arrays;
import java.util.List;
public class maxarrayval {
   public static void main(String[] args){
      List<Integer> arrayList
      = Arrays.asList(10, 07, 16, 2001, 1997, 10052022);
      int maxValue0710 = Integer.MIN_VALUE;
      for (Integer integer : arrayList) {
         if (integer > maxValue0710)
         maxValue0710 = integer;
      System.out.println("The maximum value present in the array is " + maxValue0710);


The maximum value present in the array is 10052022

By Using Library Function

By using the library functions, here we will build a Java code by using the below process −

  • Maximum(arr,0,end)

  • Read 2nd last element from that array list

  • Find the larger element between 2nd last and last one from array data

  • Max value recursive iteration

  • End


import java .io.*;
import java.util.*;
public class ARBRDD{
   static int largest(int []arr,int n){
      return arr[n - 1];
   static public void main (String[] args){
      int []arr = {07, 10, 2001,1997, 10052022};
      int n = arr.length;
      System.out.println(largest(arr, n));




In this article; today we learnt how to get the Largest Element in return from an array List using Java.

With the possible conditions and the programs written using the logic mentioned here how we can use an array loop and based on all possible conditions and process of some codes written here to satisfy every theory as well.

Updated on: 31-Mar-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started