方案1:使用数组(同类型数据)
适用场景:返回两个相同类型的值(如两个整数)。
public int[] calculateMinMax(int[] data) { int min = Arrays.stream(data).min().getAsInt(); int max = Arrays.stream(data).max().getAsInt(); return new int[]{min, max}; // 返回数组 } // 调用示例 int[] result = calculateMinMax(new int[]{3, 1, 4, 2}); System.out.println("最小值:" + result[0] + ", 最大值:" + result[1]);
方案2:自定义类(最佳实践)
适用场景:返回值有明确业务含义时(强类型、高可读性)。
// 定义结果封装类 public class CalculationResult { private final int min; private final int max; public CalculationResult(int min, int max) { this.min = min; this.max = max; } // Getter方法 public int getMin() { return min; } public int getMax() { return max; } } // 方法返回自定义对象 public CalculationResult findMinMax(int[] data) { int min = Arrays.stream(data).min().getAsInt(); int max = Arrays.stream(data).max().getAsInt(); return new CalculationResult(min, max); } // 调用示例 CalculationResult res = findMinMax(new int[]{3, 1, 4, 2}); System.out.println("最小值:" + res.getMin() + ", 最大值:" + res.getMax());
Java 14+优化:使用
record
简化(自动生成Getter/构造方法)public record ResultRecord(int min, int max) {} // 单行定义 public ResultRecord getResult() { return new ResultRecord(10, 20); }
方案3:使用Pair
或Triple
(Apache Commons库)
适用场景:快速实现键值对返回(需引入外部库)。
- 添加Maven依赖:
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.13.0</version> </dependency>
- 代码实现:
import org.apache.commons.lang3.tuple.Pair;
public Pair<String, Integer> getUserInfo() {
String name = “张三”;
int age = 28;
return Pair.of(name, age); // 返回Pair对象
}
// 调用示例
Pair<String, Integer> user = getUserInfo();
System.out.println(“姓名:” + user.getLeft() + “, 年龄:” + user.getRight());
---
### 方案4:使用`AbstractMap.SimpleEntry`(JDK内置)
**适用场景**:轻量级键值对(无需引入外部库)。
```java
import java.util.AbstractMap;
public AbstractMap.SimpleEntry<String, Double> getProduct() {
String name = "笔记本电脑";
double price = 5999.99;
return new AbstractMap.SimpleEntry<>(name, price);
}
// 调用示例
var product = getProduct();
System.out.println("商品:" + product.getKey() + ", 价格:" + product.getValue());
方案5:通过参数传递容器(修改引用对象)
适用场景:需要返回多个值且避免创建新对象。
public void getCoordinates(int[] output) { // 通过数组引用修改值 output[0] = 100; // x坐标 output[1] = 200; // y坐标 } // 调用示例 int[] coords = new int[2]; // 准备容器 getCoordinates(coords); System.out.println("X=" + coords[0] + ", Y=" + coords[1]);
方法 | 优点 | 缺点 | 推荐场景 |
---|---|---|---|
自定义类 | 类型安全、代码可读性高 | 需额外定义类 | 长期复用、复杂业务逻辑 |
数组 | 简单快速 | 类型单一、易混淆索引含义 | 临时返回同类型数据 |
Apache Commons Pair | 无需定义新类、支持泛型 | 需引入外部依赖 | 快速开发、无持久化需求 |
AbstractMap.SimpleEntry | JDK内置、无外部依赖 | 语义上为键值对,灵活性受限 | 简单键值场景 |
容器参数 | 避免对象创建 | 破坏封装性、易出错 | 性能敏感场景 |
权威建议(符合E-A-T原则)
-
业务明确时优先使用自定义类
Java官方推荐面向对象设计,自定义类能清晰表达数据语义(如UserInfo
比Pair<String,Integer>
更易维护)。 -
临时数据用
record
(Java 14+)
简化不可变数据类的定义,自动实现equals()
/hashCode()
等方法,提升开发效率。 -
避免过度依赖第三方库
如项目未引入Apache Commons,使用SimpleEntry
或数组更轻量。 -
谨慎使用容器参数
虽然高效,但会破坏代码可读性,仅在性能关键路径使用。
引用说明:
- Oracle官方Java教程:Classes and Objects
- Apache Commons文档:Pair Class
- Java
record
特性:JEP 395(Java 16标准)
原创文章,发布者:酷盾叔,转转请注明出处:https://www.kd.cn/ask/43292.html