当前位置:网站首页>关于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密集型的任务,即包含大量计算的时候使用。
边栏推荐
猜你喜欢
[graduation project] QT from introduction to practice: realize imitation of QQ communication, which is also the last blog post in school.
MySQL字符串函数
Record the error reason: terminate called after throwing an instance
字节跳动开源GAN模型压缩框架,算力最高节省97.8%丨ICCV 2021
程序员定位解决问题方法论
Activiti directory (IV) inquiry agency / done, approved
Activit fragmented deadly pit
Shell_ 01_ data processing
Error occurred during initialization of VM Could not reserve enough space for object heap
~68 Icon Font introduction
随机推荐
控制转移指令
唯有学C不负众望 TOP1环境配置
唯有学C不负众望 TOP5 S1E8|S1E9:字符和字符串&&算术运算符
~85 transition
QT system learning series: 1.2 style sheet sub control lookup
唯有學C不負眾望 TOP5 S1E8|S1E9:字符和字符串&&算術運算符
In the command mode in the VI editor, delete the character usage at the current cursor__ Command.
Assembly language segment definition
100张图训练1小时,照片风格随意变,文末有Demo试玩|SIGGRAPH 2021
js垃圾回收机制和内存泄漏
(multiple methods, need to continue to see) 7-11 go deep into the tiger's Den
Shell_ 07_ Functions and regular expressions
8086 分段技术
唯有学C不负众望 TOP2 p1变量
[unsolved]7-14 calculation diagram
Mongodb在node中的使用
~81 long table
ByteDance 2022 school recruitment R & D advance approval publicity meeting, students' top 10 issues
汇编语言基础知识
Install docker under windows10 (through Oracle VM VirtualBox)