当前位置:网站首页>关于Stream和Map的巧用
关于Stream和Map的巧用
2022-07-06 09:30: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密集型的任务,即包含大量计算的时候使用。
边栏推荐
- ~87 animation
- MySQL字符串函数
- ~68 Icon Font introduction
- 在 vi 编辑器中的命令模式下,删除当前光标处的字符使用 __ 命 令。
- The QT program compiled on CentOS lacks a MySQL driven solution
- Activiti directory (IV) inquiry agency / done, approved
- The 116 students spent three days reproducing the ByteDance internal real technology project
- Shell_ 00_ First meeting shell
- Activiti目录(四)查询代办/已办、审核
- MySQL optimization notes
猜你喜欢
程序员定位解决问题方法论
一个数10年工作经验的微服务架构老师的简历
Data config problem: the reference to entity 'useunicode' must end with ';' delimiter.
I'm "fixing movies" in ByteDance
Activiti directory (III) deployment process and initiation process
Shell_ 01_ data processing
~86m rabbit practice
Install docker under windows10 (through Oracle VM VirtualBox)
ByteDance 2022 school recruitment R & D advance approval publicity meeting, students' top 10 issues
Mongodb learning notes
随机推荐
Continue and break jump out of multiple loops
QT system learning series: 1.2 style sheet sub control lookup
Data config problem: the reference to entity 'useunicode' must end with ';' delimiter.
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]
@RestController、@Controller
~68 Icon Font introduction
[unsolved]7-14 calculation diagram
~76 sprite map
Eureka single machine construction
LeetCode 1637. The widest vertical area between two points without any point
Record the error reason
"One year after graduation, I won ACL best paper"
Solr word segmentation analysis
~82 style of table
yum install xxx报错
冯诺依曼体系结构
Shell_ 02_ Text three swordsman
程序员定位解决问题方法论
汇编语言段定义
MySQL数字函数