当前位置:网站首页>關於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密集型的任務,即包含大量計算的時候使用。
边栏推荐
猜你喜欢
~69 other ways to use icon fonts
Solr word segmentation analysis
Resume of a microservice architecture teacher with 10 years of work experience
这116名学生,用3天时间复刻了字节跳动内部真实技术项目
~Introduction to form 80
Many papers on ByteDance have been selected into CVPR 2021, and the selected dry goods are here
字节跳动海外技术团队再夺冠:高清视频编码已获17项第一
High performance mysql (Third Edition) notes
Notes on how the network is connected
Shell_ 04_ Shell script
随机推荐
MySQL optimization notes
Logical operation instruction
MySQL digital function
Shell_ 07_ Functions and regular expressions
~79 Movie card exercise
Usage of insert() in vector
这群程序员中的「广告狂人」,把抖音广告做成了AR游戏
Description of project structure configuration of idea
Fdog series (4): use the QT framework to imitate QQ to realize the login interface, interface chapter.
100张图训练1小时,照片风格随意变,文末有Demo试玩|SIGGRAPH 2021
Which is more important for programming, practice or theory [there are some things recently, I don't have time to write an article, so I'll post an article on hydrology, and I'll fill in later]
Some instructions on whether to call destructor when QT window closes and application stops
汇编语言基础知识
Eureka single machine construction
字节跳动2022校招研发提前批宣讲会,同学们最关心的10个问题
How to generate six digit verification code
Install docker under windows10 (through Oracle VM VirtualBox)
[graduation project] QT from introduction to practice: realize imitation of QQ communication, which is also the last blog post in school.
Full record of ByteDance technology newcomer training: a guide to the new growth of school recruitment
The 116 students spent three days reproducing the ByteDance internal real technology project