zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    每天玩转3分钟 MyBatis-Plus
    每天玩转3分钟 MyBatis-Plus
    每天玩转3分钟 MyBatis-Plus
    git仓库管理
    【SpringCloud之pigx框架学习之路 】2.部署环境
    【SpringCloud之pigx框架学习之路 】1.基础环境安装
    Ubuntu 14.04 安装mysql
    Netflix是什么,与Spring Cloud有什么关系
    现学现用-我的第三个小小小私活
    申请微信小游戏账号
  • 原文地址:https://www.cnblogs.com/taojietaoge/p/10947214.html
Copyright © 2011-2022 走看看