java stream flatMap的使用及个人理解

1.我认为用简单朴素的理解和使用,是对工具最好的诠释。java jdk8开始提供了stream流,方便我更高效的操作集合和编写代码。其中flatmap流中间操作api,我认为简单来说是对“集合中的集合的操作和展开”。比如说,一个对象集合里面的每个对象还有个集合对象。这时,我们如果需要对这个集合的所有对象的集合对象进行操作,那么flatmap就是一个不错的选择。

2.接下来举个例子,比如有一群人的每个人都有多套房子,我想把这群人的所有房子的地址都去重的统计出来。

首先person类定义开整

class Person{
    private Long id;
    private Integer age;
    private String name;
    private String idCard;
    private List<bigHouse> listHouse;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIdCard() {
        return idCard;
    }

    public void setIdCard(String idCard) {
        this.idCard = idCard;
    }

    public List<bigHouse> getListHouse() {
        return listHouse;
    }

    public void setListHouse(List<bigHouse> listHouse) {
        this.listHouse = listHouse;
    }
}

再定义一个房子bigHouse类

class bigHouse{

    private String address;
    private BigDecimal price;
    private Integer useAge;

    public bigHouse(String address, BigDecimal price, Integer useAge) {
        this.address = address;
        this.price = price;
        this.useAge = useAge;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public Integer getUseAge() {
        return useAge;
    }

    public void setUseAge(Integer useAge) {
        this.useAge = useAge;
    }
}

定义个main方法进行测试

public static void main(String[] args) {
        //数据填充
        Person p1 = new Person();
        p1.setId(1L);
        p1.setAge(22);
        p1.setName("p1");
        p1.setIdCard("1234");
        ArrayList<bigHouse> p1hList = new ArrayList<>();
        bigHouse p1h1 = new bigHouse("重庆",new BigDecimal(23),5);
        bigHouse p1h2 = new bigHouse("开州",new BigDecimal(62),3);
        bigHouse p1h3 = new bigHouse("南雅",new BigDecimal(6),3);
        p1hList.add(p1h1);
        p1hList.add(p1h2);
        p1hList.add(p1h3);
        p1.setListHouse(p1hList);
        Person p2 = new Person();
        p2.setId(2L);
        p2.setAge(23);
        p2.setName("p2");
        p2.setIdCard("1235");
        ArrayList<bigHouse> p2hList = new ArrayList<>();
        bigHouse p2h1 = new bigHouse("四川",new BigDecimal(223),7);
        bigHouse p2h2 = new bigHouse("重庆",new BigDecimal(123),9);
        bigHouse p2h3 = new bigHouse("南雅",new BigDecimal(6),3);
        p2hList.add(p2h1);
        p2hList.add(p2h2);
        p2hList.add(p2h3);
        p2.setListHouse(p2hList);
        Person p3 = new Person();
        p3.setId(3L);
        p3.setAge(24);
        p3.setName("p3");
        p3.setIdCard("1236");
        ArrayList<bigHouse> p3hList = new ArrayList<>();
        bigHouse p3h1 = new bigHouse("江苏",new BigDecimal(231),17);
        bigHouse p3h2 = new bigHouse("无锡",new BigDecimal(163),8);
        bigHouse p3h3 = new bigHouse("达州",new BigDecimal(8),3);
        p3hList.add(p3h1);
        p3hList.add(p3h2);
        p3hList.add(p3h3);
        p3.setListHouse(p2hList);
        //flatMap handle collection zhong collection
        //需求:提取所有的人房子的地址,并且去除
        List<Person> pList = new ArrayList<>();
        pList.add(p1);
        pList.add(p2);
        pList.add(p3);
        List<String> strings = pList.stream().flatMap(p -> {
            Stream<String> stream = p.getListHouse().stream().map(bigHouse::getAddress).distinct();
            return stream;
        }).distinct().collect(Collectors.toList());
        strings.forEach(System.out::print);
        ArrayList<String> list = CollUtil.newArrayList("ABC", "DEF", "GHI");
        List<String> collect = list.stream().flatMap(ele -> Stream.of(ele.split(""))).collect(Collectors.toList());
        collect.forEach(System.out::println);
//        List<String> list = Arrays.asList("l,y,w", "8,6,8");
//        List<String> collect = list.stream().flatMap(s -> {
//            String[] split = s.split(",");
//            new HashMap<>();
//            return Arrays.stream(split);
//        }).collect(Collectors.toList());
//        collect.forEach(System.out::println);

    }

完整代码如下:

package com.conpany.project.stream;

import cn.hutool.core.collection.CollUtil;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class FlatMap {

    public static void main(String[] args) {
        //数据填充
        Person p1 = new Person();
        p1.setId(1L);
        p1.setAge(22);
        p1.setName("p1");
        p1.setIdCard("1234");
        ArrayList<bigHouse> p1hList = new ArrayList<>();
        bigHouse p1h1 = new bigHouse("重庆",new BigDecimal(23),5);
        bigHouse p1h2 = new bigHouse("开州",new BigDecimal(62),3);
        bigHouse p1h3 = new bigHouse("南雅",new BigDecimal(6),3);
        p1hList.add(p1h1);
        p1hList.add(p1h2);
        p1hList.add(p1h3);
        p1.setListHouse(p1hList);
        Person p2 = new Person();
        p2.setId(2L);
        p2.setAge(23);
        p2.setName("p2");
        p2.setIdCard("1235");
        ArrayList<bigHouse> p2hList = new ArrayList<>();
        bigHouse p2h1 = new bigHouse("四川",new BigDecimal(223),7);
        bigHouse p2h2 = new bigHouse("重庆",new BigDecimal(123),9);
        bigHouse p2h3 = new bigHouse("南雅",new BigDecimal(6),3);
        p2hList.add(p2h1);
        p2hList.add(p2h2);
        p2hList.add(p2h3);
        p2.setListHouse(p2hList);
        Person p3 = new Person();
        p3.setId(3L);
        p3.setAge(24);
        p3.setName("p3");
        p3.setIdCard("1236");
        ArrayList<bigHouse> p3hList = new ArrayList<>();
        bigHouse p3h1 = new bigHouse("江苏",new BigDecimal(231),17);
        bigHouse p3h2 = new bigHouse("无锡",new BigDecimal(163),8);
        bigHouse p3h3 = new bigHouse("达州",new BigDecimal(8),3);
        p3hList.add(p3h1);
        p3hList.add(p3h2);
        p3hList.add(p3h3);
        p3.setListHouse(p2hList);
        //flatMap handle collection zhong collection
        //需求:提取所有的人房子的地址,并且去重
        List<Person> pList = new ArrayList<>();
        pList.add(p1);
        pList.add(p2);
        pList.add(p3);
        List<String> strings = pList.stream().flatMap(p -> {
            Stream<String> stream = p.getListHouse().stream().map(bigHouse::getAddress).distinct();
            return stream;
        }).distinct().collect(Collectors.toList());
        strings.forEach(System.out::print);
        ArrayList<String> list = CollUtil.newArrayList("ABC", "DEF", "GHI");
        List<String> collect = list.stream().flatMap(ele -> Stream.of(ele.split(""))).collect(Collectors.toList());
        collect.forEach(System.out::println);
//        List<String> list = Arrays.asList("l,y,w", "8,6,8");
//        List<String> collect = list.stream().flatMap(s -> {
//            String[] split = s.split(",");
//            new HashMap<>();
//            return Arrays.stream(split);
//        }).collect(Collectors.toList());
//        collect.forEach(System.out::println);

    }

}
class Person{
    private Long id;
    private Integer age;
    private String name;
    private String idCard;
    private List<bigHouse> listHouse;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIdCard() {
        return idCard;
    }

    public void setIdCard(String idCard) {
        this.idCard = idCard;
    }

    public List<bigHouse> getListHouse() {
        return listHouse;
    }

    public void setListHouse(List<bigHouse> listHouse) {
        this.listHouse = listHouse;
    }
}
class bigHouse{

    private String address;
    private BigDecimal price;
    private Integer useAge;

    public bigHouse(String address, BigDecimal price, Integer useAge) {
        this.address = address;
        this.price = price;
        this.useAge = useAge;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public Integer getUseAge() {
        return useAge;
    }

    public void setUseAge(Integer useAge) {
        this.useAge = useAge;
    }
}

分析:flatmap看源码需要返回一个stream流

 核心实现部分:

List<String> strings = pList.stream().flatMap(p -> {
    Stream<String> stream = p.getListHouse().stream().map(bigHouse::getAddress);
    return stream;
}).distinct().collect(Collectors.toList());

这个p就是有人对象,我们把每个人对象里面的房子的集合通过map返回地址这个字符串流,就是一个展开操作,最后再收集所有人的地址流,统一返回字符串。

看运行效果:

 

  • 8
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java StreamJava 8中的一个新特性,它提供了一种非常强大的功能,能够让我们以一种更简单、更简洁的方式来处理集合数据。Java Stream可以被看作是函数式编程的一个重要组成部分,可以非常方便地对集合进行处理和转换。 在Java Stream中,我们可以使用flatMap()方法来合并多个列表。flatMap()方法接受一个函数作为参数,这个函数将一个元素转换为一个流,然后将这些流进行扁平化,最终将它们合并成一个流。具体操作步骤如下: 1.将多个列表转化为一个流。 2.对这个流进行flatMap()操作,将其中的元素扁平化为一个个字符串,也就是我们所说的字符串流。 3.然后再将这个流转化回一个列表。 示例代码如下: ``` List<List<Integer>> lists = new ArrayList<>(); lists.add(Arrays.asList(1, 2, 3)); lists.add(Arrays.asList(4, 5, 6)); lists.add(Arrays.asList(7, 8, 9)); List<Integer> result = lists.stream() .flatMap(Collection::stream) .collect(Collectors.toList()); ``` 在上面的代码中,我们首先定义了三个列表,然后通过stream()方法将它们转化为一个流。接着,我们使用flatMap()方法将其中的元素扁平化为一个Integer类型的流,然后再将这个流转化回一个列表。 这样,我们就成功地将多个列表合并成了一个列表,并且代码也非常简洁和易于理解。这种方式不仅能够提高我们的代码效率和可读性,而且还能够使我们的代码更加容易维护和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值