91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java代碼實例分析

發布時間:2022-04-02 10:22:32 來源:億速云 閱讀:285 作者:iii 欄目:編程語言

這篇文章主要介紹“java代碼實例分析”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“java代碼實例分析”文章能幫助大家解決問題。

一、幾個坑爹代碼的目錄

1、這樣使用 StringBuffer 的方法有什么坑?

2、你寫過的最長的一行代碼有多長???

3、循環+條件判斷,你最多能嵌套幾層?

4、為了后期優化查詢速度 ~ 頗有商業頭腦!

5、你是如何被異常玩然后變成玩異常的?

6、Stream 玩得最 6 的代碼,看過的人都驚呆了!

二、坑爹代碼 | 這樣使用 StringBuffer 的方法有什么坑?

你是否曾經這樣使用過 Java 的 StringBuffer 類?

/**   * Create Time 2019/5/24   * StringBuffer追加 如癡如醉的寫法   * @author cailong   **/  public class Append {      public static void main(String[] ares){          StringBuffer sb = new StringBuffer();          //這里都能理解          sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><ROOT>");          for (int i = 0; i < 10; i++) {              //為什么到這里就要這樣寫???既然都用StringBuffer了    (這里省略集合遍歷用i代替 意思能懂就行)              sb.append("<NSRXX>" +                      "<NSRSBH>"+i+"</NSRSBH>" +                      "<NSRMC>"+i+"</NSRMC>" +                      "<DJXH>"+i+"</DJXH>" +                      "<ZGSWJ_DM>"+i+"</ZGSWJ_DM>" +                      "<ZGSWJ_MC>"+i+"</ZGSWJ_MC>" +                      "<SJLY>sjzs</SJLY>" +                      "<YWSX_DM>"+i+"</YWSX_DM>" +                      "</NSRXX>");          }          sb.append("</ROOT>");          System.out.println(sb.toString());      }  }

三、坑爹代碼 | 你寫過的最長的一行代碼有多長???

java代碼實例分析

你寫過的最長的一行代碼有多長嗎?你為什么要寫這么長?是看著帥呢,還是想減少代碼行數?

List<OperationPurchaseInfo> purchaseInfoList                  = sheet.getPurchaseInfoList()                  .stream()                  .filter(purchaseInfo -> purchaseInfo.getExteriorOperation()                          .getExteriorPart()                          .getExteriorOperationList()                          .stream()                          .filter(exteriorOperation -> exteriorOperation                                  .getProcessState()                                  .equals(ExteriorOperation.ProcessState.PROCESSING))                          .count() != 0                          // 訂單明細中工序對應的工件下的其他工序存在加工中,                          // 且已發給供應商且供應商不是當前訂單供應商時,需要判斷                          && (purchaseInfo.getExteriorOperation()                          .getExteriorPart()                          .getTeamwork() == null || !purchaseInfo.getExteriorOperation()                          .getExteriorPart().getTeamwork().equals(sheet.getTeamwork()))                  ).collect(Collectors.toList());

上面這段代碼雖然被拆開多行顯示,但本質上是一行,一個極其復雜的賦值語句!

這種代碼是不是為了讓別人看不懂來彰顯自己的編碼水平呢?

小編覺得 Java Stream API 以及各種函數式編程方法,以及各種語法糖在某種程度讓這種糟糕代碼越來越多!

那么一起來批判一下這個代碼,或者你有什么好的解決方案呢?

四、坑爹代碼 | 循環+條件判斷,你最多能嵌套幾層?

for 循環和 if 條件判斷語句,必不可少吧。但是你見過最多嵌套的循環和條件判斷有幾層呢?或者說,你最多能容忍多少層的嵌套呢?

java代碼實例分析

我們還是先來看看極端的坑爹代碼吧:

// 這個***循環嵌套,只是總循環的一部分。。。我已經繞暈在黃桷灣立交          if (recordList.size() > start) {              for (int i = start; i < end; i++) {                  Map<String, Object> map = recordList.get(i);                  Map<String, Object> field11 = (Map<String, Object>) map.get("field"); //name -> code                  Map<String, Object> record11 = (Map<String, Object>) map.get("record"); // code -> value                  String catagory1 = map.get("categoryId").toString();                  //  查詢***種類型對應的其他類型                  SalaryDataVo ss = JSON.parseObject(JSON.toJSONString(map), SalaryDataVo.class);                  Page page3 = salaryManagerService.getAllRecordsByCondition(ss);                  if (page3.getRecords().size() > 0) {                      List<Map<String, Object>> salaryDataVos = page3.getRecords();                      salaryDataVos = this.reSetMap(salaryDataVos, null, null);                      for (Map<String, Object> map2 : salaryDataVos) {                          Map<String, Object> field2 = (Map<String, Object>) map2.get("field");                          Map<String, Object> record2 = (Map<String, Object>) map2.get("record");                          String catagory2 = map2.get("categoryId").toString();                          List<SalaryGroupVO> groupList2 = salaryGroupService.getSalaryGroupsItems(this.getUserCorpId(), catagory2);                          for (SalaryGroupVO cc : groupList2) {                              cc.setCode(cc.getParentId() + cc.getCode());                          }                          //計算                          for (Map.Entry<String, Object> entity : field2.entrySet()) {                              String keyName = entity.getKey();                              for (SalaryGroupVO s2 : groupList2) {                                  if ("bigDecimal".equals(s2.getItemType()) && s2.getCode().equals(field2.get(keyName).toString()) && ("部門" != keyName) && ("姓名" != keyName) && StringUtils.isNotEmpty(s2.getItemType())) {                                      if (field11.containsKey(keyName)) {                                          if (field11.containsKey(keyName)) {                                              String code1 = field11.get(keyName).toString();                                              Double newValue = 0.0;                                              Double oldValue = 0.0;                                              if (!record11.get(code1).toString().matches("^[0-9]*$")) {                                                  oldValue = Double.parseDouble(record11.get(code1).toString());                                                  if (record2.containsKey(entity.getValue().toString()) && (!record2.get(entity.getValue().toString()).toString().matches("^[0-9]*$"))) {                                                      String value2 = record2.get(entity.getValue().toString()).toString();                                                      newValue = Double.parseDouble(value2);                                                  }                                                  record11.remove(field11.get(keyName).toString());                                              }                                              if (code1.startsWith(catagory1) || code1.startsWith(catagory2)) {                                                  String co = code1.replace(catagory1, "hz");                                                  field11.put(keyName, co);                                                  record11.put(co, oldValue + newValue);                                              }                                          }                                      } else {                                          String code = entity.getValue().toString();                                          String str = entity.getValue().toString();                                          Object value2 = record2.get(entity.getValue().toString());                                          if (str.startsWith(catagory1) && str.replace(catagory1, "").startsWith("hz")) {                                              code = str.replace(catagory1, "");                                          } else if (str.startsWith(catagory2) && str.replace(catagory2, "").startsWith("hz")) {                                              code = str.replace(catagory2, "");                                          }                                          field11.put(keyName, code);                                          record11.put(code, value2);                                      }                                  }                              }                          }                      }                  }                  List<SalaryGroupVO> sList = salaryGroupService.getSalaryGroupItemsByParentId(catagory1);                  for (SalaryGroupVO s : sList) {                      if (field11.containsKey(s.getName()) && s.getCode().startsWith("hz")) {                          String k = field11.get(s.getName()).toString();                          field11.put(s.getName(), s.getCode());                          String value = null;                          if (record11.containsKey(k)) {                              value = record11.get(k).toString();                          }                          record11.put(s.getCode(), value);                      }                  }                  resultList.add(map);                  pageInfo.setRecords(resultList);              }          }

五、坑爹代碼 | 為了后期優化查詢速度 ~ 頗有商業頭腦!

什么樣的程序員是一個好程序員呢?當我們在給客戶開發系統時候,為了后期的優化,預留一些埋點。

通過對這些埋點的優化,可以讓客戶瞬間感覺系統在運行速度上有質的飛躍,讓公司順利的簽署二期開發合同,收取巨額開發費用。

從公司角度來看,這樣的程序員就是一個好程序員。 

比如:

java代碼實例分析

六、坑爹代碼 | 你是如何被異常玩然后變成玩異常的?

玩 Java 的人,剛開始會被各種異常虐,空指針應該是最常見的。多玩兩年就開始玩異常,各種奇葩異常玩法層出不窮。

你覺得下面這種異常的定義妥嗎?

/**   * 處理業務的異常   * 居然有一堆靜態異常,準備好了隨時可以拋??   * 錯誤碼是字符串   */  public class CommandException extends BaseException {    private static final long serialVersionUID = -6354513454371927970L;    public static CommandException PARAM_NULL= new CommandException("Command_assemble_01", "Parameter is Needed But Empty");    public static CommandException DEVID_NULL = new CommandException("Command_assemble_02", "DevId Cannot Be Null");    public static CommandException MDCODE_NULL = new CommandException("Command_assemble_03", "Model Code Cannot Be Empty");    public static CommandException ORDER_NULL = new CommandException("Command_assemble_04", "Order Cannot Be Empty");    public static CommandException TYPE_NULL = new CommandException("Command_assemble_05", "Upstream / Downstream Type Cannot Be Empty");    public static CommandException MENUID_NULL = new CommandException("Command_assemble_06", "Menu Id Cannot Be Null");    public static CommandException CTRLTYPE_NOT_RANGE= new CommandException("Command_assemble_07", "Ctrltype Cannot Be Recognized, Which is not in Range");    public static CommandException CMD_NULL = new CommandException("Command_analyze_01", "CMD Cannot Be Null");    public static CommandException PAYLOAD_NULL = new CommandException("Command_analyze_02", "Payload Cannot Be Null");    public static CommandException FRAMEWORK_FAILED= new CommandException("Command_analyze_03", "Framework Failed to be Checked");    public static CommandException CHECK_FAILED= new CommandException("Command_analyze_04", "Command Failed to be Checked");    public static CommandException CONFIGURE_NOT_EXIST = new CommandException("Command_error_1001", "Configure is not Exist");    public static CommandException REDIS_ERROR = new CommandException("Command_error_1002", "Cache Command in Redis Error", true);    //省略構造函數、get/set方法  }

如果不妥,有什么問題呢?

七、坑爹代碼 | Stream 玩得最 6 的代碼,看過的人都驚呆了!

Stream 作為 Java 8 的一大亮點,它與 java.io 包里的 InputStream 和 OutputStream 是完全不同的概念。Java 8 中的 Stream 是對集合(Collection)對象功能的增強,它專注于對集合對象進行各種非常便利、高效的聚合操作(aggregate operation),或者大批量數據操作 (bulk data operation)。Stream API 借助于同樣新出現的 Lambda 表達式,極大的提高編程效率和程序可讀性。

java代碼實例分析

Stream 的用法:

//Stream 用的66的  final EventAction eventAction = redisObj(                  EventActionKey + distributionEventId,                  () -> Optional                          .of(distributionEventId)                          .map(eventId -> {                              final EventActionExample example = new EventActionExample();                              example.createCriteria()                                      .andEventIdEqualTo(eventId)                                      .andTriggerTypeEqualTo(EnumEventTriggerType.DISTRIBUTION_PURCHASE.getCode().byteValue());                              return example;                          })                          .map(eventActionMapper::selectByExample)                          .filter(StringUtil::isNotEmpty)                          .map(e -> e.get(0)).orElseThrow(() -> ExceptionUtil.createParamException("事件觸發信息不存在"))                  , EventAction.class);          final AwardConfig awardConfig = redisObj(EventConfigKey + eventAction.getId(),                  () -> Optional.ofNullable(eventAction.getId())                          .map(actionId -> {                              final AwardConfigExample example = new AwardConfigExample();                              example.createCriteria()                                      .andActionIdEqualTo(actionId);                              return example;                          })                          .map(awardConfigMapper::selectByExample)                          .filter(StringUtil::isNotEmpty)                          .map(e -> e.get(0)).orElseThrow(() -> ExceptionUtil.createParamException("xxx")),                  AwardConfig.class          );          Optional.of(req)                  .map(e -> e.clueUid)                  .map(id -> {                      final ClueExample example = new ClueExample();                      example.createCriteria()                              .andClueUidEqualTo(id)                              .andDeletedEqualTo(false)                              .andReceivedEqualTo(false)                              .andCreateTimeGreaterThan(now - cluetime);                      example.setOrderByClause("create_time asc");                      return example;                  })  // 獲取該被邀請人所有未過期且未被領取的線索的線索                  .map(clueMapper::selectByExample)                  .filter(StringUtil::isNotEmpty)                  .ifPresent(clues -> {                              final ClueResp clueResp = Optional.of(req)                                      .filter(c -> {                                          c.count = clues.size();                                          return true;                                      })                                      .map(this::awardValue)                                      .orElseThrow(() -> ExceptionUtil.createParamException("參數錯誤"));                              final Integer specialId = req.getIsHead()                                      ? clues.get(0).getId()                                      : clues.get(clues.size() - 1).getId();                              clues.stream()                                      .peek(clue -> {                                          final AwardConfig awardConfigclone = Optional.of(awardConfig)                                                  .map(JSONUtil::obj2Json)                                                  .map(json -> JSONUtil.json2Obj(json, AwardConfig.class))                                                  .orElseGet(AwardConfig::new);                                          awardConfigclone.setMoney(                                                  Optional.of(clue.getId())                                                          .filter(specialId::equals)                                                          .map(e -> clueResp.specialReward.longValue())                                                          .orElse(clueResp.otherAverageReward.longValue())                                          );                                          eventActionService.assembleAward(                                                  awardConfigclone,                                                  clue.getAdviserUid(),                                                  clue.getAdviserUid(),                                                  clue.getClueUid(),                                                  eventAction,                                                  new PasMessageParam(),                                                  clue.getId(),                                                  AwardRelationType.Clud.code()                                          );                                      })                                      .forEach(clue -> {                                          clue.setOrderNo(req.orderNo);                                          clue.setCommodityName(req.commodityName);                                          clue.setOrderAmount(req.orderAmount);                                          clue.setReceived(true);                                          clue.setModifyTime(now);                                          clueMapper.updateByPrimaryKeySelective(clue);                                      });                          }                  );

關于“java代碼實例分析”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

双桥区| 交口县| 张家界市| 阿鲁科尔沁旗| 新余市| 延寿县| 白银市| 东乡族自治县| 潞西市| 光山县| 宝坻区| 南京市| 宣威市| 英超| 丰镇市| 广昌县| 台山市| 紫金县| 平南县| 沈丘县| 克什克腾旗| 集贤县| 和顺县| 区。| 达孜县| 伊川县| 绥中县| 青海省| 商河县| 峨山| 宜宾市| 辽阳县| 团风县| 龙胜| 万安县| 巩留县| 杭锦旗| 绥棱县| 衡山县| 海丰县| 东莞市|