How to Get Max Element in an ArrayList in Java


In this example we will show the method to get the max element in an ArrayList in Java.

Source Code

package com.beginner.examples;

import java.util.ArrayList;
import java.util.Collections;

public class MaxElementOfArrayList {

  public static void main(String[] args) {

    ArrayList arrays = new ArrayList();
    arrays.add(6);
    arrays.add(1);
    arrays.add(4);
    System.out.println("Max : " + Collections.max(arrays)); // get the max element
 }
}

Output:

Max : 6

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments