# Min and Max in a List in Java

Given an unsorted list of integers, find maximum and minimum values in it.

```Input : list = [10, 4, 3, 2, 1, 20]
Output : max = 20, min = 1

Input : list = [10, 400, 3, 2, 1, -1]
Output : max = 400, min = -1
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Sorting

This is least efficient approach but will get the work done. The idea is to sort the list in natural order, then the first or last element would be the minimum and maximum element respectively. Below’s implementation in Java.

 `// This java program find minimum and maximum value ` `// of an unsorted list of Integer by using Collection ` `import` `java.util.ArrayList; ` `import` `java.util.Collections; ` `import` `java.util.List; ` ` `  `public` `class` `GFG { ` ` `  `    ``// function to find minimum value in an unsorted ` `    ``// list in Java using Collection ` `    ``public` `static` `Integer findMin(List list) ` `    ``{ ` ` `  `        ``// check list is empty or not ` `        ``if` `(list == ``null` `|| list.size() == ``0``) { ` `            ``return` `Integer.MAX_VALUE; ` `        ``} ` ` `  `        ``// create a new list to avoid modification  ` `        ``// in the original list ` `        ``List sortedlist = ``new` `ArrayList<>(list); ` ` `  `        ``// sort list in natural order ` `        ``Collections.sort(sortedlist); ` ` `  `        ``// first element in the sorted list ` `        ``// would be minimum ` `        ``return` `sortedlist.get(``0``); ` `    ``} ` ` `  `    ``// function return maximum value in an unsorted ` `    ``// list in Java using Collection ` `    ``public` `static` `Integer findMax(List list) ` `    ``{ ` ` `  `        ``// check list is empty or not ` `        ``if` `(list == ``null` `|| list.size() == ``0``) { ` `            ``return` `Integer.MIN_VALUE; ` `        ``} ` ` `  `        ``// create a new list to avoid modification ` `        ``// in the original list ` `        ``List sortedlist = ``new` `ArrayList<>(list); ` ` `  `        ``// sort list in natural order ` `        ``Collections.sort(sortedlist); ` ` `  `        ``// last element in the sorted list would be maximum ` `        ``return` `sortedlist.get(sortedlist.size() - ``1``); ` `    ``} ` ` `  `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` ` `  `        ``// create an ArrayList Object list ` `        ``List list = ``new` `ArrayList<>(); ` ` `  `        ``// add element in ArrayList object list ` `        ``list.add(``44``); ` `        ``list.add(``11``); ` `        ``list.add(``22``); ` `        ``list.add(``33``); ` ` `  `        ``// print min amd max value of ArrayList ` `        ``System.out.println(``"Min: "` `+ findMin(list)); ` `        ``System.out.println(``"Max: "` `+ findMax(list)); ` `    ``} ` `} `

Output:

```Min: 11
Max: 44```

Collections.max()

Collections.min() method return the minimum element in the specified collection and Collections.max () returns the maximum element in the specified collection, according to the natural ordering of its elements.

 `// This java program find minimum and maximum value ` `// of an unsorted list of Integer by using Collection ` `import` `java.util.ArrayList; ` `import` `java.util.Collections; ` `import` `java.util.List; ` ` `  `public` `class` `GFG { ` ` `  `    ``// function to find minimum value in an unsorted ` `    ``// list in Java using Collection ` `    ``public` `static` `Integer findMin(List list) ` `    ``{ ` ` `  `        ``// check list is empty or not ` `        ``if` `(list == ``null` `|| list.size() == ``0``) { ` `            ``return` `Integer.MAX_VALUE; ` `        ``} ` ` `  `        ``// return minimum value of the ArrayList ` `        ``return` `Collections.min(list); ` `    ``} ` ` `  `    ``// function return maximum value in an unsorted ` `    ``//  list in Java using Collection ` `    ``public` `static` `Integer findMax(List list) ` `    ``{ ` ` `  `        ``// check list is empty or not ` `        ``if` `(list == ``null` `|| list.size() == ``0``) { ` `            ``return` `Integer.MIN_VALUE; ` `        ``} ` ` `  `        ``// return maximum value of the ArrayList ` `        ``return` `Collections.max(list); ` `    ``} ` ` `  `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` ` `  `        ``// create an ArrayList Object list ` `        ``List list = ``new` `ArrayList<>(); ` ` `  `        ``// add element in ArrayList object list ` `        ``list.add(``44``); ` `        ``list.add(``11``); ` `        ``list.add(``22``); ` `        ``list.add(``33``); ` ` `  `        ``// print min amd max value of ArrayList ` `        ``System.out.println(``"Min: "` `+ findMin(list)); ` `        ``System.out.println(``"Max: "` `+ findMax(list)); ` `    ``} ` `} `

Output:

```Min: 11
Max: 44```

Naive

Here is naive way of finding find minimum and maximum value in an unsorted list where we check against all values present in the list and maintain minimum & maximum value found so far.

 `// This java program find minimum and maximum value ` `// of an unsorted list of Integer ` `import` `java.util.ArrayList; ` `import` `java.util.List; ` ` `  `public` `class` `GFG { ` ` `  `    ``// Naive function to find minimum value in an ` `    ``// unsorted list in Java ` `    ``public` `static` `Integer findMin(List list) ` `    ``{ ` `        ``// initialize min to some maximum value ` `        ``Integer min = Integer.MAX_VALUE; ` ` `  `        ``// loop through every element in the list and ` `        ``// compare min found so far with current value ` `        ``for` `(Integer i : list) { ` ` `  `            ``// update min if found to be more than  ` `            ``// the current element ` `            ``if` `(min > i) { ` `                ``min = i; ` `            ``} ` `        ``} ` ` `  `        ``return` `min; ` `    ``} ` ` `  `    ``// This function return maximum value in an ` `    ``// unsorted list in Java ` `    ``public` `static` `Integer findMax(List list) ` `    ``{ ` `        ``// initialize max variable to minimum value ` `        ``Integer max = Integer.MIN_VALUE; ` ` `  `        ``// loop for compare to current max value ` `        ``// with all list element and find maximum value ` `        ``for` `(Integer i : list) { ` ` `  `            ``// update max if found to be less than  ` `            ``// the current element ` `            ``if` `(max < i) { ` `                ``max = i; ` `            ``} ` `        ``} ` ` `  `        ``return` `max; ` `    ``} ` ` `  `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``// create an ArrayList Object list ` `        ``List list = ``new` `ArrayList<>(); ` `        ``// add element in ArrayList object list ` `        ``list.add(``44``); ` `        ``list.add(``11``); ` `        ``list.add(``22``); ` `        ``list.add(``33``); ` ` `  `        ``// print min amd max value of ArrayList ` `        ``System.out.println(``"Min: "` `+ findMin(list)); ` `        ``System.out.println(``"Max: "` `+ findMax(list)); ` `    ``} ` `} `

Output:

```Min: 11
Max: 44```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.