Finding Minimum 3 values from a list in Java 8

To find Minimum 3 values from a list using sorted() and limit() method from Java 8.

  • First convert Arrays to list using Arrays.asList() method.
  • Now get the Stream data from List using arrayList.stream() method.
  • Here sorted() method of java 8 allow to be sorting the number in ascending order.
  • After limit() method limit the stream of first 3 number.
  • From collect() method, we collect all the list of integer data.
  • Print the Minimum 3 number from a list.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class MinThreeNumber {
	public static void main(String[] args) {
		List<Integer> list = Arrays.asList(12, 43, 56, 37, 98, 25, 80);
		List<Integer> minNumber = list.stream().sorted().limit(3).collect(Collectors.toList());
		System.out.println("Minimum three number are :- " + minNumber);
	}
}

Output :-
Minimum three number are :- [12, 25, 37]