当前位置:网站首页>關於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密集型的任務,即包含大量計算的時候使用。
边栏推荐
- Activit fragmented deadly pit
- Activiti目录(五)驳回、重新发起、取消流程
- [graduation project] QT from introduction to practice: realize imitation of QQ communication, which is also the last blog post in school.
- Control transfer instruction
- Mongodb learning notes
- 字节跳动技术面试官现身说法:我最想pick什么样的候选人
- Assembly language addressing mode
- 「博士毕业一年,我拿下 ACL Best Paper」
- Activiti目录(一)重点介绍
- 关于Stream和Map的巧用
猜你喜欢

Activiti directory (V) reject, restart and cancel process

姚班智班齐上阵,竞赛高手聚一堂,这是什么神仙编程大赛?

Activit零零碎碎要人命的坑

Fdog series (I): think about it. It's better to write a chat software. Then start with the imitation QQ registration page.

Shell_ 03_ environment variable

MySQL date function

~76 sprite map

Redis standalone startup

~86m rabbit practice

MySQL digital function
随机推荐
Erlang installation
姚班智班齐上阵,竞赛高手聚一堂,这是什么神仙编程大赛?
The most lost road I have ever walked through is the brain circuit of ByteDance programmers
字节跳动技术面试官现身说法:我最想pick什么样的候选人
数据传送指令
DOS 功能调用
Usage of insert() in vector
~82 style of table
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
The QT program compiled on CentOS lacks a MySQL driven solution
Log4j2 major vulnerabilities and Solutions
~84 form supplement
@RestController、@Controller
DS18B20数字温度计系统设计
Use of mongodb in node
MySQL字符串函数
~68 Icon Font introduction
TCP的三次握手和四次挥手
MySQL date function
Solr new core