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返回地址这个字符串流,就是一个展开操作,最后再收集所有人的地址流,统一返回字符串。
看运行效果:
文章来源:https://www.toymoban.com/news/detail-800457.html
文章来源地址https://www.toymoban.com/news/detail-800457.html
到了这里,关于java stream flatMap的使用及个人理解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!