java MaxMin
时间: 2024-01-02 19:22:55 浏览: 116
maxmin的java
以下是两种Java求最大值和最小值的例子:
1. 使用进阶的MinMax类:
```java
import com.northcastle.I_stream.MinMax;
int[] nums = {1, 2, -5, ..., n};
int min = MinMax.min(nums);
int max = MinMax.max(nums);
System.out.println("最小值是:" + min);
System.out.println("最大值是:" + max);
```
2. 使用流(Stream)的方式:
```java
import java.util.Optional;
import java.util.stream.Stream;
String[] strs = {"1", "5", "2", "3", "100"};
Optional<Integer> max = Stream.of(strs)
.map(Integer::parseInt)
.max((o1, o2) -> {
System.out.println("o1 = " + o1 + " ; o2 = " + o2);
return o1 - o2;
});
System.out.println("最大值是:" + max.get());
```
阅读全文