Java 8 – Find Smallest number in an Arrays or List or Stream ?

In this article, we will discuss how to find smallest number in an Arrays and List using Java 1.8 version

In one of the previous article, already discussed to find largest number/element in an Arrays or List without using Java 8 Stream, read Java – Find Smallest number in an Arrays or List ?

1. Finding Smallest number in List or ArrayList :

We will find Smallest number in a List or ArrayList using different methods of Java 8 Stream

  • Using Stream.min() method
  • Using Stream.collect() method
  • Using Stream.reduce() method
  • Using IntStream.summaryStatistics() method

1.1 Using Stream.min() method :

  • Stream.min() method allows to get minimum value from the processing Stream elements by passing java.util.Comparator as argument
  • In the below illustration we used different Comparators as method-reference to min() method,
    • Integer::compare
    • Integer::compareTo
    • Comparator.naturalOrder()
    • Comparator.comparing(Integer::valueOf)
  • Stream.min() method returns Optional<T>
    • Using get() method of Optional<T> we can get minimum number from the List or ArrayList
  • Finally, printing minimum value to the console

FindSmallestNumberInListUsingJava8StreamMinMethod.java

package in.bench.resources.smallest.number;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class FindSmallestNumberInListUsingJava8StreamMinMethod {

	public static void main(String[] args) {

		// random numbers
		List<Integer> numbers = Arrays.asList(5, 9, 11, 2, 8, 21, 1);


		// print to console
		System.out.println("Original Integer List - " + numbers);


		// 1. find Smallest number in List using min(Integer::compare).get()
		int min1 = numbers.stream().min(Integer::compare).get();
		System.out.println("\nSmallest number using "
				+ "min(Integer::compare).get() is = " 
				+ min1);


		// 2. find Smallest number in List using min(Integer::compareTo).get()
		int min2 = numbers.stream().min(Integer::compareTo).get();
		System.out.println("\nSmallest number using "
				+ "min(Integer::compareTo).get() is = " 
				+ min2);


		// 3. find Smallest number in List using min(Comparator.naturalOrder()).get()
		int min3 = numbers.stream().min(Comparator.naturalOrder()).get();
		System.out.println("\nSmallest number using "
				+ "min(Comparator.naturalOrder()).get() is = " 
				+ min3);


		// 4. find Smallest number in List using min(Comparator.comparing(Integer::valueOf)).get()
		int min4 = numbers.stream().min(Comparator.comparing(Integer::valueOf)).get();
		System.out.println("\nSmallest number using "
				+ "min(Comparator.comparing(Integer::valueOf)).get() is = " 
				+ min4);


		// 5. find Smallest number in List using mapToInt(Integer::intValue).min().getAsInt()
		int min5 = numbers.stream().mapToInt(Integer::intValue).min().getAsInt();
		System.out.println("\nSmallest number using "
				+ "mapToInt(Integer::intValue).min().getAsInt() is = " 
				+ min5);
	}
}

Output:

Original Integer List - [5, 9, 11, 2, 8, 21, 1]

Smallest number using min(Integer::compare).get() is = 1

Smallest number using min(Integer::compareTo).get() is = 1

Smallest number using min(Comparator.naturalOrder()).get() is = 1

Smallest number using min(Comparator.comparing(Integer::valueOf)).get() is = 1

Smallest number using mapToInt(Integer::intValue).min().getAsInt() is = 1

1.2 Using Stream.collect() method :

  • Stream.collect() method accepts java.util.stream.Collectors as argument
  • Collectors class has many useful methods to get minimum value from the processing Stream elements like
    • Collectors.minBy()
    • Collectors.summarizingInt()
    • Collectors.reducing()
  • Collectors.minBy() accepts Comparator.naturalOrder() as method-argument and returns Optional<T>
    • Using get() method of Optional<T> we can get minimum number from the List or ArrayList
  • Collectors.summarizingInt() accepts Integer::intValue as method-reference and returns IntSummaryStatistics
    • Using getMin() method of IntSummaryStatistics we can get minimum number from the List or ArrayList
  • Collectors.reducing() accepts Integer::min or Math::min as method-reference and returns Optional<T>
    • Using get() method of Optional<T> we can get minimum number from the List or ArrayList
  • Finally, printing minimum value to the console

FindSmallestNumberInListUsingJava8StreamCollectMethod.java

package in.bench.resources.smallest.number;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class FindSmallestNumberInListUsingJava8StreamCollectMethod {

	public static void main(String[] args) {

		// random numbers
		List<Integer> numbers = Arrays.asList(5, 9, 11, 2, 8, 21, 1);


		// print to console
		System.out.println("Original Integer List - " + numbers);


		// 1. find Smallest number using collect(Collectors.minBy(Comparator.naturalOrder())).get()
		int min1 = numbers.stream().collect(Collectors.minBy(Comparator.naturalOrder())).get();
		System.out.println("\nSmallest number using "
				+ "collect(Collectors.minBy(Comparator.naturalOrder())).get() is = " 
				+ min1);


		// 2. find Smallest number - collect(Collectors.summarizingInt(Integer::intValue)).getMin()
		int min2 = numbers.stream().collect(Collectors.summarizingInt(Integer::intValue)).getMin();
		System.out.println("\nSmallest number using "
				+ "collect(Collectors.summarizingInt(Integer::intValue)).getMin() is = " 
				+ min2);


		// 3. find Smallest number in List using collect(Collectors.reducing(Integer::min)).get()
		int min3 = numbers.stream().collect(Collectors.reducing(Integer::min)).get();
		System.out.println("\nSmallest number using "
				+ "collect(Collectors.reducing(Integer::min)).get() is = " 
				+ min3);


		// 4. find Smallest number in List using collect(Collectors.reducing(Math::min)).get()
		int min4 = numbers.stream().collect(Collectors.reducing(Math::min)).get();
		System.out.println("\nSmallest number using "
				+ "collect(Collectors.reducing(Math::min)).get() is = " 
				+ min4);
	}
}

Output:

Original Integer List - [5, 9, 11, 2, 8, 21, 1]

Smallest number using collect(Collectors.minBy(Comparator.naturalOrder())).get() is = 1

Smallest number using collect(Collectors.summarizingInt(Integer::intValue)).getMin() is = 1

Smallest number using collect(Collectors.reducing(Integer::min)).get() is = 1

Smallest number using collect(Collectors.reducing(Math::min)).get() is = 1

1.3 Using Stream.reduce() method :

  • Stream.reduce() method accepts BinaryOperator to get minimum value from the processing Stream elements by passing method-references as argument
  • In the below illustration we used different method-references to reduce() method,
    • Integer::min
    • Math::min
    • Lambda expression :- (x, y) -> x < y ? x : y
    • Lambda expression :- (x, y) -> Integer.min(x, y)
    • Lambda expression :- (x, y) -> Math.min(x, y)
  • Stream.reduce() method returns Optional<T>
    • Using get() method of Optional<T> we can get minimum number from the List or ArrayList
  • Finally, printing minimum value to the console

FindSmallestNumberInListUsingJava8StreamReduceMethod.java

package in.bench.resources.smallest.number;

import java.util.Arrays;
import java.util.List;

public class FindSmallestNumberInListUsingJava8StreamReduceMethod {

	public static void main(String[] args) {

		// random numbers
		List<Integer> numbers = Arrays.asList(5, 9, 11, 2, 8, 21, 1);


		// print to console
		System.out.println("Original Integer List - " + numbers);


		// 1. find Smallest number in List using reduce(Integer::min).get()
		int min1 = numbers.stream().reduce(Integer::min).get();
		System.out.println("\nSmallest number using "
				+ "reduce(Integer::min).get() is = " 
				+ min1);


		// 2. find Smallest number in List using reduce(Math::min).get()
		int min2 = numbers.stream().reduce(Math::min).get();
		System.out.println("\nSmallest number using "
				+ "reduce(Math::min).get() is = " 
				+ min2);


		// 3. find Smallest number in List using reduce((x, y) -> x < y ? x : y).get()
		int min3 = numbers.stream().reduce((x, y) -> x < y ? x : y).get();
		System.out.println("\nSmallest number using "
				+ "reduce((x, y) -> x < y ? x : y).get() is = " 
				+ min3);


		// 4. find Smallest number in List using reduce((x, y) -> Integer.min(x, y)).get()
		int min4 = numbers.stream().reduce((x, y) -> Integer.min(x, y)).get();
		System.out.println("\nSmallest number using "
				+ "reduce((x, y) -> Integer.min(x, y)).get() is = " 
				+ min4);


		// 5. find Smallest number in List using reduce((x, y) -> Math.min(x, y)).get()
		int min5 = numbers.stream().reduce((x, y) -> Math.min(x, y)).get();
		System.out.println("\nSmallest number using "
				+ "reduce((x, y) -> Math.min(x, y)).get() is = " 
				+ min5);
	}
}

Output:

Original Integer List - [5, 9, 11, 2, 8, 21, 1]

Smallest number using reduce(Integer::min).get() is = 1

Smallest number using reduce(Math::min).get() is = 1

Smallest number using reduce((x, y) -> x < y ? x : y).get() is = 1

Smallest number using reduce((x, y) -> Integer.min(x, y)).get() is = 1

Smallest number using reduce((x, y) -> Math.min(x, y)).get() is = 1

1.4 Using IntStream.summaryStatistics() method :

  • We can get summaryStatistics from the processing Stream elements which has useful methods to get,
    • minimum value
    • maximum value
    • average
    • number of elements – count
    • sum of the processing elements
  • Using getMin() method of IntSummaryStatistics returns minimum value/element from the processing Stream elements or List or ArrayList
  • Finally, printing minimum value to the console

FindSmallestNumberInListUsingJava8IntSummaryStatistics.java

package in.bench.resources.smallest.number;

import java.util.Arrays;
import java.util.List;

public class FindSmallestNumberInListUsingJava8IntSummaryStatistics {

	public static void main(String[] args) {

		// random numbers
		List<Integer> numbers = Arrays.asList(5, 9, 11, 2, 8, 21, 1);


		// print to console
		System.out.println("Original Integer List - " + numbers);


		// find Smallest number in List using IntSummaryStatistics
		int min = numbers // original source or numbers
				.stream() // get stream
				.mapToInt(num -> num) // convert to IntStream
				.summaryStatistics() // summary statistics
				.getMin(); // get min


		// print to console
		System.out.println("\nSmallest number using "
				+ "IntSummaryStatistics.getmin() is = " 
				+ min);
	}
}

Output:

Original Integer List - [5, 9, 11, 2, 8, 21, 1]

Smallest number using IntSummaryStatistics.getmin() is = 1

2. Finding Smallest number in an Arrays :

We will find Smallest number in an Arrays using different methods of Java 8 Stream

  • Using Stream.min() method
  • Using Stream.reduce() method
  • Using IntStream.summaryStatistics() method

2.1 Using Stream.min() method :

  • Arrays.stream.min() method returns miimum value element from the processing Stream elements and this method returns OptionalInt
    • Using getAsInt() method of OptionalInt we can get minimum number from Arrays
  • Finally, printing minimum value to the console

FindSmallestNumberInAnArraysUsingJava8StreamMinMethod.java

package in.bench.resources.smallest.number;

import java.util.Arrays;

public class FindSmallestNumberInAnArraysUsingJava8StreamMinMethod {

	public static void main(String[] args) {

		// random numbers
		int[] numbers = {5, 9, 11, 2, 8, 21, 1};


		// print to console
		System.out.println("Numbers in an Arrays - " 
				+ Arrays.toString(numbers));


		// find Smallest number in List using Stream.min().getAsInt()
		int min = Arrays.stream(numbers).min().getAsInt();


		// print to console
		System.out.println("\nSmallest number using "
				+ "Stream.min().getAsInt() is = " 
				+ min);
	}
}

Output:

Numbers in an Arrays - [5, 9, 11, 2, 8, 21, 1]

Smallest number using Stream.min().getAsInt() is = 1

2.2 Using Stream.reduce() method :

  • Arrays.stream.reduce() method accepts IntBinaryOperator to get minimum value from the processing Stream elements by passing method-references as argument
  • In the below illustration we used different method-references to reduce() method,
    • Integer::min
    • Math::min
    • Lambda expression :- (x, y) -> x < y ? x : y
    • Lambda expression :- (x, y) -> Integer.min(x, y)
    • Lambda expression :- (x, y) -> Math.min(x, y)
  • Arrays.stream.reduce() method returns OptionalInt
    • Using getAsInt() method of OptionalInt we can get minimum number from Arrays
  • Finally, printing minimum value to the console

FindSmallestNumberInAnArraysUsingJava8StreamReduceMethod.java

package in.bench.resources.smallest.number;

import java.util.Arrays;

public class FindSmallestNumberInAnArraysUsingJava8StreamReduceMethod {

	public static void main(String[] args) {

		// random numbers
		int[] numbers = {5, 9, 11, 2, 8, 21, 1};


		// print to console
		System.out.println("Numbers in an Arrays - " 
				+ Arrays.toString(numbers));


		// 1. find Smallest number in an Arrays using reduce(Integer::min).getAsInt()
		int min1 = Arrays.stream(numbers).reduce(Integer::min).getAsInt();
		System.out.println("\nSmallest number using "
				+ "reduce(Integer::min).getAsInt() is = " 
				+ min1);


		// 2. find Smallest number in an Arrays using reduce(Math::min).getAsInt()
		int min2 = Arrays.stream(numbers).reduce(Math::min).getAsInt();
		System.out.println("\nSmallest number using "
				+ "reduce(Math::min).getAsInt() is = " 
				+ min2);


		// 3. find Smallest number in an Arrays using reduce((x, y) -> x < y ? x : y).getAsInt()
		int min3 = Arrays.stream(numbers).reduce((x, y) -> x < y ? x : y).getAsInt();
		System.out.println("\nSmallest number using "
				+ "reduce((x, y) -> x < y ? x : y).getAsInt() is = " 
				+ min3);


		// 4. find Smallest number in an Arrays using reduce((x, y) -> Integer.min(x, y)).getAsInt()
		int min4 = Arrays.stream(numbers).reduce((x, y) -> Integer.min(x, y)).getAsInt();
		System.out.println("\nSmallest number using "
				+ "reduce((x, y) -> Integer.min(x, y)).getAsInt() is = " 
				+ min4);


		// 5. find Smallest number in an Arrays using reduce((x, y) -> Math.min(x, y)).getAsInt()
		int min5 = Arrays.stream(numbers).reduce((x, y) -> Math.min(x, y)).getAsInt();
		System.out.println("\nSmallest number using "
				+ "reduce((x, y) -> Math.min(x, y)).getAsInt() is = " 
				+ min5);
	}
}

Output:

Numbers in an Arrays - [5, 9, 11, 2, 8, 21, 1]

Smallest number using reduce(Integer::min).getAsInt() is = 1

Smallest number using reduce(Math::min).getAsInt() is = 1

Smallest number using reduce((x, y) -> x < y ? x : y).getAsInt() is = 1

Smallest number using reduce((x, y) -> Integer.min(x, y)).getAsInt() is = 1

Smallest number using reduce((x, y) -> Math.min(x, y)).getAsInt() is = 1

2.3 Using IntStream.summaryStatistics() method :

  • We can get summaryStatistics from the processing Stream elements which has useful methods to get,
    • minimum value
    • maximum value
    • average
    • number of elements – count
    • sum of the processing elements
  • Using getMin() method of IntSummaryStatistics returns minimum value/element from the processing Stream elements or Arrays
  • Finally, printing minimum value to the console

FindSmallestNumberInAnArraysUsingJava8IntSummaryStatistics.java

package in.bench.resources.smallest.number;

import java.util.Arrays;

public class FindSmallestNumberInAnArraysUsingJava8IntSummaryStatistics {

	public static void main(String[] args) {

		// random numbers
		int[] numbers = {5, 9, 11, 2, 8, 21, 1};


		// print to console
		System.out.println("Numbers in an Arrays - " 
				+ Arrays.toString(numbers));


		// find Smallest number in an Arrays using IntSummaryStatistics
		int min = Arrays.stream(numbers).summaryStatistics().getMin();


		// print to console
		System.out.println("\nSmallest number using "
				+ "IntSummaryStatistics.getmin() is = " 
				+ min);
	}
}

Output:

Numbers in an Arrays - [5, 9, 11, 2, 8, 21, 1]

Smallest number using IntSummaryStatistics.getmin() is = 1

Related Articles :

References :

Happy Coding !!
Happy Learning !!

Java 8 – Find Second Largest number in an Arrays or List or Stream ?
Java 8 – Find Largest number in an Arrays or List or Stream ?