156

There is an ArrayList which stores integer values. I need to find the maximum value in this list. E.g. suppose the arrayList stored values are : 10, 20, 30, 40, 50 and the max value would be 50.

What is the efficient way to find the maximum value?

@Edit : I just found one solution for which I am not very sure

ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(100); /* add(200), add(250) add(350) add(150) add(450)*/

Integer i = Collections.max(arrayList)

and this returns the highest value.

Another way to compare the each value e.g. selection sort or binary sort algorithm  

Line
  • 1,529
  • 3
  • 18
  • 42
user1010399
  • 2,258
  • 5
  • 30
  • 42
  • 2
    Have you attempted to find the value? Where did you get stuck? Is your own solution perhaps too inefficient? – Anthony Pegram Nov 29 '11 at 01:42
  • 1
    If it's something you do a lot Java will compile it to assembly so unless you do something silly your code will be quite efficient with just a simple iterator. – Bill K Nov 29 '11 at 01:53
  • @AnthonyPegram : i mean which sort algorithm or is there any method in java? BTW check the gotomanners's answer. – user1010399 Nov 29 '11 at 02:47
  • For an array that may contain `null` values: http://stackoverflow.com/questions/369383/what-is-the-best-way-to-get-min-and-max-value-from-a-list-of-comparables-that-ma/30596059#30596059 – Ciro Santilli OurBigBook.com Jun 02 '15 at 12:27
  • 1
    Java 8 : https://stackoverflow.com/a/52270228/1216775 – akhil_mittal Oct 08 '18 at 05:20

16 Answers16

338

You can use the Collections API to achieve what you want easily - read efficiently - enough Javadoc for Collections.max

Collections.max(arrayList);

Returns the maximum element of the given collection, according to the natural ordering of its elements. All elements in the collection must implement the Comparable interface.

nanosoft
  • 2,913
  • 4
  • 41
  • 61
gotomanners
  • 7,808
  • 1
  • 24
  • 39
  • 1
    Yes, iterating through the list is `O(n log(n))` but if "There is no particularly efficient way", what do you propose that is a better solution besides checking them all? – gotomanners Nov 29 '11 at 09:53
  • Naively iterating is faster (checked, comparator was fetching scores from a Map) than either sorting and getting first element or using max. Both sort+take first and max used a lambda. – majTheHero Mar 27 '19 at 12:24
33

This question is almost a year old but I have found that if you make a custom comparator for objects you can use Collections.max for an array list of objects.

import java.util.Comparator;

public class compPopulation implements Comparator<Country> {
    public int compare(Country a, Country b) {
        if (a.getPopulation() > b.getPopulation())
            return -1; // highest value first
        if (a.getPopulation() == b.Population())
            return 0;
        return 1;
    }
}
ArrayList<Country> X = new ArrayList<Country>();
// create some country objects and put in the list
Country ZZ = Collections.max(X, new compPopulation());
Tom
  • 16,842
  • 17
  • 45
  • 54
Robert Quinn
  • 579
  • 5
  • 10
  • do you need a custom comparator for Calendar types? – tatmanblue Sep 26 '17 at 14:42
  • Your code return smallest value in the list, if (a.getPopulation() > b.getPopulation()) return -1; The above has to change to, if (a.getPopulation() < b.getPopulation()) return -1; // highest value first – Chandrakanth M Mar 07 '18 at 07:44
  • This can be done using a lambda as well: maxElement = Collections.max(collection, (el1, el2)-> el1 - el2); – majTheHero Mar 27 '19 at 12:18
28
public int getMax(ArrayList list){
    int max = Integer.MIN_VALUE;
    for(int i=0; i<list.size(); i++){
        if(list.get(i) > max){
            max = list.get(i);
        }
    }
    return max;
}

From my understanding, this is basically what Collections.max() does, though they use a comparator since lists are generic.

John
  • 289
  • 3
  • 2
15

We can simply use Collections.max() and Collections.min() method.

public class MaxList {
    public static void main(String[] args) {
        List l = new ArrayList();
        l.add(1);
        l.add(2);
        l.add(3);
        l.add(4);
        l.add(5);
        System.out.println(Collections.max(l)); // 5
        System.out.println(Collections.min(l)); // 1
    }
}
Bhavin Shah
  • 1,287
  • 13
  • 14
10

Comparator.comparing

In Java 8, Collections have been enhanced by using lambda. So finding max and min can be accomplished as follows, using Comparator.comparing:

Code:

List<Integer> ints = Stream.of(12, 72, 54, 83, 51).collect(Collectors.toList());
System.out.println("the list: ");
ints.forEach((i) -> {
    System.out.print(i + " ");
});
System.out.println("");
Integer minNumber = ints.stream()
        .min(Comparator.comparing(i -> i)).get();
Integer maxNumber = ints.stream()
        .max(Comparator.comparing(i -> i)).get();

System.out.println("Min number is " + minNumber);
System.out.println("Max number is " + maxNumber);

Output:

 the list: 12 72 54 83 51  
 Min number is 12 
 Max number is 83
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
9

Integer class implements Comparable.So we can easily get the max or min value of the Integer list.

public int maxOfNumList() {
    List<Integer> numList = new ArrayList<>();
    numList.add(1);
    numList.add(10);
    return Collections.max(numList);
}

If a class does not implements Comparable and we have to find max and min value then we have to write our own Comparator.

List<MyObject> objList = new ArrayList<MyObject>();
objList.add(object1);
objList.add(object2);
objList.add(object3);
MyObject maxObject = Collections.max(objList, new Comparator<MyObject>() {
    @Override
    public int compare(MyObject o1, MyObject o2) {
        if (o1.getValue() == o2.getValue()) {
            return 0;
        } else if (o1.getValue() > o2.getValue()) {
            return -1;
        } else if (o1.getValue() < o2.getValue()) {
            return 1;
        }
        return 0;
    }
});
bluish
  • 26,356
  • 27
  • 122
  • 180
Avijit Karmakar
  • 8,890
  • 6
  • 44
  • 59
5

There is no particularly efficient way to find the maximum value in an unsorted list -- you just need to check them all and return the highest value.

Brendan Long
  • 53,280
  • 21
  • 146
  • 188
4

Here are three more ways to find the maximum value in a list, using streams:

List<Integer> nums = Arrays.asList(-1, 2, 1, 7, 3);
Optional<Integer> max1 = nums.stream().reduce(Integer::max);
Optional<Integer> max2 = nums.stream().max(Comparator.naturalOrder());
OptionalInt max3 = nums.stream().mapToInt(p->p).max();
System.out.println("max1: " + max1.get() + ", max2: " 
   + max2.get() + ", max3: " + max3.getAsInt());

All of these methods, just like Collections.max, iterate over the entire collection, hence they require time proportional to the size of the collection.

bluish
  • 26,356
  • 27
  • 122
  • 180
Ida Bucić
  • 939
  • 8
  • 10
4

Java 8

As integers are comparable we can use the following one liner in:

List<Integer> ints = Stream.of(22,44,11,66,33,55).collect(Collectors.toList());
Integer max = ints.stream().mapToInt(i->i).max().orElseThrow(NoSuchElementException::new); //66
Integer min = ints.stream().mapToInt(i->i).min().orElseThrow(NoSuchElementException::new); //11

Another point to note is we cannot use Funtion.identity() in place of i->i as mapToInt expects ToIntFunction which is a completely different interface and is not related to Function. Moreover this interface has only one method applyAsInt and no identity() method.

akhil_mittal
  • 23,309
  • 7
  • 96
  • 95
4

In Java8

arrayList.stream()
         .reduce(Integer::max)
         .get()
lasclocker
  • 311
  • 3
  • 8
1

Here is the fucntion

public int getIndexOfMax(ArrayList<Integer> arr){
    int MaxVal = arr.get(0); // take first as MaxVal
    int indexOfMax = -1; //returns -1 if all elements are equal
    for (int i = 0; i < arr.size(); i++) {
        //if current is less then MaxVal
        if(arr.get(i) < MaxVal ){
            MaxVal = arr.get(i); // put it in MaxVal
            indexOfMax = i; // put index of current Max
        }
    }
    return indexOfMax;  
}
SAM
  • 11
  • 2
1
package in.co.largestinarraylist;

import java.util.ArrayList;
import java.util.Scanner;

public class LargestInArrayList {

    public static void main(String[] args) {

        int n;
        ArrayList<Integer> L = new ArrayList<Integer>();
        int max;
        Scanner in = new Scanner(System.in);
        System.out.println("Enter Size of Array List");
        n = in.nextInt();
        System.out.println("Enter elements in Array List");

        for (int i = 0; i < n; i++) {
            L.add(in.nextInt());
        }

        max = L.get(0);

        for (int i = 0; i < L.size(); i++) {
            if (L.get(i) > max) {
                max = L.get(i);
            }
        }

        System.out.println("Max Element: " + max);
        in.close();
    }
}
Anh Pham
  • 2,108
  • 9
  • 18
  • 29
1

In addition to gotomanners answer, in case anyone else came here looking for a null safe solution to the same problem, this is what I ended up with

Collections.max(arrayList, Comparator.nullsFirst(Comparator.naturalOrder()))
1
model =list.stream().max(Comparator.comparing(Model::yourSortList)).get();
Mehmet Onar
  • 349
  • 2
  • 13
1

They're many ways to find the maximum. But there will be no noticeable difference in performance unless the collection is huge.

List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);

System.out.println(
        integers.stream().max(Integer::compare).get()
);
System.out.println(
        integers.stream().mapToInt(Integer::intValue).max().getAsInt()
);
System.out.println(
        integers.stream().max(Comparator.comparing(i -> i)).get()
);
System.out.println(
        integers.stream().reduce((a, b) -> a > b ? a : b).get()
);
System.out.println(
        integers.stream().reduce(Integer.MIN_VALUE, (a, b) -> a > b ? a : b)
);

The max method expects a Comparator as a parameter.

The reduce method expects a BinaryOperator as a parameter.

Gayan Weerakutti
  • 11,904
  • 2
  • 71
  • 68
-3

depending on the size of your array a multithreaded solution might also speed up things

niklas
  • 2,887
  • 3
  • 38
  • 70