O(1)纬度减少循环次数
O(1)纬度减少循环次数
平事看淡,不服就干。老子有句粗口话不知道当不当讲,我们公司上一次发工资时4月4号,时至今日5-30已经有57天没有发工资了,我还要继续坚持下去吗?难不成现在大家工作都TM的不在乎钱了的吗?
使用O(1)纬度减少循环次数,提高代码质量。
需要实现匹配list1 和 list2 中keyName相等的cipher,并把list1中的cipher写入list2:
1 package com.xinyan.springcloud.tjt;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
8 import org.apache.commons.lang.StringUtils;
9
10 import lombok.Data;
11
12 public class CompareOne {
13 private static List<KeyInfo> list1 = new ArrayList<>();
14 private static List<CipherPathInfo> list2 = new ArrayList<>();
15
16 /**
17 * 比较low的methodOne设计
18 */
19 public void methodOne() {
20 // 匹配list1 和 list2 中keyName相等的cipher,并把list1中的cipher写入list2:
21 // 设计方案1:
22 for (int i = 0; i < list1.size(); i++) {
23 KeyInfo keyInfo = list1.get(i);
24 String keyName = keyInfo.getKeyName();
25 String cipher = keyInfo.getCipher();
26 for (int j = 0; j < list2.size(); j++) {
27 CipherPathInfo cipherPathInfo = list2.get(j);
28 String keyName2 = cipherPathInfo.getKeyName();
29 if (StringUtils.equals(keyName, keyName2)) {
30 cipherPathInfo.setCipher(cipher);
31 }
32 }
33 }
34 }
35
36 /**
37 * 较好的methodTwo设计
38 */
39 public void methodTwo() {
40 // 匹配list1 和 list2 中keyName相等的cipher,并把list1中的cipher写入list2:
41 // 设计方案2:
42 Map<String, String> keyNameMap = new HashMap<>();
43 // 使用keyNameMap缓存keyName的cipher
44 for (int i = 0; i < list1.size(); i++) {
45 KeyInfo keyInfo = list1.get(i);
46 String keyName = keyInfo.getKeyName();
47 String cipher = keyInfo.getCipher();
48 keyNameMap.put(keyName, cipher);
49 }
50 // 根据keyName的名称查keyNameMap取出cipher
51 for (int j = 0; j < list2.size(); j++) {
52 CipherPathInfo cipherPathInfo = list2.get(j);
53 String keyName = cipherPathInfo.getKeyName();
54 String cipher = keyNameMap.get(keyName);
55 if (StringUtils.isNotEmpty(cipher)) {
56 cipherPathInfo.setCipher(cipher);
57 }
58 }
59 }
60
61 /**
62 * 实体KeyInfo
63 *
64 * @author apple
65 */
66 @Data
67 class KeyInfo {
68 private String keyName;
69 private String cipher;
70 }
71
72 /**
73 * 实体CipherPathInfo
74 *
75 * @author apple
76 */
77 @Data
78 class CipherPathInfo {
79 private String keyName;
80 private String cipher;
81 private String path;
82 }
83
84 /**
85 * 构造KeyInfo、CipherPathInfo实体信息
86 */
87 public void makeEntityInfo() {
88 KeyInfo keyInfo = new KeyInfo();
89 // 构造30个keyInfo实体
90 for (int i = 0; i < 30; i++) {
91 keyInfo.setKeyName("name_" + i);
92 keyInfo.setCipher("cipher_" + i);
93 list1.add(keyInfo);
94 }
95 CipherPathInfo cipherPathInfo = new CipherPathInfo();
96 // 构造100个ciperhPathInfo实体,其中cipher为null
97 for (int j = 0; j < 100; j++) {
98 cipherPathInfo.setKeyName("name_" + j);
99 cipherPathInfo.setPath("path_" + j);
100 list2.add(cipherPathInfo);
101 }
102 }
103
104 public static void main(String[] args) {
105 CompareOne c = new CompareOne();
106 c.makeEntityInfo();
107 // 匹配list1 和 list2 中keyName相等的cipher,并把list1中的cipher写入list2:
108 // 设计方案1:
109 c.methodOne();
110 // 方案1设计明显不合理,很low;其中list1有30个元素,而list2有100个
111 // 这样就会累计循环30*100次
112 // 可以将讲list1中获取到的keyName插入哈希中,只需要O(1)的纬度
113 // 方案设计2:
114 c.methodTwo();
115
116 }
117
118 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?