Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Thursday, November 19, 2020

Min And Max Value From List In Java

1. Overview

In this tutorial, you'll learn what are the different ways to get the minimum and maximum value from unsorted List in java.

List may contains values type of integers or Strings or any custom objects. But let us understand with the simple data set such as list of integer values.


2. Using Sorting

First, sort the list using Collections.sort() in ascending order. Next, take the first value as min and last value as max.

package com.javaprogramto.java8.sorting.list;

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

public class ListSortMinMaxExample {

	public static void main(String[] args) {

		// creating a list to store the integer values
		List<Integer> numbers = new ArrayList<>();

		// adding integer values
		numbers.add(30);
		numbers.add(20);
		numbers.add(50);
		numbers.add(10);
		numbers.add(20);

		// sorting List using collections.sort()
		Collections.sort(numbers);

		// getting the min value
		int min = numbers.get(0);

		// getting max value
		int max = numbers.get(numbers.size() - 1);

		System.out.println("List sorting - min : " + min + ", max : " + max);

	}

}

Output:

List sorting - min : 10, max : 50

3. Using Collections.min() and Collections.max() Methods

Java Collections class has two methods to get the min and max values using Collections.max() and Collections.min() methods.

import java.util.Collections;

public class ListCollectionsMinMaxExample {
	public static void main(String[] args) {

		// creating a list to store the integer values
		List<Integer> numbers = new ArrayList<>();

		// adding integer values
		numbers.add(23);
		numbers.add(45);
		numbers.add(76);
		numbers.add(9);
		numbers.add(10);

		// getting the min value using min() method 
		int min = Collections.min(numbers);

		// getting max value using max() method
		int max = Collections.max(numbers);

		System.out.println("List Collections api - min : " + min + ", max : " + max);
	}
}

Output:

List Collections api - min : 9, max : 76

4. Using Native Approach Method

First create the max variable with first value in the list. Next, Iterate the list values and compare the max value with the next value. When max value is greater than next number, store the current number in the max variable else do nothing. Next, Compare the max value with the next number in the list. Repeat the same logic.

At the end, whatever the value present in the max variable is the highest value.

The same logic applies to get the min value. But, condition should be min value less than current number.

package com.javaprogramto.java8.sorting.list;

import java.util.ArrayList;
import java.util.List;

public class ListNativeMinMaxExample {

	private static int getMinValue(List<Integer> numbers) {
		int min = Integer.MAX_VALUE;
		if (numbers.size() > 0) {

			for (Integer currentNumber : numbers) {

				if (min > currentNumber) {
					min = currentNumber;
				}
			}
		}
		return min;
	}

	private static int getMaxValue(List<Integer> numbers) {
		int max = Integer.MIN_VALUE;
		if (numbers.size() > 0) {

			for (Integer currentNumber : numbers) {

				if (max < currentNumber) {
					max = currentNumber;
				}
			}
		}
		return max;
	}

	public static void main(String[] args) {

		// creating a list to store the integer values
		List<Integer> numbers = new ArrayList<>();

		// adding integer values
		numbers.add(23);
		numbers.add(45);
		numbers.add(76);
		numbers.add(9);
		numbers.add(10);

		// getting the min value using min() method
		int min = getMinValue(numbers);

		// getting max value using max() method
		int max = getMaxValue(numbers);

		System.out.println("Native method - min : " + min + ", max : " + max);
	}

}

Output:

Native method - min : 9, max : 76

Time complexity is O(n).

5. Conclusion

In this article, you've seen the 3 different ways to get the min and max value from list in java.

GitHub

ListSortMinMaxExample

ListCollectionsMinMaxExample

ListNativeMinMaxExample

Read Next

Collections API

Collections.sort() and Custom Objects

How to find the largest value from array in java ?


No comments:

Post a Comment

Please do not add any spam links in the comments section.