@@ -602,60 +602,86 @@ public static <T> T requireNonNull(T obj) {
602602
603603`ofNullable` 方法和`of`方法唯一区别就是当 value 为 null 时,`ofNullable` 返回的是`EMPTY `,of 会抛出 `NullPointerException ` 异常。如果需要把 `NullPointerException ` 暴漏出来就用 `of`,否则就用 `ofNullable`。
604604
605- ### `map()`相关方法。
606605
607- ```java
608- /**
609- * 如果value为null,返回EMPTY,否则返回Optional封装的参数值
610- */
611- public < U > Optional<U > map(Function<? super T , ? extends U > mapper) {
612- Objects . requireNonNull(mapper);
613- if (! isPresent())
614- return empty();
615- else {
616- return Optional . ofNullable(mapper. apply(value));
617- }
618- }
619- /**
620- * 如果value为null,返回EMPTY,否则返回Optional封装的参数值,如果参数值返回null会抛 NullPointerException
621- */
622- public < U > Optional<U > flatMap(Function<? super T , Optional<U > > mapper) {
623- Objects . requireNonNull(mapper);
624- if (! isPresent())
625- return empty();
626- else {
627- return Objects . requireNonNull(mapper. apply(value));
628- }
629- }
606+ ** `map()` 和 `flatMap()` 有什么区别的?**
607+
608+ 先看一下`map`和`flatMap`在`Stream `中的不同。
609+ `map` 和 `flatMap` 都是将一个函数应用于集合中的每个元素,但不同的是`map`返回一个新的集合,`flatMap`是将每个元素都映射为一个集合,最后再将这个集合展平。
610+
611+ 在实际应用场景中,如果`map`返回的是数组,那么最后得到的是一个二维数组,使用`flatMap`就是为了将这个二维数组展平变成一个一维数组。
630612```
613+ public class MapAndFlatMapExample {
614+ public static void main (String [] args ) {
615+ List<String []> listOfArrays = Arrays . asList(
616+ new String []{" apple" , " banana" , " cherry" },
617+ new String []{" orange" , " grape" , " pear" },
618+ new String []{" kiwi" , " melon" , " pineapple" }
619+ );
631620
632- ** `map()` 和 `flatMap()` 有什么区别的?**
621+ List<String []> mapResult = listOfArrays. stream()
622+ .map(array - > Arrays . stream(array). map(String :: toUpperCase). toArray(String []:: new ))
623+ .collect(Collectors . toList());
633624
634- ** 1. 参数不一样,`map` 的参数上面看到过,`flatMap` 的参数是这样**
625+ System . out. println(" Using map:" );
626+ System . out. println(mapResult);
635627
636- ```java
637- class ZooFlat {
638- private DogFlat dog = new DogFlat ( );
628+ List< String > flatMapResult = listOfArrays . stream()
629+ .flatMap(array - > Arrays . stream(array) . map( String :: toUpperCase))
630+ .collect( Collectors . toList() );
639631
640- public DogFlat getDog () {
641- return dog;
642- }
632+ System . out. println(" Using flatMap:" );
633+ System . out. println(flatMapResult);
643634 }
644-
645- class DogFlat {
646- private int age = 1 ;
647- public Optional<Integer > getAge () {
648- return Optional . ofNullable(age);
649- }
650635}
651636
652- ZooFlat zooFlat = new ZooFlat ();
653- Optional . ofNullable(zooFlat). map(o - > o. getDog()). flatMap(d - > d. getAge()). ifPresent(age - >
654- System . out. println(age)
655- );
656637```
638+ 运行结果:
639+ ```
640+ Using map:
641+ [[APPLE , BANANA , CHERRY ], [ORANGE , GRAPE , PEAR ], [KIWI , MELON , PINEAPPLE ]]
642+
643+ Using flatMap:
644+ [APPLE , BANANA , CHERRY , ORANGE , GRAPE , PEAR , KIWI , MELON , PINEAPPLE ]
645+
646+ ```
647+ 最简单的理解就是`flatMap()`可以将`map()`的结果展开。
657648
658- ** 2. `flatMap()` 参数返回值如果是 null 会抛 `NullPointerException `,而 `map()` 返回`EMPTY `。**
649+ 在`Optional `里面,当使用`map()`时,如果映射函数返回的是一个普通值,它会将这个值包装在一个新的`Optional `中。而使用`flatMap`时,如果映射函数返回的是一个`Optional `,它会将这个返回的`Optional `展平,不再包装成嵌套的`Optional `。
650+
651+ 下面是一个对比的示例代码:
652+ ```
653+ public static void main(String [] args) {
654+ int userId = 1 ;
655+
656+ // 使用flatMap的代码
657+ String cityUsingFlatMap = getUserById(userId)
658+ .flatMap(OptionalExample :: getAddressByUser)
659+ .map(Address :: getCity)
660+ .orElse(" Unknown" );
661+
662+ System . out. println(" User's city using flatMap: " + cityUsingFlatMap);
663+
664+ // 不使用flatMap的代码
665+ Optional<Optional<Address > > optionalAddress = getUserById(userId)
666+ .map(OptionalExample :: getAddressByUser);
667+
668+ String cityWithoutFlatMap;
669+ if (optionalAddress. isPresent()) {
670+ Optional<Address > addressOptional = optionalAddress. get();
671+ if (addressOptional. isPresent()) {
672+ Address address = addressOptional. get();
673+ cityWithoutFlatMap = address. getCity();
674+ } else {
675+ cityWithoutFlatMap = " Unknown" ;
676+ }
677+ } else {
678+ cityWithoutFlatMap = " Unknown" ;
679+ }
680+
681+ System . out. println(" User's city without flatMap: " + cityWithoutFlatMap);
682+ }
683+ ```
684+ 在`Stream `和`Optional `中正确使用flatMap可以减少很多不必要的代码。
659685
660686### 判断 value 是否为 null
661687
0 commit comments