当前位置:网站首页>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 .
边栏推荐
- 这群程序员中的「广告狂人」,把抖音广告做成了AR游戏
- DOS function call
- 8086 CPU internal structure
- Log4j2 major vulnerabilities and Solutions
- Saw local status change event StatusChangeEvent [timestamp=1644048792587, current=DOWN, previous=UP]
- arithmetic operation
- JVM运行时数据区之程序计数器
- After the subscript is used to assign a value to the string type, the cout output variable is empty.
- QT system learning series: 1.2 style sheet sub control lookup
- 汇编语言基础知识
猜你喜欢
Description of project structure configuration of idea
Activiti目录(三)部署流程、发起流程
逻辑运算指令
Eight part essay that everyone likes
字节跳动多篇论文入选 CVPR 2021,精选干货都在这里了
Solr word segmentation analysis
The daemon thread starts redis and modifies the configuration file
~74 JD top navigation bar exercise
Notes on how the network is connected
The most lost road I have ever walked through is the brain circuit of ByteDance programmers
随机推荐
@RequestMapping、@GetMapping
Use of mongodb in node
Only learning C can live up to expectations top5 S1E8 | S1E9: characters and strings & arithmetic operators
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
Shell_ 01_ data processing
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
After idea installs the plug-in, restart the plug-in and disappear
js垃圾回收机制和内存泄漏
Restful style interface design
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
汇编课后作业
Eureka high availability
~68 Icon Font introduction
JVM运行时数据区之程序计数器
Activiti目录(四)查询代办/已办、审核
我走過最迷的路,是字節跳動程序員的腦回路
Fdog series (I): think about it. It's better to write a chat software. Then start with the imitation QQ registration page.
面试集锦库
DS18B20數字溫度計系統設計
In the command mode in the VI editor, delete the character usage at the current cursor__ Command.