当前位置:网站首页>關於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密集型的任務,即包含大量計算的時候使用。
边栏推荐
- ~72 horizontal and vertical alignment of text
- MySQL string function
- 服务器端渲染(SSR)和客户端渲染(CSR)的区别
- Assembly language addressing mode
- Error occurred during initialization of VM Could not reserve enough space for object heap
- 在 vi 编辑器中的命令模式下,删除当前光标处的字符使用 __ 命 令。
- Activiti directory (IV) inquiry agency / done, approved
- Shell_ 02_ Text three swordsman
- In the command mode in the VI editor, delete the character usage at the current cursor__ Command.
- Control transfer instruction
猜你喜欢
[graduation project] QT from introduction to practice: realize imitation of QQ communication, which is also the last blog post in school.
Assembly language addressing mode
ByteDance open source Gan model compression framework, saving up to 97.8% of computing power - iccv 2021
Shell_ 01_ data processing
Install docker under windows10 (through Oracle VM VirtualBox)
ByteDance 2022 school recruitment R & D advance approval publicity meeting, students' top 10 issues
~83 form introduction
MySQL字符串函数
汇编语言寻址方式
TCP的三次握手和四次挥手
随机推荐
Eureka single machine construction
The most lost road I have ever walked through is the brain circuit of ByteDance programmers
Saw local status change event StatusChangeEvent [timestamp=1644048792587, current=DOWN, previous=UP]
Alibaba cloud server docker installation mysql5.5
Fdog series (4): use the QT framework to imitate QQ to realize the login interface, interface chapter.
我走過最迷的路,是字節跳動程序員的腦回路
Only learning C can live up to expectations top5 S1E8 | S1E9: characters and strings & arithmetic operators
~74 JD top navigation bar exercise
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
亮相Google I/O,字节跳动是这样应用Flutter的
[unsolved]7-14 calculation diagram
When it comes to Google i/o, this is how ByteDance is applied to flutter
谢邀,人在工区,刚交代码,在下字节跳动实习生
IDEA断点调试技巧,多张动图包教包会。
The "advertising maniacs" in this group of programmers turned Tiktok advertisements into ar games
吴军三部曲见识(七) 商业的本质
MySQL digital function
Activiti directory (V) reject, restart and cancel process
redux使用说明
8086 CPU internal structure