/** * flatMap */ public static void flatMapString() { List<PersonModel> data = Data.getData(); //返回类型不一样 List<String> list = data.stream() .flatMap(personModel -> Arrays.stream(personModel.getName().split(""))) .collect(Collectors.toList()); List<Stream<String>> collect = data.stream() .map(personModel -> Arrays.stream(personModel.getName().split(""))) .collect(Collectors.toList()); //用map实现 List<String> collect1 = data.stream() .map(personModel -> personModel.getName().split("")) .flatMap(Arrays::stream) .collect(Collectors.toList()); //另一种方式实现 List<String> collect2 = data.stream() .map(personModel -> personModel.getName().split("")) .flatMap(str -> Arrays.asList(str).stream()) .collect(Collectors.toList()); }
java8-flatMap
最新推荐文章于 2024-07-24 09:18:36 发布