Java-语法总结【持续更新】

2024年04月11日 14:30 · 阅读(377) ·

注释-枚举类型

  1. /**
  2. * 发起付款申请的表名称,利息收付/本金收付/提前终止表
  3. * {@link com.luoma.finance.enums.TableTypeEnum}
  4. *
  5. */
  6. @NotBlank(message = "发起付款申请的表名称不能为空")
  7. private String payType;

注释-代码块

  1. //#region
  2. //#endregion

StringJoiner

  1. StringJoiner sj = new StringJoiner(",");
  2. sj.add("1");
  3. sj.add("2");
  4. //1,2
  5. System.out.println(sj.toString());

Lists.partition 自动分页

  1. List<String> list = new ArrayList<>();
  2. for (int i = 0; i < 100; i++) {
  3. list.add(String.valueOf(i));
  4. }
  5. List<List<String>> listPages = Lists.partition(list,10);
  6. listPages.forEach(System.out::println);

输出

  1. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  2. [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
  3. [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
  4. [30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
  5. [40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
  6. [50, 51, 52, 53, 54, 55, 56, 57, 58, 59]
  7. [60, 61, 62, 63, 64, 65, 66, 67, 68, 69]
  8. [70, 71, 72, 73, 74, 75, 76, 77, 78, 79]
  9. [80, 81, 82, 83, 84, 85, 86, 87, 88, 89]
  10. [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

StopWatch 计时

  1. StopWatch stopWatch = new StopWatch();
  2. stopWatch.start("test1");
  3. TimeUnit.SECONDS.sleep(1);
  4. stopWatch.stop();
  5. stopWatch.start("test2");
  6. TimeUnit.SECONDS.sleep(1);
  7. stopWatch.stop();
  8. System.out.println(stopWatch.prettyPrint());

输出

  1. StopWatch '': 2.0131498 seconds
  2. ----------------------------------------
  3. Seconds % Task name
  4. ----------------------------------------
  5. 1.0103674 50% test1
  6. 1.0027824 50% test2

StopWatch 记录耗时日志案例

  1. import org.springframework.util.StopWatch;
  2. /**
  3. * 出队操作
  4. *
  5. * @param msg 出队数据
  6. * @return 出队结果
  7. */
  8. @Transactional(rollbackFor = Exception.class)
  9. @Override
  10. public boolean consumer(Object msg) {
  11. boolean result;
  12. StopWatch queueTime = new StopWatch();
  13. queueTime.start();
  14. long getKVListTime = 0L,checkKVListTime = 0L;
  15. String traceId = UUID.randomUUID().toString().replace("-", "");
  16. try {
  17. StopWatch stopWatch = new StopWatch();
  18. stopWatch.start();
  19. //业务逻辑
  20. stopWatch.stop();
  21. getKVListTime = stopWatch.getLastTaskTimeMillis();
  22. stopWatch.start();
  23. //业务逻辑
  24. stopWatch.stop();
  25. checkKVListTime = stopWatch.getLastTaskTimeMillis();
  26. } catch (Exception e) {
  27. log.error("traceId:[{}],{}结束调用,返回结果:{},e:{}", traceId, methodName, false, ExceptionUtil.getErrorMessage(e));
  28. result = false;
  29. }
  30. queueTime.stop();
  31. DecimalFormat decimalFormat = new DecimalFormat("##.##%");
  32. log.info("traceId:[{}],获取KV集合数据,总耗时:[{}]ms,占比:[{}]", traceId, getKVListTime,
  33. decimalFormat.format((float) getKVListTime / (float) queueTime.getTotalTimeMillis()));
  34. log.info("traceId:[{}],KV数据校验去重,总耗时:[{}]ms,占比:[{}]", traceId, checkKVListTime,
  35. decimalFormat.format((float) checkKVListTime / (float) queueTime.getTotalTimeMillis()));
  36. log.info("traceId:[{}],保存发放平台支付数据队列,总耗时:[{}]ms", traceId, queueTime.getTotalTimeMillis());
  37. return true;
  38. }

原子类-AtomicReference 使用场景

参考:AtomicReference 基本使用

ConcurrentMap

  • ConcurrentMap,它是一个接口,是一个能够支持并发访问的 java.util.map 集合;

  • ConcurrentMap,是一个线程安全,并且是一个高效的 HashMap

  1. private static ConcurrentMap<String, String> localMap = new ConcurrentHashMap();
  2. public static void main(String[] args) {
  3. localMap.put("testkey", "testValue");
  4. //testValue
  5. System.out.println(localMap.get("testkey"));
  6. }