当前位置:网站首页>One bite of Stream(9)
One bite of Stream(9)
2022-07-21 22:58:00 【程序员·小李】
Partitioning
其实partitioning属于groupingBy的一种特殊情况,那就是二分,分成两组。
Map<Boolean, List<Dish>> partitionedMenu = menu.stream()
.collect(partitioningBy(Dish::isVegetarian));
List<Dish> vegetarianDishes = partitionedMenu.get(true);
根据partitioningBy的条件,分为True和False各一组。
但是这种又可以直接使用filter来实现:
List<Dish> vegetarianDishes = menu.stream()
.filter(Dish::isVegetarian).collect(toList());
我们来看看更高级一些的用法:
1. 二次分组,先partitioningBy再groupingBy
Map<Boolean, Map<Dish.Type, List<Dish>>> vegetarianDishesByType = menu.stream()
.collect(partitioningBy(Dish::isVegetarian,
groupingBy(Dish::getType)));
相当于先进行了一次二分组,划分出是否素食,然后在素食和非素食内部再根据type进行分组。
2. 先二分,再求算最大值
Map<Boolean, Dish> mostCaloricPartitionedByVegetarian = menu.stream().collect(
partitioningBy(Dish::isVegetarian,
collectingAndThen(maxBy(comparingInt(Dish::getCalories)),
Optional::get)));
先进行了一次二分组,划分出是否素食,然后素食和非素食内部根据Calories进行比较,找到最大的Dish,然后取到。
在这里看,partitioningBy和groupingBy没有太大的区别,一个是多分组,另外一个是二分罢了。
一个实例,判断质数合数:
public boolean isPrime(int candidate) {
return IntStream.range(2, candidate).noneMatch(i -> candidate % i == 0);
}
实际上不需要计算这么多,只需要到平方根即可:
public boolean isPrime(int candidate) {
int candidateRoot = (int) Math.sqrt((double) candidate);
return IntStream.rangeClosed(2, candidateRoot)
.noneMatch(i -> candidate % i == 0);
}
根据我们上述方法,可以进行二分了:
public Map<Boolean, List<Integer>> partitionPrimes(int n) {
return IntStream.rangeClosed(2, n)
.boxed()
.collect(partitioningBy(candidate -> isPrime(candidate)));
}
附录:所有收集器
边栏推荐
- Digital supply chain management system for intelligent instrument industry: accelerating the transformation of enterprise intelligent supply chain platform
- mapState
- "New energy + energy storage" starts from the digital twin, Tupu will make smart power to the extreme
- Database operation using JDBC database operation
- Download picture function, full screen function, copy function
- 小程序项目总结
- 多重背包笔记
- Ioinputstream type
- Use of mapmutations in projects
- 2022/07/19----栈的压入、弹出序列;表示数值的字符串
猜你喜欢
Esp8266 analog input (ADC) detection problem
Red Hat 4.8 安装Oracle 11g报错:Error in invoking target ‘agent nmhs‘ of makefile
制药机械行业供应链协同管理系统:全链路数字化覆盖,实现产业供应链可视化
ECCV 2022 | generalized long tail classification based on invariant feature learning
B-end product manager learning: data analysis - Zhuge IO, Shence data
iTOP-RK3568开发板Debian系统功能测试-有线网测试
采购管控: 缩短采购周期,从源头上降本增效
:class在项目中的使用
Procurement control: shorten the procurement cycle, reduce costs and increase efficiency from the source
ClickHouse引擎之-MaterializeMYSQL
随机推荐
[where can I buy hcip question bank?]
Nightmare of concurrent programs -- data competition
[technology] introduction of uniapp u-charts partial demo
机器学习-集成学习
Experience sharing of system architecture designers in preparing for the exam: ensuring review time
(Applied intelligence-2022) transgait: gait recognition and ensemble transformer based on multimodality
el-input 失去焦点事件
: use of class in the project
【达梦数据库】监视器上查看集群机器状态
SeekTiger的Okaleido有大动作,生态通证STI会借此爆发?
[Li Kou] relative ranking
制药机械行业供应链协同管理系统:全链路数字化覆盖,实现产业供应链可视化
ACL-IJCAI-SIGIR顶级会议论文报告会(AIS 2022)笔记1:推荐系统
em与rem的区别
安科瑞智能小型断路器改如何选型?
Use of watch in projects
:style在项目中的使用
[ kitex 源码解读 ] Kitex 扩展性设计思路
《6》 BFC
老薛主机磁盘空间满了怎么办