Java 流式操作 Stream(6) flatMap合并去重
package com.lius.stream; import java.util.Arrays; import java.util.List; import java.util.stream.Stream; /** * 对集合元素的单词进行去重操作 */ public class streamTest5 { public static void main(String[] args) { //初始化数据 List<String> stringList = Arrays.asList("hello welcome","welcome world","world hello world","hello world hello"); //flatMap() 将多个流合并成一个流 //format_1 stringList.stream().map(str-> str.split(" ")).//将字符串通过“ ”分割成数组 flatMap(Stream::of).//通过Stream.of将数组生成一个Stream,然后通过flatMap将多个Stream流对象合并成一个Stream流 distinct().forEach(System.out::println);//去重输出 System.out.println("************************"); //format_2 stringList.stream().map(str->str.split(" ")).//将字符串通过“ ”分
Java 流式操作 Stream(6) flatMap合并去重
最新推荐文章于 2024-05-01 08:36:06 发布