Stream Min

The Min method of Stream class is used to get the minimum element from the Stream. It returns an Optional of the minimum element of the Stream or an Empty Optional so that if the minimum element does not exist it won't throw an Exception.

package org.wesome.java8;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.IntStream;

class Apple {
    public static void main(String args[]) {
        List<String> apples = Arrays.asList("Macintosh", "Fuji", "Gala", "Jonagold");

        /*  min based on first character of String  */
        String min = apples.stream().min(String::compareTo).get();
        System.out.println("min = " + min);

        /*  min based on first character of String  */
        min = apples.stream().min(Comparator.naturalOrder()).get();
        System.out.println("min = " + min);

        /*  min based on first character of String  */
        min = apples.stream().min(Comparator.reverseOrder()).get();
        System.out.println("min = " + min);

        /*  min based on length of String  */
        min = apples.stream().min(Comparator.comparing(String::length)).get();
        System.out.println("min = " + min);

        /*  min using compareTo method of String  */
        min = apples.stream().min((str1, str2) -> str1.compareTo(str2)).get();
        System.out.println("min = " + min);

        /*  min using case-insensitive order  */
        min = apples.stream().min((str1, str2) -> String.CASE_INSENSITIVE_ORDER.compare(str1, str2)).get();
        System.out.println("min = " + min);

        /*  compare on last character of String */
        min = apples.stream().min(Comparator.comparingInt(str -> str.charAt(str.length() - 1))).get();
        System.out.println("min = " + min);

        /*-------------------------Min on Integer-------------------------*/
        int minInt = IntStream.rangeClosed(1, 5).min().getAsInt();
        System.out.println("minInt = " + minInt);

        minInt = Arrays.stream(IntStream.rangeClosed(1, 5).toArray()).min().getAsInt();
        System.out.println("minInt = " + minInt);

        minInt = IntStream.rangeClosed(1, 5).min().orElse(Integer.MIN_VALUE);
        System.out.println("minInt = " + minInt);

        minInt = IntStream.rangeClosed(1, 5).min().orElseThrow(NoSuchElementException::new);
        System.out.println("minInt = " + minInt);
    }
}

Stream Min on user-defined object

package org.wesome.java8;

import lombok.AllArgsConstructor;
import lombok.Data;

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

@Data
@AllArgsConstructor
class Fruit {
    int fruitId;
    String fruitName;
    String taste;
    double price;
}

class FruitIdComparator implements Comparator<Fruit> {
    @Override
    public int compare(Fruit o1, Fruit o2) {
        return o1.getFruitName().compareTo(o2.getFruitName());
    }
}

class Apple {
    public static void main(String args[]) {
        List<Fruit> fruits = Arrays.asList(new Fruit(1, "Macintosh", "sweet", 1.1), new Fruit(2, "Fuji", "sweet", 2.2), new Fruit(3, "Gala", "sour", 1.1), new Fruit(4, "Jonagold", "sour", 2.2));

        Fruit fruit = fruits.stream().min(new FruitIdComparator()).get();
        System.out.println("fruit = " + fruit);

        fruit = fruits.stream().min((o1, o2) -> o1.getFruitName().compareTo(o2.getFruitName())).get();
        System.out.println("fruit = " + fruit);

        fruit = fruits.stream().min(Comparator.comparing(Fruit::getFruitName)).get();
        System.out.println("fruit = " + fruit);
    }
}

 

follow us on