Find 3 Max Integers in an Array in Java 8

Learn how to find 3 max integers in an array in Java 8 using Stream API , Collections.reverseOrder() , compareTo(), sorted(), limit(), and Collectors.toList() .

There are different ways to find 3 max integers in an array in Java.

  1. Using Collections.reverseOrder() method
  2. Using compareTo() method

Code Explanation Step-by-Step :

Arrays.asList() converts the given numbers into a list.

Using Collections.reverseOrder() :

  • stream() converts the numbers list into a Stream so that we can perform Stream operations on it.
  • .sorted(Collections.reverseOrder()) sorts the numbers in descending order, meaning from highest to lowest.
  • .limit(3) takes only the first 3 numbers from the sorted stream.
  • .collect(Collectors.toList()) collects the final 3 maximum numbers and stores them into a new list named value.
  • Finally, the top 3 maximum integers are printed on the console.

Using compareTo() :

  • stream() converts the numbers list into a Stream to process the elements one by one.
  • .sorted((a, b) -> b.compareTo(a)) sorts the numbers in descending order using the compareTo() method. here, b.compareTo(a) compares the second number with the first number, so larger numbers come first.
  • .limit(3) takes only the first 3 numbers from the sorted stream.
  • .collect(Collectors.toList()) collects the selected 3 maximum numbers into a new list named data.
  • Finally, the top 3 maximum integers are printed on the console.
				
					import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

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

        List<Integer> numbers = Arrays.asList(2, 4, 10, 5, 20, 15, 25, 6, 12, 56);

        List<Integer> value = numbers.stream()
                .sorted(Collections.reverseOrder())
                .limit(3)
                .collect(Collectors.toList());

        System.out.println("First 3 max numbers using reverse method: " + value);

        List<Integer> data = numbers.stream()
                .sorted((a, b) -> b.compareTo(a))
                .limit(3)
                .collect(Collectors.toList());

        System.out.println("First 3 max numbers using compareTo method: " + data);
    }
}
				
			

Output :
First 3 max numbers using reverse method: [56, 25, 20]
First 3 max numbers using compareTo method: [56, 25, 20]

Find 3 Minimum Integers in an Array in Java 8

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

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

        List<Integer> numbers = Arrays.asList(2, 4, 10, 5, 20, 15, 25, 6, 12, 56);

        List<Integer> minNumbers = numbers.stream()
                .sorted()
                .limit(3)
                .collect(Collectors.toList());

        System.out.println("First 3 min numbers: " + minNumbers);
    }
}
				
			

Output :
First 3 min numbers: [2, 4, 5]

FAQ
How do you find 3 max integers in an array in Java?
You can find 3 max integers in an array in Java by sorting the numbers in descending order using Java 8 Stream API and then applying the limit(3) method.
Which method is used to sort numbers in descending order in Java?
You can use Collections.reverseOrder() or compareTo() inside the sorted() method to sort numbers in descending order.
What does limit(3) do in Java Stream?
The limit(3) method returns a stream containing only the first 3 elements from the original stream.
Can we find top 3 maximum numbers using Java 8 Stream API?
Yes, we can find the top 3 maximum numbers using Java 8 Stream API with sorted(), limit(), and collect() methods.