在 Java 中,查询最大值和最小值是常见需求。以下将详细介绍 最大值和最小值的查询方法,包括适用于数组、集合、以及更复杂的数据结构的解决方案。
Java 提供了 Math.max
和 Math.min
方法,可用于直接比较两个值。
public class MaxMinWithMath {
public static void main(String[] args) {
int a = 5, b = 10;
System.out.println("最大值: " + Math.max(a, b)); // 输出 10
System.out.println("最小值: " + Math.min(a, b)); // 输出 5
double x = 2.5, y = 7.8;
System.out.println("最大值: " + Math.max(x, y)); // 输出 7.8
System.out.println("最小值: " + Math.min(x, y)); // 输出 2.5
}
}
通过遍历数组,可以找到其最大值和最小值。这是最常见的算法。
public class MaxMinInArray {
public static void main(String[] args) {
int[] nums = {3, 7, 2, 9, 4};
int max = nums[0]; // 假设第一个值为最大值
int min = nums[0]; // 假设第一个值为最小值
for (int num : nums) {
if (num > max) max = num; // 更新最大值
if (num < min) min = num; // 更新最小值
}
System.out.println("最大值: " + max); // 输出 9
System.out.println("最小值: " + min); // 输出 2
}
}
Java 的 java.util.Arrays
提供了对数组操作的工具,可以通过排序找到最大值和最小值。
import java.util.Arrays;
public class MaxMinUsingArrays {
public static void main(String[] args) {
int[] nums = {3, 7, 2, 9, 4};
Arrays.sort(nums); // 对数组进行排序
int min = nums[0]; // 第一个元素是最小值
int max = nums[nums.length - 1]; // 最后一个元素是最大值
System.out.println("最大值: " + max); // 输出 9
System.out.println("最小值: " + min); // 输出 2
}
}
java.util.Collections
提供了对集合操作的工具方法,可轻松找到 List
的最大值和最小值。
List
类型的集合。import java.util.Collections;
import java.util.Arrays;
import java.util.List;
public class MaxMinUsingCollections {
public static void main(String[] args) {
List<Integer> nums = Arrays.asList(3, 7, 2, 9, 4);
int max = Collections.max(nums); // 找最大值
int min = Collections.min(nums); // 找最小值
System.out.println("最大值: " + max); // 输出 9
System.out.println("最小值: " + min); // 输出 2
}
}
List
类型的数据。Java 8 引入了 Streams
API,可用于高效查询最大值和最小值。
import java.util.stream.IntStream;
public class MaxMinWithStreams {
public static void main(String[] args) {
int[] nums = {3, 7, 2, 9, 4};
int max = IntStream.of(nums).max().orElse(Integer.MIN_VALUE);
int min = IntStream.of(nums).min().orElse(Integer.MAX_VALUE);
System.out.println("最大值: " + max); // 输出 9
System.out.println("最小值: " + min); // 输出 2
}
}
import java.util.Arrays;
import java.util.List;
public class MaxMinInListStreams {
public static void main(String[] args) {
List<Integer> nums = Arrays.asList(3, 7, 2, 9, 4);
int max = nums.stream().max(Integer::compareTo).orElse(Integer.MIN_VALUE);
int min = nums.stream().min(Integer::compareTo).orElse(Integer.MAX_VALUE);
System.out.println("最大值: " + max); // 输出 9
System.out.println("最小值: " + min); // 输出 2
}
}
Streams
API 的用法。Optional
或 orElse
提供默认值。import java.util.OptionalInt;
public class HandleEmptyArray {
public static void main(String[] args) {
int[] nums = {};
int max = IntStream.of(nums).max().orElse(Integer.MIN_VALUE); // 默认值
int min = IntStream.of(nums).min().orElse(Integer.MAX_VALUE); // 默认值
System.out.println("最大值: " + max); // 输出 Integer.MIN_VALUE
System.out.println("最小值: " + min); // 输出 Integer.MAX_VALUE
}
}
当所有值相等时,最大值和最小值相等。
int[] nums = {5, 5, 5, 5};
int max = IntStream.of(nums).max().orElse(Integer.MIN_VALUE);
int min = IntStream.of(nums).min().orElse(Integer.MAX_VALUE);
System.out.println("最大值: " + max); // 输出 5
System.out.println("最小值: " + min); // 输出 5
方法 | 优点 | 缺点 |
---|---|---|
Math.max / Math.min | 简单直接,适合少量数字比较 | 无法直接处理数组或集合 |
遍历法 | 高效( O ( n ) O(n) O(n)),适合数组 | 需要手动实现逻辑 |
Arrays.sort | 简洁,适合小型数组 | 修改原数组,效率低于遍历法 |
Collections.max / min | 简洁,专为 List 提供 |
不支持数组,需先转换为 List |
Streams | 现代化、支持并行流 | 写法较复杂,依赖 Java 8 及以上版本 |
Math.max
和 Math.min
。选择合适的方法可以提升代码的可读性和性能。