如何使用 Java 的可选功能
How to use Java's Optional feature
我有一个用例,想知道如何使用 Java 可选来最好地编写它,以便我可以减少空检查?
// To get first residence place for family places or empty string if null
// What if familyPlaces is null?
public String getFirstResidencePlace(FamilyPlaces familyPlaces) {
// What if getResidencePlaces() returns null in list?
List<Place> places = familyPlaces.getResidencePlaces();
for (Place place : places) {
if (place.getResidencePlaces() != null &&
!place.getResidencePlaces().isEmpty()) {
return place.getResidencePlaces().toLowerCase();
}
}
return "";
}
这可以使用 Optional
来真正处理,确保第一个调用的方法产生 null
值 (familyPlaces.getResidencePlaces()
):
Java 9+
Optional.ofNullable(familyPlaces.getResidencePlaces()) // Optional<List<Place>>
.stream() // Stream<List<Place>>
.flatMap(list -> list.stream()
.map(Place::getResidencePlaces)) // Stream<String>
.filter(res -> res != null && !res.isEmpty()) // Stream<String>
.map(String::toLowerCase) // Stream<String>
.findFirst() // the first String found
.orElse(""); // default return value
注意平面映射等于:
.flatMap(List::stream) // from Stream<List<Place>> to Stream<Place>
.map(Place::getResidencePlaces) // from Stream<Place> to Stream<String>
Java 8:
Optional.ofNullable(familyPlaces.getResidencePlaces()) // Optional<List<Place>>
.orElse(Collections.emptyList()) // List<Place>
.stream() // Stream<Place>
.map(Place::getResidencePlaces) // Stream<String>
.filter(res -> res != null && !res.isEmpty()) // Stream<String>
.map(String::toLowerCase) // Stream<String>
.findFirst() // the first String found
.orElse(""); // default return value
这是一种替代方法,它通过调用 Optional
本身的 map()
和 flatMap()
方法来更好地利用 Java 的 Optional
功能而不是 Place
的 Stream
。
已对以下对象进行空检查。
familyPlaces
getResidencePlaces
列表
getResidencePlaces
列表中的元素
getResidencePlaces
字符串
String firstPlace = Optional.ofNullable(familyPlaces)
.map(FamilyPlaces::getResidencePlaces)
.flatMap(places -> places.stream().filter(Objects::nonNull).findFirst())
.map(Place::getResidencePlaces)
.map(String::toLowerCase)
.orElse("");
这适用于 Java 8.
我有一个用例,想知道如何使用 Java 可选来最好地编写它,以便我可以减少空检查?
// To get first residence place for family places or empty string if null
// What if familyPlaces is null?
public String getFirstResidencePlace(FamilyPlaces familyPlaces) {
// What if getResidencePlaces() returns null in list?
List<Place> places = familyPlaces.getResidencePlaces();
for (Place place : places) {
if (place.getResidencePlaces() != null &&
!place.getResidencePlaces().isEmpty()) {
return place.getResidencePlaces().toLowerCase();
}
}
return "";
}
这可以使用 Optional
来真正处理,确保第一个调用的方法产生 null
值 (familyPlaces.getResidencePlaces()
):
Java 9+
Optional.ofNullable(familyPlaces.getResidencePlaces()) // Optional<List<Place>>
.stream() // Stream<List<Place>>
.flatMap(list -> list.stream()
.map(Place::getResidencePlaces)) // Stream<String>
.filter(res -> res != null && !res.isEmpty()) // Stream<String>
.map(String::toLowerCase) // Stream<String>
.findFirst() // the first String found
.orElse(""); // default return value
注意平面映射等于:
.flatMap(List::stream) // from Stream<List<Place>> to Stream<Place>
.map(Place::getResidencePlaces) // from Stream<Place> to Stream<String>
Java 8:
Optional.ofNullable(familyPlaces.getResidencePlaces()) // Optional<List<Place>>
.orElse(Collections.emptyList()) // List<Place>
.stream() // Stream<Place>
.map(Place::getResidencePlaces) // Stream<String>
.filter(res -> res != null && !res.isEmpty()) // Stream<String>
.map(String::toLowerCase) // Stream<String>
.findFirst() // the first String found
.orElse(""); // default return value
这是一种替代方法,它通过调用 Optional
本身的 map()
和 flatMap()
方法来更好地利用 Java 的 Optional
功能而不是 Place
的 Stream
。
已对以下对象进行空检查。
familyPlaces
getResidencePlaces
列表getResidencePlaces
列表中的元素getResidencePlaces
字符串String firstPlace = Optional.ofNullable(familyPlaces) .map(FamilyPlaces::getResidencePlaces) .flatMap(places -> places.stream().filter(Objects::nonNull).findFirst()) .map(Place::getResidencePlaces) .map(String::toLowerCase) .orElse("");
这适用于 Java 8.