当前位置:网站首页>关于Stream和Map的巧用
关于Stream和Map的巧用
2022-07-06 09:30:00 【机智的爆爆哥】
题目是这样的
//假设db中有两个这样的表 数据有100w 最终要返回的是Provice表的List集合 如果status为fasle 则不返回 如何在java层面解决呢?
public class Mydemo {
static ArrayList<Province> provinces = new ArrayList<>();
static ArrayList<ProvinceStatus> provinceStatusList = new ArrayList<>();
static {
for (int i = 0; i < 10000; i++) {
Province province = new Province();
ProvinceStatus provinceStatus = new ProvinceStatus();
province.setName(UUID.randomUUID().toString());
province.setProvinceId(i);
provinceStatus.setProvinceId(i);
provinceStatus.setStatus(new Random().nextBoolean());
//添加
provinces.add(province);
provinceStatusList.add(provinceStatus);
}
System.out.println("显示的列表大小为:" + provinceStatusList.stream().filter(ProvinceStatus::getStatus).count());
}
public static void main(String[] args) {
ArrayList<Province> resList = new ArrayList<>();
System.out.println("最终显示的个数:" + resList.size());
}
}
@Data
class Province {
private Integer provinceId;
private String name;
}
@Data
class ProvinceStatus {
private Integer provinceId;
private Boolean status;
}
答案如下,有更好的想法请留言哦!
public static void main(String[] args) {
//双重for循环? 不 用hash是个好办法
long start1 = System.currentTimeMillis();
System.out.println();
Map<Integer, Boolean> map = provinceStatusList.stream()
.collect(Collectors.toMap(ProvinceStatus::getProvinceId, ProvinceStatus::getStatus));
ArrayList<Province> resList = new ArrayList<>();
for (Province province : Mydemo.provinces) {
if (map.get(province.getProvinceId())) {
//显示这些
resList.add(province);
}
}
long start2 = System.currentTimeMillis();
System.out.println("用时:" + (start2 - start1));
System.out.println("最终显示的个数:" + resList.size());
}
}
实测在数据量大的时候,并行流反而速度更慢,应该是因为没有经过什么计算,毕竟并行流适合CPU密集型的任务,即包含大量计算的时候使用。
边栏推荐
猜你喜欢

MySQL optimization notes

The daemon thread starts redis and modifies the configuration file

~87 animation
![[unsolved]7-14 calculation diagram](/img/33/39802c0106f2bef4b9979e5ae12b83.jpg)
[unsolved]7-14 calculation diagram

~68 Icon Font introduction

逻辑运算指令

Many papers on ByteDance have been selected into CVPR 2021, and the selected dry goods are here

~74 JD top navigation bar exercise

ByteDance open source Gan model compression framework, saving up to 97.8% of computing power - iccv 2021

字节跳动技术面试官现身说法:我最想pick什么样的候选人
随机推荐
Alibaba cloud server builds SVN version Library
Go language uses the thrift protocol to realize the client and service end reports not enough arguments in call to oprot Writemessagebegin error resolution
Activiti directory (III) deployment process and initiation process
Activit fragmented deadly pit
程序员定位解决问题方法论
数据传送指令
Fdog series (VI): use QT to communicate between the client and the client through the server (less information, recommended Collection)
After idea installs the plug-in, restart the plug-in and disappear
唯有学C不负众望 TOP3 Demo练习
DOS function call
字节跳动开源GAN模型压缩框架,算力最高节省97.8%丨ICCV 2021
~75 background
~74 JD top navigation bar exercise
~77 linear gradient
字节跳动技术面试官现身说法:我最想pick什么样的候选人
Shell_ 07_ Functions and regular expressions
MySQL字符串函数
唯有学C不负众望 TOP2 p1变量
redux使用说明
Record the error reason: terminate called after throwing an instance