Stream Max

Min method of Stream class is used to get the minimum element from the stream. It returns an Optional of the minimum element of this stream or an empty optional.

package org.wesome.java8;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Comparator;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

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

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

        /*  max based on first character of String  */
        max = apples.stream().max(Comparator.comparing(String::valueOf)).get();
        System.out.println("max = " + max);

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

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

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

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

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

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

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

        maxInt = IntStream.rangeClosed(1, 5).boxed().max(Comparator.comparing(Integer::valueOf)).get();
        System.out.println("maxInt = " + maxInt);

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

        maxInt = IntStream.rangeClosed(1, 5).max().orElse(Integer.MAX_VALUE);
        System.out.println("maxInt = " + maxInt);

        maxInt = IntStream.rangeClosed(1, 5).max().orElseThrow(NoSuchElementException::new);
        System.out.println("maxInt = " + maxInt);

        IntSummaryStatistics intSummaryStatistics = IntStream.rangeClosed(1, 5).summaryStatistics();
        System.out.println("intSummaryStatistics.getmax = " + intSummaryStatistics.getMax());

        /*-------------------------max on Date-------------------------*/

        LocalDate start = LocalDate.now();
        LocalDate end = LocalDate.now().plusMonths(1);

        List<LocalDate> dates = Stream.iterate(start, date -> date.plusDays(5)).limit(ChronoUnit.DAYS.between(start, end)).collect(Collectors.toList());
        System.out.println("dates = " + dates);
        LocalDate maxDate = dates.stream().max((o1, o2) -> o1.compareTo(o2)).get();
        System.out.println("maxDate = " + maxDate);
        maxDate = dates.stream().max(LocalDate::compareTo).get();
        System.out.println("maxDate = " + maxDate);
        maxDate = dates.stream().max(Comparator.naturalOrder()).get();
        System.out.println("maxDate = " + maxDate);
    }
}

 

Stream Max 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().max(new FruitIdComparator()).get();
        System.out.println("fruit = " + fruit);

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

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

 

follow us on