Stream之flatMap用法


记录一下flatMap的用法

 

个人理解是将流中的流合并

@Data
@AllArgsConstructor
@NoArgsConstructor
public class WhiteIp {

    //id
    private Integer id;

    //域名
    private String domain;

    //ip,多个用;分隔
    private String ipaddress;

    public static void main(String[] args) {
        WhiteIp w1 = new WhiteIp(1,"127.0.0.1","127.0.0.1;localhost");
        WhiteIp w2 = new WhiteIp(2,"www.baidu.com","192.123.123.1;192.111.111.1");
        WhiteIp w3 = new WhiteIp(3,"www.hao123.com","localhost");

        List<WhiteIp> list = new ArrayList<>();
        list.add(w1);
        list.add(w2);
        list.add(w3);

        List<String> result = list.stream()
                .map(WhiteIp::getIpaddress)
                .flatMap(v -> Arrays.stream(v.split(";")))
                .collect(Collectors.toList());

        System.out.println(result);
    }
}

 

结果

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM