find maximum of list of numbers in java

Share post:

find maximum of list of numbers in java

Quick Answer:

find the maximum of a list of numbers in java using the best way, we can do it using the Collections.max(arraylist) but this is much more elegant

Used treeset here,

import java.util.Collections;
import java.util.TreeSet;

public class FindMax {
public static void main(String[] args) {
TreeSet<Integer> l = new TreeSet<>();
l.add(-1);
l.add(2);
l.add(3);
l.add(400);
l.add(-5);
System.out.println(l.first()+" "+l.last()); // 5
System.out.println(Collections.max(l)); // 5
System.out.println(Collections.min(l)); // 1
}
}

Smashplus

Insights

Share post: