当前位置:网站首页>關於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密集型的任務,即包含大量計算的時候使用。
边栏推荐
- Activiti directory (IV) inquiry agency / done, approved
- 服务器端渲染(SSR)和客户端渲染(CSR)的区别
- MySQL digital function
- When it comes to Google i/o, this is how ByteDance is applied to flutter
- 这116名学生,用3天时间复刻了字节跳动内部真实技术项目
- 冯诺依曼体系结构
- Fdog series (V): use QT to imitate QQ to realize login interface to main interface, function chapter.
- redux使用说明
- Fdog series (4): use the QT framework to imitate QQ to realize the login interface, interface chapter.
- Activit fragmented deadly pit
猜你喜欢

Shell_ 00_ First meeting shell

Solr new core

MySQL optimization notes

我走过最迷的路,是字节跳动程序员的脑回路
![[graduation project] QT from introduction to practice: realize imitation of QQ communication, which is also the last blog post in school.](/img/ef/2072aac5f85c7daf39174784dec7ee.jpg)
[graduation project] QT from introduction to practice: realize imitation of QQ communication, which is also the last blog post in school.

The most lost road I have ever walked through is the brain circuit of ByteDance programmers

Data transfer instruction

~87 animation

搭建flutter环境入坑集合
![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]](/img/a1/7dd41e75d6768159317b65e436030d.jpg)
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]
随机推荐
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]
Basic knowledge of assembly language
The "advertising maniacs" in this group of programmers turned Tiktok advertisements into ar games
Activiti directory (I) highlights
Shell_ 03_ environment variable
这群程序员中的「广告狂人」,把抖音广告做成了AR游戏
8086 分段技术
Yao BanZhi and his team came together, and the competition experts gathered together. What fairy programming competition is this?
[graduation project] QT from introduction to practice: realize imitation of QQ communication, which is also the last blog post in school.
8086 segmentation technology
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
Introduction to microservices
~85 transition
~76 sprite map
~74 JD top navigation bar exercise
~83 form introduction
Activiti directory (III) deployment process and initiation process
原型链继承
Fdog series (I): think about it. It's better to write a chat software. Then start with the imitation QQ registration page.
Eight part essay that everyone likes