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