1. 程式人生 > >個人程式碼,自創迴圈套迴圈後輸出Map資料結構的寫法

個人程式碼,自創迴圈套迴圈後輸出Map資料結構的寫法

@Override
public HttpCommandResultWithData getSalesDataByUsersAndTransId(SalesQueryLogDataByUserCommand command) {
    HttpCommandResultWithData result = new HttpCommandResultWithData().fillResult(ReturnCode.OK);
    // 入參校驗
    List<String> userIds = command.getUserIds();
    if (CollectionUtils.
isEmpty(userIds)) { throw new SellAppCustomFailException(ErrorMsgEnum.SELL_APP_SERVICE_PARAMS_ERROR_USERS_NOT_FIND); } List<String> transIds = command.getTransIds(); if (CollectionUtils.isEmpty(transIds)) { throw new SellAppCustomFailException(ErrorMsgEnum.SELL_APP_SERVICE_PARAMS_ERROR_TRANS_ID_NOT_FIND)
; } // 獲取redis資料方法 Function<String, Map<String, String>> getDataByRedis = userId -> { Map<String, String> beanMap = new HashMap<>(); transIds.forEach(transId -> { String jsonLogArrays = Optional.ofNullable( (String)
redisTemplate.opsForHash().get(RedisStaticConst.QINGQI_LOG_REDIS_KEY_PREX + transId, userId)) .orElseGet(String::new); List<RedisLogSimplePojo> list = new ArrayList(); try { list = JsonUtil.toList(jsonLogArrays, RedisLogSimplePojo.class); } catch (IOException e) { logger.warn("=== getSalesDataByUsers 查詢雲端redis資料 雲端redis hkey : {} key: {} 轉換json陣列異常", RedisStaticConst.QINGQI_LOG_REDIS_KEY_PREX + transId, userId); } beanMap.put(transId, String.valueOf(list.size())); }); return beanMap; }; // 返回結果 AtomicInteger index = new AtomicInteger(); Map<String, Map<String, String>> retMap = userIds.stream().collect(Collectors.toMap(key -> { String userId = userIds.get(index.get()); index.incrementAndGet(); return userId; }, getDataByRedis)); result.setData(retMap); return result; }