O(1)緯度減小循環次數java
平事看淡,不服就幹。老子有句粗口話不知道當不當講,咱們公司上一次發工資時4月4號,時至今日5-30已經有57天沒有發工資了,我還要繼續堅持下去嗎?難不成如今你們工做都TM的不在意錢了的嗎?spring
使用O(1)緯度減小循環次數,提升代碼質量。apache
須要實現匹配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 }