Stream流概述
开始管道
中间管道
终止管道
map 与 flatMap
map 与 flatMap 都是用于转换已有的元素为其它元素,区别点在于:
- map 必须是一对一的,即每个元素都只能转换为 1 个新的元素
- flatMap 可以是一对多的,即每个元素都可以转换为1个或者多个新的元素
比如:有一个字符串 ID 列表,现在需要将其转为 User 对象列表。可以使用 map 来实现:
/**
* 演示map的用途:一对一转换
*/
public void stringToIntMap() {
List<String> ids = Arrays.asList("205", "105", "308", "469", "627", "193", "111");
// 使用流操作
List<User> results = ids.stream()
.map(id -> {
User user = new User();
user.setId(id);
return user;
})
.collect(Collectors.toList());
System.out.println(results);
}
执行之后,会发现每一个元素都被转换为对应新的元素,但是前后总元素个数是一致的:
[User{id='205'},
User{id='105'},
User{id='308'},
User{id='469'},
User{id='627'},
User{id='193'},
User{id='111'}]
再比如:现有一个句子列表,需要将句子中每个单词都提取出来得到一个所有单词列表。
这种情况用 map 就搞不定了,需要 flatMap 上场了:
public void stringToIntFlatmap() {
List<String> sentences = Arrays.asList("hello world","Jia Gou Wu Dao");
// 使用流操作
List<String> results = sentences.stream()
.flatMap(sentence -> Arrays.stream(sentence.split(" ")))
.collect(Collectors.toList());
System.out.println(results);
}
执行结果如下,可以看到结果列表中元素个数是比原始列表元素个数要多的:
[hello, world, Jia, Gou, Wu, Dao]
peek 和 foreach 方法
peek 和 foreach,都可以用于对元素进行遍历然后逐个的进行处理。
但根据前面的介绍,peek 属于中间方法,而 foreach 属于终止方法。这也就意味着 peek 只能作为管道中途的一个处理步骤,而没法直接执行得到结果,其后面必须还要有其它终止操作的时候才会被执行;而 foreach 作为无返回值的终止方法,则可以直接执行相关操作。
public void testPeekAndforeach() {
List<String> sentences = Arrays.asList("hello world","Jia Gou Wu Dao");
// 演示点1: 仅peek操作,最终不会执行
System.out.println("----before peek----");
sentences.stream().peek(sentence -> System.out.println(sentence));
System.out.println("----after peek----");
// 演示点2: 仅foreach操作,最终会执行
System.out.println("----before foreach----");
sentences.stream().forEach(sentence -> System.out.println(sentence));
System.out.println("----after foreach----");
// 演示点3:peek操作后面增加终止操作,peek会执行
System.out.println("----before peek and count----");
sentences.stream().peek(sentence -> System.out.println(sentence)).count();
System.out.println("----after peek and count----");
}
输出结果可以看出,peek 独自调用时并没有被执行、但 peek 后面加上终止操作之后便可以被执行,而 foreach 可以直接被执行:
----before peek----
----after peek----
----before foreach----
hello world
Jia Gou Wu Dao
----after foreach----
----before peek and count----
hello world
Jia Gou Wu Dao
----after peek and count----
Stream常用场景实战
1.如何快速优雅格式化list中一个对象中的date类型的日期格式为string,并返回?
public PageResult<Map<String, Object>> findPageByCondition(QueryPageBean queryPageBean){
String queryString = queryPageBean.getQueryString();
Page<User> page = new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
QueryWrapper<User> qw = new QueryWrapper<>();
qw.like(StringUtils.isNotBlank(queryString),"username",queryString).or().like(StringUtils.isNotBlank(queryString)
,"tel",queryString);
userMapper.selectPage(page,qw);
List<User> userList = page.getRecords();
//快速把user类中的Date类型的createTime格式化为String并返回
List<Map<String, Object>> userMapList = userList.stream().map(user -> {
//这里引用了hutool中的BeanUtil
Map<String, Object> userMap = BeanUtil.beanToMap(user);
userMap.put("createTime", DateUtil.formatDate(user.getCreateTime()));
return userMap;
}).collect(Collectors.toList());
return new PageResult<>(ResultCode.SUCCESS.getCode(), Msg.SELECT_OK,page.getTotal(),userMapList);
}
<!--常用工具类-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.5.4</version>
</dependency>
2.如何快速把一个List<User>中存储的User转换为User的一个属性为key,对象本身为value的Map<String,User>集合?
List<User> userList = userMapper.findAll();
//第一种写法(这里我以id为key,故不会存在重复或者为空的情况)
Map<Integer,User> userMap = userList.stream().collect(Collectors.toMap(User::getId,user->user));
//第二种写法(这里我以id为key,故不会存在重复或者为空的情况)
Map<Integer,User> userMap = userList.stream().collect(Collectors.toMap(User::getId,Function.identity()));
//第三种写法(这里我以User的name为key,这时候先判空,再指定key重复时的处理规则-(k1,k2)->k2,这个合并函数的意思是:当两个name重复时,选第二个)
Map<Integer,User> userMap = userList.stream().collect(Collectors.toMap(User::getId, Function.identity(),(k1,k2)->k2));
//第四种写法(在第三种的基础上,可以指定Map<Integer,User>的具体实现)
Map<Integer,User> userMap = userList.stream().collect(Collectors.toMap(User::getId, Function.identity(),(k1,k2)->k2,LinkedHashMap::new));
//第五种写法(若name的值可能为空时,可以过滤掉可能为空的值)
Map<Integer,User> userMap = userList.stream().filter(user -> user.getName()!=null).collect(Collectors.toMap(User::getId,Function.identity()));
3.生成拼接字符串
将一个 List 或者数组中的值拼接到一个字符串里并以逗号分隔开,这个场景相信大家都不陌生吧?
public void testCollectJoinStrings() {
List<String> ids = Arrays.asList("205", "10", "308", "49", "627", "193", "111", "193");
String joinResult = ids.stream().collect(Collectors.joining(","));
System.out.println("拼接后:" + joinResult);
}
输出
拼接后:205,10,308,49,627,193,111,193
拓展:
//第三种写法分三种情况
//第1种:value值重复时,要求取一个
Map<Integer,User> userMap = userList.stream().collect(Collectors.toMap(User::getId, Function.identity(),(k1,k2)->k2));
//第2种:value值重复时,两个都要,返回的value为String类型
Map<Integer,String> userMap = userList.stream().collect(Collectors.toMap(User::getId, User::getName,(k1,k2)->k1+","+k2));
//第3种:value值重复时,两个都要,返回的value为集合类型
Map<String, List<String>> userMap = userList.stream().collect(Collectors.toMap(Person::getId, p -> {
List<String> getNameList = new ArrayList<>();
getNameList.add(p.getName());
return getNameList;
}, (
List<String> k1, List<String> k2) -> { k1.addAll(k2); return k1;
}
));