import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.*; import java.util.stream.*; public class StreamComparatorInlining { static class Holder { @Nullable String nullable; @Nullable String getNullable() { return nullable; } } @Nullable String process(String s) { return s.isEmpty() ? null : s; } void testMinMax(List list) { list.stream().map(this::process).max(Comparator.naturalOrder()); list.stream().map(this::process).max(Comparator.nullsFirst(Comparator.naturalOrder())); list.stream().map(this::process).max(Comparator.nullsFirst(Comparator.comparing(String::length))); list.stream().map(this::process).min(Comparator.nullsLast(Comparator.comparing(String::length))); list.stream().map(this::process).min(Comparator.comparing(String::length)); list.stream().map(this::process).min(Comparator.comparing(String::length).reversed()); } void testSorted(List list) { list.stream().sorted(Comparator.comparing(this::process, Comparator.reverseOrder())).collect(Collectors.toList()); list.stream().sorted(Comparator.comparing(this::process)).collect(Collectors.toList()); list.stream().sorted(Comparator.comparing(this::process, Comparator.nullsFirst(Comparator.naturalOrder()))).collect(Collectors.toList()); list.stream().map(this::process).sorted(Comparator.comparing(String::length)).collect(Collectors.toList()); list.stream().map(this::process).sorted().collect(Collectors.toList()); list.stream().map(this::process).sorted(String::compareToIgnoreCase).collect(Collectors.toList()); list.stream().map(this::process).sorted(String.CASE_INSENSITIVE_ORDER).collect(Collectors.toList()); } void testSortedCheck(List holders) { holders.stream().sorted(Comparator.comparing(h -> h.nullable)).toArray(); holders.stream().filter(h -> h.nullable != null).sorted(Comparator.comparing(h -> h.nullable)).toArray(); holders.stream().sorted(Comparator.comparing(h -> h.getNullable())).toArray(); holders.stream().filter(h -> h.getNullable() != null).sorted(Comparator.comparing(h -> h.getNullable())).toArray(); holders.stream().sorted(Comparator.comparing(Holder::getNullable)).toArray(); holders.stream().filter(h -> h.getNullable() != null).sorted(Comparator.comparing(Holder::getNullable)).toArray(); } }