Write a Java program using Lambda Expression to find the minimum values in a list of integers


The java program that finds the minimum number from a list of integers using Java Streams. Here's an explanation of the code:

  • import java.util.Arrays;: This line imports the Arrays class from the java.util package, which is used to create a list of integers from an array.
  • import java.util.List;: This line imports the List class from the java.util package, which is used to work with lists.
  • The MinimumNumber class is defined, which contains the main method, where the program's execution begins.
  • Inside the main method:
    • List<Integer> numb = Arrays.asList(13, 21, 78, 3, 35, 8, 10, 23);: This line creates a list of integers named numb containing the values 13, 21, 78, 3, 35, 8, 10, and 23.
    • int minNum = numb.stream().min(Integer::compare).orElse(0);: This line uses a stream to find the minimum value in the numb list using the min operation. It uses Integer::compare as the comparator to compare integers. If the list is empty, it returns the default value of 0 using the orElse method and stores the result in the minNum variable.
    • System.out.println("Minimum Value : " + minNum); : This line prints the minimum value found.

Source Code

import java.util.Arrays;
import java.util.List;
 
public class MinimumNumber
{
	public static void main(String[] args)
	{
		List<Integer> numb = Arrays.asList(13, 21, 78, 3, 35, 8, 10, 23);
 
		int minNum = numb.stream().min(Integer::compare).orElse(0);
 
		System.out.println("Minimum Value : " + minNum);
	}
}

Output

Minimum Value : 3

Example Programs