方法返回多个对象
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Apple {
private String name;
private Integer price;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Watermelon {
private String name;
private Integer price;
}
对象
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Fruit {
private Apple apple;
private Watermelon watermelon;
}
private Fruit object() {
return Fruit.builder()
.apple(Apple.builder().name("苹果").price(20).build())
.watermelon(Watermelon.builder().name("西瓜").price(25).build())
.build();
}
Map
private Map<String, Object> map() {
HashMap<String, Object> map = new HashMap<>();
map.put("apple", Apple.builder().name("苹果").price(20).build());
map.put("watermelon", Watermelon.builder().name("西瓜").price(25).build());
return map;
}
Pair
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
private Pair<Apple, Watermelon> pair() {
return ImmutablePair.of(Apple.builder().name("苹果").price(20).build(), Watermelon.builder().name("西瓜").price(25).build());
}
Tuple2
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>0.10.4</version>
</dependency>
private Tuple2<Apple, Watermelon> Tuple2() {
return Tuple.of(Apple.builder().name("苹果").price(20).build(), Watermelon.builder().name("西瓜").price(25).build());
}
思考
- 新见识了两个包,Apache的工具包commons-lang3和Java8函数式编程的增强包vavr
参考链接