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

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

1. Finding Largest number in List or ArrayList

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

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

1.1 Using Stream.max() method

  • Stream.max() method allows to get maximum 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 max() method,
    • Integer::compare
    • Integer::compareTo
    • Comparator.naturalOrder()
    • Comparator.comparing(Integer::valueOf)
  • Stream.max() method returns Optional<T>
    • Using get() method of Optional<T> we can get maximum number from the List or ArrayList
  • Finally, printing maximum value to the console

FindLargestNumberInListUsingJava8StreamMaxMethod.java

package in.bench.resources.largest.number;

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

public class FindLargestNumberInListUsingJava8StreamMaxMethod {

	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 Largest number in List using max(Integer::compare).get()
		int max1 = numbers.stream().max(Integer::compare).get();
		System.out.println("\nLargest number using "
				+ "max(Integer::compare).get() is = " 
				+ max1);


		// 2. find Largest number in List using max(Integer::compareTo).get()
		int max2 = numbers.stream().max(Integer::compareTo).get();
		System.out.println("\nLargest number using "
				+ "max(Integer::compareTo).get() is = " 
				+ max2);


		// 3. find Largest number in List using max(Comparator.naturalOrder()).get()
		int max3 = numbers.stream().max(Comparator.naturalOrder()).get();
		System.out.println("\nLargest number using "
				+ "max(Comparator.naturalOrder()).get() is = " 
				+ max3);


		// 4. find Largest number in List using max(Comparator.comparing(Integer::valueOf)).get()
		int max4 = numbers.stream().max(Comparator.comparing(Integer::valueOf)).get();
		System.out.println("\nLargest number using "
				+ "max(Comparator.comparing(Integer::valueOf)).get() is = " 
				+ max4);


		// 5. find Largest number in List using mapToInt(Integer::intValue).max().getAsInt()
		int max5 = numbers.stream().mapToInt(Integer::intValue).max().getAsInt();
		System.out.println("\nLargest number using "
				+ "mapToInt(Integer::intValue).max().getAsInt() is = " 
				+ max5);
	}
}

Output:

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

Largest number using max(Integer::compare).get() is = 21

Largest number using max(Integer::compareTo).get() is = 21

Largest number using max(Comparator.naturalOrder()).get() is = 21

Largest number using max(Comparator.comparing(Integer::valueOf)).get() is = 21

Largest number using mapToInt(Integer::intValue).max().getAsInt() is = 21

1.2 Using Stream.collect() method

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

FindLargestNumberInListUsingJava8StreamCollectMethod.java

package in.bench.resources.largest.number;

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

public class FindLargestNumberInListUsingJava8StreamCollectMethod {

	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 Largest number using collect(Collectors.maxBy(Comparator.naturalOrder())).get()
		int max1 = numbers.stream().collect(Collectors.maxBy(Comparator.naturalOrder())).get();
		System.out.println("\nLargest number using "
				+ "collect(Collectors.maxBy(Comparator.naturalOrder())).get() is = " 
				+ max1);


		// 2. find Largest number - collect(Collectors.summarizingInt(Integer::intValue)).getMax()
		int max2 = numbers.stream().collect(Collectors.summarizingInt(Integer::intValue)).getMax();
		System.out.println("\nLargest number using "
				+ "collect(Collectors.summarizingInt(Integer::intValue)).getMax() is = " 
				+ max2);


		// 3. find Largest number in List using collect(Collectors.reducing(Integer::max)).get()
		int max3 = numbers.stream().collect(Collectors.reducing(Integer::max)).get();
		System.out.println("\nLargest number using "
				+ "collect(Collectors.reducing(Integer::max)).get() is = " 
				+ max3);


		// 4. find Largest number in List using collect(Collectors.reducing(Math::max)).get()
		int max4 = numbers.stream().collect(Collectors.reducing(Math::max)).get();
		System.out.println("\nLargest number using "
				+ "collect(Collectors.reducing(Math::max)).get() is = " 
				+ max4);
	}
}

Output:

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

Largest number using collect(Collectors.maxBy(Comparator.naturalOrder())).get() is = 21

Largest number using collect(Collectors.summarizingInt(Integer::intValue)).getMax() is = 21

Largest number using collect(Collectors.reducing(Integer::max)).get() is = 21

Largest number using collect(Collectors.reducing(Math::max)).get() is = 21

1.3 Using Stream.reduce() method

  • Stream.reduce() method accepts BinaryOperator to get maximum 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::max
    • Math::max
    • Lambda expression :- (x, y) -> x > y ? x : y
    • Lambda expression :- (x, y) -> Integer.max(x, y)
    • Lambda expression :- (x, y) -> Math.max(x, y)
  • Stream.reduce() method returns Optional<T>
    • Using get() method of Optional<T> we can get maximum number from the List or ArrayList
  • Finally, printing maximum value to the console

FindLargestNumberInListUsingJava8StreamReduceMethod.java

package in.bench.resources.largest.number;

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

public class FindLargestNumberInListUsingJava8StreamReduceMethod {

	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 Largest number in List using reduce(Integer::max).get()
		int max1 = numbers.stream().reduce(Integer::max).get();
		System.out.println("\nLargest number using "
				+ "reduce(Integer::max).get() is = " 
				+ max1);


		// 2. find Largest number in List using reduce(Math::max).get()
		int max2 = numbers.stream().reduce(Math::max).get();
		System.out.println("\nLargest number using "
				+ "reduce(Math::max).get() is = " 
				+ max2);


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


		// 4. find Largest number in List using reduce((x, y) -> Integer.max(x, y)).get()
		int max4 = numbers.stream().reduce((x, y) -> Integer.max(x, y)).get();
		System.out.println("\nLargest number using "
				+ "reduce((x, y) -> Integer.max(x, y)).get() is = " 
				+ max4);


		// 5. find Largest number in List using reduce((x, y) -> Math.max(x, y)).get()
		int max5 = numbers.stream().reduce((x, y) -> Math.max(x, y)).get();
		System.out.println("\nLargest number using "
				+ "reduce((x, y) -> Math.max(x, y)).get() is = " 
				+ max5);
	}
}

Output:

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

Largest number using reduce(Integer::max).get() is = 21

Largest number using reduce(Math::max).get() is = 21

Largest number using reduce((x, y) -> x > y ? x : y).get() is = 21

Largest number using reduce((x, y) -> Integer.max(x, y)).get() is = 21

Largest number using reduce((x, y) -> Math.max(x, y)).get() is = 21

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 getMax() method of IntSummaryStatistics returns maximum value/element from the processing Stream elements or List or ArrayList
  • Finally, printing maximum value to the console

FindLargestNumberInListUsingJava8IntSummaryStatistics.java

package in.bench.resources.largest.number;

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

public class FindLargestNumberInListUsingJava8IntSummaryStatistics {

	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 Largest number in List using IntSummaryStatistics
		int max = numbers // original source or numbers
				.stream() // get stream
				.mapToInt(num -> num) // convert to IntStream
				.summaryStatistics() // summary statistics
				.getMax(); // get max


		// print to console
		System.out.println("\nLargest number using "
				+ "IntSummaryStatistics.getMax() is = " 
				+ max);
	}
}

Output:

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

Largest number using IntSummaryStatistics.getMax() is = 21

2. Finding Largest number in an Arrays

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

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

2.1 Using Stream.max() method

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

FindLargestNumberInAnArraysUsingJava8StreamMaxMethod.java

package in.bench.resources.largest.number;

import java.util.Arrays;

public class FindLargestNumberInAnArraysUsingJava8StreamMaxMethod {

	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 Largest number in List using .max().getAsInt()
		int max = Arrays.stream(numbers).max().getAsInt();


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

Output:

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

Largest number using max().getAsInt() is = 21

2.2 Using Stream.reduce() method

  • Arrays.stream.reduce() method accepts IntBinaryOperator to get maximum 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::max
    • Math::max
    • Lambda expression :- (x, y) -> x > y ? x : y
    • Lambda expression :- (x, y) -> Integer.max(x, y)
    • Lambda expression :- (x, y) -> Math.max(x, y)
  • Arrays.stream.reduce() method returns OptionalInt
    • Using getAsInt() method of OptionalInt we can get maximum number from Arrays
  • Finally, printing maximum value to the console

FindLargestNumberInAnArraysUsingJava8StreamReduceMethod.java

package in.bench.resources.largest.number;

import java.util.Arrays;

public class FindLargestNumberInAnArraysUsingJava8StreamReduceMethod {

	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 Largest number in an Arrays using reduce(Integer::max).getAsInt()
		int max1 = Arrays.stream(numbers).reduce(Integer::max).getAsInt();
		System.out.println("\nLargest number using "
				+ "reduce(Integer::max).getAsInt() is = " 
				+ max1);


		// 2. find Largest number in an Arrays using reduce(Math::max).getAsInt()
		int max2 = Arrays.stream(numbers).reduce(Math::max).getAsInt();
		System.out.println("\nLargest number using "
				+ "reduce(Math::max).getAsInt() is = " 
				+ max2);


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


		// 4. find Largest number in an Arrays using reduce((x, y) -> Integer.max(x, y)).getAsInt()
		int max4 = Arrays.stream(numbers).reduce((x, y) -> Integer.max(x, y)).getAsInt();
		System.out.println("\nLargest number using "
				+ "reduce((x, y) -> Integer.max(x, y)).getAsInt() is = " 
				+ max4);


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

Output:

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

Largest number using reduce(Integer::max).getAsInt() is = 21

Largest number using reduce(Math::max).getAsInt() is = 21

Largest number using reduce((x, y) -> x > y ? x : y).getAsInt() is = 21

Largest number using reduce((x, y) -> Integer.max(x, y)).getAsInt() is = 21

Largest number using reduce((x, y) -> Math.max(x, y)).getAsInt() is = 21

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 getMax() method of IntSummaryStatistics returns maximum value/element from the processing Stream elements or Arrays
  • Finally, printing maximum value to the console

FindLargestNumberInAnArraysUsingJava8IntSummaryStatistics.java

package in.bench.resources.largest.number;

import java.util.Arrays;

public class FindLargestNumberInAnArraysUsingJava8IntSummaryStatistics {

	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 Largest number in an Arrays using IntSummaryStatistics
		int max = Arrays.stream(numbers).summaryStatistics().getMax();


		// print to console
		System.out.println("\nLargest number using "
				+ "IntSummaryStatistics.getMax() is = " 
				+ max);
	}
}

Output:

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

Largest number using IntSummaryStatistics.getMax() is = 21

Related Articles:

References:

Happy Coding !!
Happy Learning !!

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