当前位置:网站首页>On the clever use of stream and map
On the clever use of stream and map
2022-07-06 17:05:00 【Smart popcorn】
The title is like this
// hypothesis db There are two such tables in The data are 100w The final thing to return is Provice Tabular List aggregate If status by fasle Then don't go back How to be in java Level solution ?
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());
// add to
provinces.add(province);
provinceStatusList.add(provinceStatus);
}
System.out.println(" The size of the displayed list is :" + provinceStatusList.stream().filter(ProvinceStatus::getStatus).count());
}
public static void main(String[] args) {
ArrayList<Province> resList = new ArrayList<>();
System.out.println(" The number of final displays :" + resList.size());
}
}
@Data
class Province {
private Integer provinceId;
private String name;
}
@Data
class ProvinceStatus {
private Integer provinceId;
private Boolean status;
}
The answer is as follows , If you have better ideas, please leave a message !
public static void main(String[] args) {
// double for loop ? No use hash It's a good idea
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())) {
// Show these
resList.add(province);
}
}
long start2 = System.currentTimeMillis();
System.out.println(" when :" + (start2 - start1));
System.out.println(" The number of final displays :" + resList.size());
}
}
When there is a large amount of data , Parallel streams are slower , It should be because there is no calculation , After all, parallel flow is suitable for CPU Intensive tasks , That is, when a large number of calculations are involved .
边栏推荐
- 我走过最迷的路,是字节跳动程序员的脑回路
- ByteDance technical Interviewer: what kind of candidate do I want to pick most
- MySQL数字函数
- Jedis
- Introduction to microservices
- Activiti directory (III) deployment process and initiation process
- ~84 form supplement
- Fdog series (VI): use QT to communicate between the client and the client through the server (less information, recommended Collection)
- Yao BanZhi and his team came together, and the competition experts gathered together. What fairy programming competition is this?
- Monomer application concept
猜你喜欢
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
谢邀,人在工区,刚交代码,在下字节跳动实习生
Fdog series (4): use the QT framework to imitate QQ to realize the login interface, interface chapter.
Shell_ 05_ operator
was unable to send heartbeat
Shell_ 07_ Functions and regular expressions
Description of project structure configuration of idea
算数运算指令
字节跳动春招攻略:学长学姐笔经面经,还有出题人「锦囊」
Solr new core
随机推荐
"One year after graduation, I won ACL best paper"
在 vi 编辑器中的命令模式下,删除当前光标处的字符使用 __ 命 令。
Fdog series (V): use QT to imitate QQ to realize login interface to main interface, function chapter.
The 116 students spent three days reproducing the ByteDance internal real technology project
Mongodb在node中的使用
Notes on how the network is connected
Use of mongodb in node
字节跳动开源GAN模型压缩框架,算力最高节省97.8%丨ICCV 2021
汇编语言段定义
Only learning C can live up to expectations top5 S1E8 | S1E9: characters and strings & arithmetic operators
这群程序员中的「广告狂人」,把抖音广告做成了AR游戏
Koa Middleware
TypeScript基本操作
Some instructions on whether to call destructor when QT window closes and application stops
~83 form introduction
MySQL digital function
8086 memory
Go language uses the thrift protocol to realize the client and service end reports not enough arguments in call to oprot Writemessagebegin error resolution
汇编语言基础知识
Fdog series (I): think about it. It's better to write a chat software. Then start with the imitation QQ registration page.