1.我认为用简单朴素的理解和使用,是对工具最好的诠释。java jdk8开始提供了stream流,方便我更高效的操作集合和编写代码。其中flatmap流中间操作api,我认为简单来说是对“集合中的集合的操作和展开”。比如说,一个对象集合里面的每个对象还有个集合对象。这时,我们如果需要对这个集合的所有对象的集合对象进行操作,那么flatmap就是一个不错的选择。
2.接下来举个例子,比如有一群人的每个人都有多套房子,我想把这群人的所有房子的地址都去重的统计出来。
首先person类定义开整
class Person{ private Long id; private Integer age; private String name; private String idCard; private ListlistHouse; 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 getListHouse() { return listHouse; } public void setListHouse(List 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"); ArrayListp1hList = 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 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 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 pList = new ArrayList<>(); pList.add(p1); pList.add(p2); pList.add(p3); List strings = pList.stream().flatMap(p -> { Stream stream = p.getListHouse().stream().map(bigHouse::getAddress).distinct(); return stream; }).distinct().collect(Collectors.toList()); strings.forEach(System.out::print); ArrayList list = CollUtil.newArrayList("ABC", "DEF", "GHI"); List collect = list.stream().flatMap(ele -> Stream.of(ele.split(""))).collect(Collectors.toList()); collect.forEach(System.out::println); // List list = Arrays.asList("l,y,w", "8,6,8"); // List 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"); ArrayListp1hList = 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 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 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 pList = new ArrayList<>(); pList.add(p1); pList.add(p2); pList.add(p3); List strings = pList.stream().flatMap(p -> { Stream stream = p.getListHouse().stream().map(bigHouse::getAddress).distinct(); return stream; }).distinct().collect(Collectors.toList()); strings.forEach(System.out::print); ArrayList list = CollUtil.newArrayList("ABC", "DEF", "GHI"); List collect = list.stream().flatMap(ele -> Stream.of(ele.split(""))).collect(Collectors.toList()); collect.forEach(System.out::println); // List list = Arrays.asList("l,y,w", "8,6,8"); // List 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 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 getListHouse() { return listHouse; } public void setListHouse(List 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流
核心实现部分:
Liststrings = pList.stream().flatMap(p -> { Stream stream = p.getListHouse().stream().map(bigHouse::getAddress); return stream; }).distinct().collect(Collectors.toList());
这个p就是有人对象,我们把每个人对象里面的房子的集合通过map返回地址这个字符串流,就是一个展开操作,最后再收集所有人的地址流,统一返回字符串。
看运行效果: