Stream.min
和
Stream.max
示例。
java.util.stream.Stream
已经在java8中引入。
使用Stream.min
方法,我们得到给定比较器的流的最小元素。
使用Stream.max
方法,我们得到了给定比较器的流的最大元素。
min
和max
方法都是流终端操作。
让我们用例子来讨论min和max方法。
它根据提供的比较器(Comparator
)返回此流的最小元素。这是一种获取流元素中最小值简写的方法,例如使用Stream.reduce
方法。
在javadoc中min
方法声明
Optional<T> min(Comparator<? super T> comparator)
参数:传递比较器(Comparator
)来比较元素。
返回:该方法返回提供最小元素或为空的选择。
异常:如果最小值为null,则该方法抛出NullPointerException。
它根据提供的比较器(Comparator
)返回此流的最大元素。这是一种获取流元素中最大值简写的方法,例如使用Stream.reduce
方法。
在javadoc中max
方法声明
Optional<T> max(Comparator<? super T> comparator)
参数:传递比较器(Comparator
)来比较元素。
返回:该方法返回提供最大元素或为空的选择。
异常:如果最大值为null,则该方法抛出NullPointerException。
String
)和整数(Integer
)中如何使用min和max方法下面是在字符串流和整数流中获取最小值和最大值的示例
MinMaxDemo1.java
package com.concretepage;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
public class MinMaxDemo1 {
public static void main(String[] args) {
System.out.println("---Min and Max for Integer---");
List<Integer> numList = Arrays.asList(42, 44, 43, 41);
Comparator<Integer> comparator = Comparator.comparing(Integer::intValue);
Optional<Integer> minOptional = numList.stream().min(comparator);
minOptional.ifPresent(e -> System.out.println("Min: " + e));
Optional<Integer> maxOptional = numList.stream().max(comparator);
maxOptional.ifPresent(e -> System.out.println("Max: " + e));
System.out.println("---Min and Max for String---");
List<String> list = Arrays.asList("Mohit", "Nilesh", "Shankar", "Brajesh");
list.stream().min(Comparator.comparing(String::valueOf))
.ifPresent(e -> System.out.println("Min: " + e));
list.stream().max(Comparator.comparing(String::valueOf))
.ifPresent(e -> System.out.println("Max: " + e));
}
}
输出
---Min and Max for Integer---
Min: 41
Max: 44
---Min and Max for String---
Min: Brajesh
Max: Shankar
我们知道min和max是流获取最大最小值的简写方法。让我们使用非简写的方式来获取最小值和最大值。
MinMaxDemoWithReduce.java
package com.concretepage;
import java.util.Arrays;
import java.util.List;
public class MinMaxDemoWithReduce {
public static void main(String[] args) {
System.out.println("---Min and Max for Integer---");
List<Integer> numList = Arrays.asList(42, 44, 43, 41);
//For min
numList.stream().reduce(Integer::min).ifPresent(s -> System.out.println(s)); //41
//For max
numList.stream().reduce(Integer::max).ifPresent(s -> System.out.println(s)); //44
System.out.println("---Min and Max for String---");
List<String> list = Arrays.asList("Mohit", "Nilesh", "Shankar", "Brajesh");
//For min
list.stream().reduce((s1, s2) -> {
if (s1.compareTo(s2) <= 0) {
return s1;
}
return s2;
}).ifPresent(s -> System.out.println(s)); //Brajesh
//For max
list.stream().reduce((s1, s2) -> {
if (s1.compareTo(s2) >= 0) {
return s1;
}
return s2;
}).ifPresent(s -> System.out.println(s)); //Shankar
}
}
Object
)中使用Min和Max方法下面是在对象流中获取最小值和最大值的示例
MinMaxDemo2.java
package com.concretepage;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class MinMaxDemo2 {
public static void main(String[] args) {
List<User> users = Arrays.asList(
new User("Mahesh", 30),
new User("Krishna", 29),
new User("Virat", 28)
);
System.out.println("---Min and Max on the basis of user name---");
users.stream()
.min(Comparator.comparing(u -> u.getUserName()))
.ifPresent(e -> System.out.println("Min: " + e.getUserName()));
users.stream()
.max(Comparator.comparing(u -> u.getUserName()))
.ifPresent(e -> System.out.println("Max: " + e.getUserName()));
System.out.println("---Min and Max on the basis of age---");
users.stream()
.min(Comparator.comparing(User::getAge))
.ifPresent(e -> System.out.println("Min: " + e.getUserName()));
users.stream()
.max(Comparator.comparing(User::getAge))
.ifPresent(e -> System.out.println("Max: " + e.getUserName()));
}
}
class User {
private String userName;
private int age;
public User(String userName, int age) {
this.userName = userName;
this.age = age;
}
//Sets and Gets
}
输出
---Min and Max on the basis of user name---
Min: Krishna
Max: Virat
---Min and Max on the basis of age---
Min: Virat
Max: Mahesh
Date
)中使用Min和Max方法下面是在日期流中获取最小值和最大值的示例
MinMaxDemo3.java
package com.concretepage;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class MinMaxDemo3 {
public static void main(String[] args) {
LocalDate ld = LocalDate.parse("2020-05-15");
List<LocalDate> ldList = Arrays.asList(
ld.minus(10, ChronoUnit.DAYS), //2020-05-05
ld, //2020-05-15
ld.plus(15, ChronoUnit.DAYS) //2020-05-30
);
ldList.stream()
.min(Comparator.comparing(LocalDate::toEpochDay))
.ifPresent(e -> System.out.println("Min: " + e));
ldList.stream()
.max(Comparator.comparing(LocalDate::toEpochDay))
.ifPresent(e -> System.out.println("Max: " + e));
}
输出
Min: 2020-05-05
Max: 2020-05-30
找到IntStream、LongStream和DoubleStream的min和max方法在的Javadoc的声明。
For IntStream.
OptionalInt min()
OptionalInt max()
For LongStream.
OptionalLong min()
OptionalLong max()
For DoubleStream.
OptionalDouble min()
OptionalDouble max()
我们可以看到,在上述方法中,我们不需要通过比较器。
MinMaxDemo4.java
package com.concretepage;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
public class MinMaxDemo4 {
public static void main(String[] args) {
System.out.println("--- Min and Max for IntStream ---");
IntStream.of(12, 20, 35, 48).min()
.ifPresent(i -> System.out.println(i));
IntStream.of(12, 20, 35, 48).max()
.ifPresent(i -> System.out.println(i));
System.out.println("--- Min and Max for LongStream ---");
LongStream.of(200, 300, 400).min()
.ifPresent(l -> System.out.println(l));
LongStream.of(200, 300, 400).max()
.ifPresent(l -> System.out.println(l));
System.out.println("--- Min and Max for DoubleStream ---");
DoubleStream.of(110.54, 230.57, 360.65).min()
.ifPresent(l -> System.out.println(l));
DoubleStream.of(110.54, 230.57, 360.65).max()
.ifPresent(l -> System.out.println(l));
}
}
输出
--- Min and Max for IntStream ---
12
48
--- Min and Max for LongStream ---
200
400
--- Min and Max for DoubleStream ---
110.54
360.65
【1】Java doc: Stream
【2】Java Stream min() and max()