# min() & max() methods

In 
Published 2023-06-10

This tutorial explains how we can use min() and max() methods with Streams in Java.

Please take a look at the following example and read carefully the comments. The code is self-explanatory.

From the base application downloaded from Spring Initializr, I updated the main class as below:

DemoApplication.java
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

import java.util.*;
import java.util.concurrent.ExecutionException;

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) throws InterruptedException, ExecutionException {

		ApplicationContext appContext = SpringApplication.run(DemoApplication.class, args);

		Integer[] numbers = {10, 20, 30};

		Integer max = Arrays.stream(numbers)
				// Get the MAX value from a stream
				.max(Comparator.comparing(Integer::valueOf)).get();

		System.out.println("MAX="+max);
		System.out.println("----------------------------");

		Integer min = Arrays.stream(numbers)
				// Get the MIN value from a stream
				.min(Comparator.comparing(Integer::valueOf)).get();

		System.out.println("MIN="+min);
		System.out.println("----------------------------");

	}
}

When I run this code I receive:

MAX=30
----------------------------
MIN=10
----------------------------