当前位置:网站首页>双重for循环优化
双重for循环优化
2022-07-28 22:06:00 【Mr.杨先森】
public class test {
public static void main(String[] args) throws InterruptedException {
List<Member> list1 = new ArrayList<>();
List<Member> list2 = new ArrayList<>();
for (int i = 0; i < 30000; i++) {
Date date = new Date();
//每个元素有名字
list1.add(new Member((i + 1), "技术客", (i + 1), date));
if (i % 2 == 0) {
//对2取模无名字
list2.add(new Member((i + 1), null, (i + 1), date));
}
}
//双for循环嵌套测试
long s1 = System.currentTimeMillis();
int forNumber = 0;
for (Member m2 : list2) {
if (m2.getName() == null) {
for (Member m1 : list1) {
if (m1.getId().intValue() == m2.getId().intValue()) {
// System.out.println(m2.getId()+" Name 值为空!!!");
forNumber++;
}
}
}
}
long s2 = System.currentTimeMillis();
System.out.println("双for循环查询时间为:" + (s2 - s1) + "(毫秒),一共查询出" + forNumber + "条数据 \n\n\n");
//TimeUnit.SECONDS.sleep(3);
//map查询测试
long s3 = System.currentTimeMillis();
int mapNumber = 0;
Map<Integer, Member> map = new HashMap<>();
for (Member m1 : list1) {
map.put(m1.getId(), m1);
}
for (Member m2 : list2) {
if (m2.getName() == null) {
Member m = map.get(m2.getId());
if (m != null) {
mapNumber++;
}
}
}
long s4 = System.currentTimeMillis();
System.out.println("使用map结构查询时间为:" + (s4 - s3) + "(毫秒),一共查询出" + mapNumber + "条数据 \n\n\n");
//stream流方法1
for (Person e1 : list1) {
list2.stream().anyMatch(e2 -> {
if (e1.getId() == e2.getId()) {
e1.setName(e2.getName());
}
return false;
});
}
//stream流方法1
list1.stream().forEach(e1 -> list2.stream().anyMatch(e2 -> {
if (e1.getId() == e2.getId()) {
e1.setName(e2.getName());
}
return false;
}));
System.out.println(list1);
}
}
边栏推荐
猜你喜欢

有效供应链管理的八大绩效分析指标(上)

多传感器融合定位(三)——惯性技术

Blocking queue

How to add the index of a set in mongodb to another set in mongodb

EN 1873屋面用装配附件.塑料单个屋面灯—CE认证

2022 R2 mobile pressure vessel filling test question simulation test platform operation

浪潮ClusterEngineV4.0 远程命令执行漏洞 CVE-2020-21224

电商数据模型设计

Worthington丨STEMxyme的分类和测定方法

什么是驱动程序签名,驱动程序如何获取数字签名?
随机推荐
深度之眼(十八)——偏导数
sql 左连接,内连接 的写法「建议收藏」
Codeforces Round #474 (Div. 1 + Div. 2) - C, F
Zero view h5s video platform getUserInfo information disclosure vulnerability cnvd-2020-67113
Explanation of history and chemical properties of Worthington ribonuclease B
Tyrosine decarboxylase -- characteristics of tyrosine decarboxylase of Streptococcus faecalis in Worthington
SQL left connection, internal connection writing method "recommended collection"
The classic dual stack implementation queue, pay attention to the modification of the judgment conditions of traversing the stack.
Asynchronism and synchronization of visa write and read functions by LabVIEW
Codeforces Round #474 (Div. 1 + Div. 2) - C, F
Worthington丨Worthington胰蛋白酶抑制剂说明书
Deep analysis of integrated learning AdaBoost
[self] - question brushing - string
websocket心跳机制(保活机制)
Pycharm new project
MySQL introduction
pip镜像下载
MySQL log management, backup and recovery
数据中台的那些“经验与陷阱”
[self] - question brushing - dynamic programming