当前位置:网站首页>關於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密集型的任務,即包含大量計算的時候使用。
边栏推荐
- arithmetic operation
- Redis standalone startup
- DS18B20數字溫度計系統設計
- The 116 students spent three days reproducing the ByteDance internal real technology project
- 唯有学C不负众望 TOP4 S1E6:数据类型
- 汇编课后作业
- Install docker under windows10 (through Oracle VM VirtualBox)
- 字节跳动2022校招研发提前批宣讲会,同学们最关心的10个问题
- Notes on how the network is connected
- Usage of insert() in vector
猜你喜欢

The 116 students spent three days reproducing the ByteDance internal real technology project

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

Alibaba cloud server docker installation mysql5.5

Shell_ 06_ Judgment and circulation

koa中间件

When it comes to Google i/o, this is how ByteDance is applied to flutter

~86m rabbit practice

Fdog series (4): use the QT framework to imitate QQ to realize the login interface, interface chapter.

Yao BanZhi and his team came together, and the competition experts gathered together. What fairy programming competition is this?

Log4j2 major vulnerabilities and Solutions
随机推荐
Activiti directory (I) highlights
was unable to send heartbeat
MySQL日期函数
Von Neumann architecture
Assembly language addressing mode
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]
Use of mongodb in node
Erlang installation
README. txt
Activiti目录(五)驳回、重新发起、取消流程
After idea installs the plug-in, restart the plug-in and disappear
Many papers on ByteDance have been selected into CVPR 2021, and the selected dry goods are here
搭建flutter环境入坑集合
Activiti目录(三)部署流程、发起流程
QT system learning series: 1.2 style sheet sub control lookup
唯有學C不負眾望 TOP5 S1E8|S1E9:字符和字符串&&算術運算符
8086 segmentation technology
数据传送指令
关于Stream和Map的巧用
吴军三部曲见识(七) 商业的本质