当前位置:网站首页>關於Stream和Map的巧用
關於Stream和Map的巧用
2022-07-06 17:04: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密集型的任務,即包含大量計算的時候使用。
边栏推荐
- 唯有学C不负众望 TOP4 S1E6:数据类型
- Koa Middleware
- 程序员定位解决问题方法论
- 唯有學C不負眾望 TOP5 S1E8|S1E9:字符和字符串&&算術運算符
- When it comes to Google i/o, this is how ByteDance is applied to flutter
- Activit零零碎碎要人命的坑
- Error occurred during initialization of VM Could not reserve enough space for object heap
- Shell_ 04_ Shell script
- The daemon thread starts redis and modifies the configuration file
- MySQL optimization notes
猜你喜欢

搭建flutter环境入坑集合

Train 100 pictures for 1 hour, and the style of the photos changes at will. There is a demo at the end of the article | siggraph 2021

Data transfer instruction

Error occurred during initialization of VM Could not reserve enough space for object heap

MySQL string function

IDEA断点调试技巧,多张动图包教包会。

逻辑运算指令

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

Log4j2 major vulnerabilities and Solutions

Solr new core
随机推荐
Yum install XXX reports an error
Thank you for your invitation. I'm in the work area. I just handed in the code. I'm an intern in the next ByteDance
yum install xxx报错
Log4j2 major vulnerabilities and Solutions
Solr standalone installation
Use of mongodb in node
Restful style interface design
Shell_ 07_ Functions and regular expressions
Train 100 pictures for 1 hour, and the style of the photos changes at will. There is a demo at the end of the article | siggraph 2021
字节跳动海外技术团队再夺冠:高清视频编码已获17项第一
The "advertising maniacs" in this group of programmers turned Tiktok advertisements into ar games
~69 other ways to use icon fonts
Activiti目录(一)重点介绍
High performance mysql (Third Edition) notes
Ce n'est qu'en apprenant que c est à la hauteur des attentes Top5 s1e8 | s1e9: caractères et chaînes & opérateurs arithmétiques
~83 form introduction
字节跳动多篇论文入选 CVPR 2021,精选干货都在这里了
Shell_ 01_ data processing
字节跳动2022校招研发提前批宣讲会,同学们最关心的10个问题
"One year after graduation, I won ACL best paper"