Java 8 Streams - Finding largest & smallest element in an Array, List & Set

Example 1: Finding the largest element in an array

import java.util.Arrays;
/*
Java Program to find the largest number from an Array.
*/
public class DriverClass {
public static void main(String[] args) {

int[] numbers = {2, 6, 7, 9, 5, 155, 66, 99};
int largestNumber = Arrays.stream(numbers).
max().getAsInt();
System.out.println("Largest Number= " + largestNumber);
}
}

Output:
Largest Number= 155


Example 2: Finding the largest element in a List

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

/*
Java Program to find the largest number from a List.
*/
public class DriverClass {
public static void main(String[] args) {
List<Integer> numbers = Arrays.
asList(2, 6, 7, 9, 5, 155, 66, 99);
int largestNumber = numbers.stream().
max(Comparator.comparing
(Integer::valueOf)).get();
System.out.println("Largest Number= " + largestNumber);
}
}

Output:
Largest Number= 155


Example 3: Finding the smallest element in an Array

import java.util.Arrays;
/*
Java Program to find the smallest number from an array
*/
public class DriverClass {
public static void main(String[] args) {

int[] numbers = {12, 6, 7, 9, 5, 55, 4, 99};
int smallestNumber = Arrays.stream(numbers).
min().getAsInt();
System.out.println("Smallest Number= " + smallestNumber);
}
}

Output:
Smallest Number= 4


Example 4: Finding the smallest element in a List

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

/*
Java Program to find the smallest number from a List
*/
public class DriverClass {
public static void main(String[] args) {
List<Integer> numbers = Arrays.
asList(2, 6, 7, 9, 5, 55, 4, 99);
int smallestNumber = numbers.stream().
min(Comparator.comparing(Integer::valueOf)).get();
System.out.println("Smallest Number= " + smallestNumber);
}
}

Output:
Smallest Number= 2


Example 5: Finding the smallest element in a Set

import java.util.Arrays;
import java.util.Comparator;
import java.util.Set;

/*
Java Program to find the smallest number from a Set
*/
public class DriverClass {
public static void main(String[] args) {
Set<Integer> numbers = Set.of(2, 6, 7, 9, 5, 55, 4, 99);
int smallestNumber = numbers.stream().
min(Comparator.comparing(Integer::valueOf)).get();
System.out.println("Smallest Number= " + smallestNumber);
}
}

Output:
Smallest Number= 2


Example 6: Finding the largest element in a Set

import java.util.Arrays;
import java.util.Comparator;
import java.util.Set;

/*
Java Program to find the largest number from a Set.
*/
public class DriverClass {
public static void main(String[] args) {
Set<Integer> numbers = Set.of(2, 6, 7, 9, 5, 155, 66, 99);
int largestNumber = numbers.stream().max(Comparator.
comparing(Integer::valueOf)).get();
System.out.println("Largest Number= " + largestNumber);
}
}

Output:
Largest Number= 155


More...

Spring Boot + Angular - CRUD examples:


Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Spring boot video streaming example-HTML5

Spring Boot + Mockito simple application with 100% code coverage

Spring Boot + OpenCSV Export Data to CSV Example

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Custom Exception Handling in Quarkus REST API

ReactJS, Spring Boot JWT Authentication Example

DataTable-Pagination example with Spring boot, jQuery and ajax

Spring Webflux File Download Example

Node JS mini projects with source code - free download