Close

Java Collections - Collections.max() Examples

Java Collections Java Java API 


Class:

java.util.Collections

java.lang.Objectjava.lang.Objectjava.util.Collectionsjava.util.CollectionsLogicBig

Methods:

public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)

Returns the maximum element of the given collection, according to the natural ordering of its elements.



public static <T> T max(Collection<? extends T> coll,
                        Comparator<? super T> comp)

Returns the maximum element of the given collection, according to the order induced by the specified comparator.


Examples


package com.logicbig.example.collections;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class MaxExample {

public static void main(String... args) {
List<Integer> list = Arrays.asList(20, 10, 100, 140);
Integer max = Collections.max(list);
System.out.println(max);
}
}

Output

140




package com.logicbig.example.collections;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class MaxExample2 {

public static void main(String... args) {
List<Integer> list = Arrays.asList(20, 10, 100, 140);
Integer max = Collections.max(list, Collections.reverseOrder());
System.out.println(max);
}
}

Output

10




See Also