当前位置:网站首页>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 .
边栏推荐
- Notes on how the network is connected
- The QT program compiled on CentOS lacks a MySQL driven solution
- Yum install XXX reports an error
- 唯有学C不负众望 TOP2 p1变量
- Data transfer instruction
- 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
- Activiti目录(一)重点介绍
- Shell_ 02_ Text three swordsman
- ~79 Movie card exercise
- 字节跳动开源GAN模型压缩框架,算力最高节省97.8%丨ICCV 2021
猜你喜欢
随机推荐
~86m rabbit practice
ByteDance technical Interviewer: what kind of candidate do I want to pick most
TCP的三次握手和四次挥手
8086 segmentation technology
DOS function call
Activiti目录(三)部署流程、发起流程
MySQL date function
8086 CPU 内部结构
逻辑运算指令
Alibaba cloud server builds SVN version Library
~83 form introduction
MySQL日期函数
~75 background
High performance mysql (Third Edition) notes
TCP's three handshakes and four waves
Some instructions on whether to call destructor when QT window closes and application stops
Assembly language segment definition
~82 style of table
Full record of ByteDance technology newcomer training: a guide to the new growth of school recruitment
Solr standalone installation









