对象转集合
Arrays.asList(resourceInfo)
Collections.singletonList (resourceInfo)
获取最大值
集合.stream().max(Comparator.comparingLong(对象::getCreateTime))
获取空集合
Collections.emptyList();
时间戳转时间
DateUtil.format(DateUtil.date(i.getCreateTime()),"yyyy-MM-dd HH:mm:ss")
查询相同字段的数量
SELECT `name` FROM `sys_employee` GROUP BY sys_employee.`name`,sys_employee.hi_id HAVING count(1)>=2
集合转指定连接符字符串
String string = list.stream().map(String::valueOf).collect(Collectors.joining(“,”));
String.join(“,”, 集合);
List<MenuVO> listChildren = children.stream().sorted(Comparator.comparing(MenuVO::getOrderNum,Comparator.nullsLast(Integer::compareTo))).collect(Collectors.toList());
按照指定顺序转换为map
Map<String, List<T>> collect1 = list.stream().collect(Collectors.groupingBy(T::getT, LinkedHashMap::new, Collectors.toList()));
Page<PfmReviewRules> rulesPage = this.page(reviewRulesPage, new LambdaQueryWrapper<PfmReviewRules>()
.eq(PfmReviewRules::getHospId, hiId).and(StrUtil.isNotEmpty(keyword), i -> i.like(PfmReviewRules::getNumStr, keyword)
.or().like(PfmReviewRules::getName, keyword)));
like与and的使用
AND (IAS_SoreId LIKE CONCAT( #{iaId,jdbcType=VARCHAR}, ",", "%" )
OR IAS_SoreId LIKE CONCAT( #{iaId,jdbcType=VARCHAR} )
OR IAS_SoreId LIKE CONCAT( "%",",", #{iaId,jdbcType=VARCHAR} )
OR IAS_SoreId LIKE CONCAT("%",",",#{iaId,jdbcType=VARCHAR},",","%"))
简单粗暴
// 对 key 进行统计个数
Map<String, Long> map1 = kvsList.stream().collect(Collectors.groupingBy(Kvs::getKey, Collectors.counting()));
System.out.println("map1=" + JSON.toJSONString(map1));
System.out.println();
// 根据 key 对集合进行分组-1
Map<String, List<Kvs>> map2_1 = kvsList.stream().collect(Collectors.groupingBy(Kvs::getKey, Collectors.toList()));
System.out.println("map2_1=" + JSON.toJSONString(map2_1));
System.out.println();
// 根据 key 对集合进行分组-2
Map<String, List<Kvs>> map2_2 = kvsList.stream().collect(Collectors.groupingBy(Kvs::getKey));
System.out.println("map2_2=" + JSON.toJSONString(map2_2));
System.out.println();
// 根据 key 对集合进行分组, 并且 value 的集合为指定属性集合而不是对象
Map<String, List<String>> map3 = kvsList.stream().collect(Collectors.groupingBy(Kvs::getKey, Collectors.mapping(Kvs::getValue, Collectors.toList())));
System.out.println("map3=" + JSON.toJSONString(map3));
System.out.println();
// List<Kvs> -> Map<String, List<Order>>
// Map<String, List<Order>> map3 = kvsList.stream().collect(Collectors.groupingBy(Kvs::getKey, Collectors.mapping(e -> new Order(e.getNo(), e.getAmt(), Collectors.toList())));
// 根据 key 进行分组并且将指定属性值进行拼接作为哈希表的 value 值
Map<String, String> map4 = kvsList.stream().collect(Collectors.groupingBy(Kvs::getKey, Collectors.mapping(Kvs::getValue, Collectors.joining("#"))));
System.out.println("map4=" + JSON.toJSONString(map4));
System.out.println();
// 根据 key 进行分组并根据 指定属性进行比较取最大元素
Map<String, Optional<Kvs<String>>> map5 = kvsList.stream().collect(Collectors.groupingBy(Kvs::getKey, Collectors.maxBy(Comparator.comparing(Kvs::getValue))));
System.out.println("map5=" + JSON.toJSONString(map5));
System.out.println();
// partitioningBy 会根据 传入的 表达式进行判断, 将集合分为 true 和 false 两个列表
Map<Boolean, List<Kvs>> map6 = kvsList.stream().collect(Collectors.partitioningBy(kvs -> "A0001".equals(kvs.getKey())));
System.out.println("map6=" + JSON.toJSONString(map6));
System.out.println();
// 遍历结果集
kvsList.stream().collect(Collectors.groupingBy(Kvs::getKey, Collectors.toList())).forEach((k, v) -> {
System.out.println(MessageFormat.format("key={0}, value={1}", k, v));
});
List<Kvs<Integer>> numList = Lists.newArrayList(
new Kvs<>("A001", 1),new Kvs<>("A001", 2),new Kvs<>("A001", 3),
new Kvs<>("A002", 5),new Kvs<>("A002", 3),new Kvs<>("A003", 7)
);
System.out.println();
// 根据 key 进行分组并且根据 指定数据进行统计
Map<String, Integer> num1 = numList.stream().collect(Collectors.groupingBy(Kvs::getKey, Collectors.summingInt(Kvs::getValue)));
System.out.println("num1=" + JSON.toJSON(num1));
System.out.println();
// summarizingInt 用来同时计算 average、min、max、count、sum
Map<String, IntSummaryStatistics> num2 = numList.stream().collect(Collectors.groupingBy(Kvs::getKey, Collectors.summarizingInt(Kvs::getValue)));
System.out.println("num2=" + JSON.toJSON(num2));
System.out.println();
// 集合实体转 实体中某一字段集合
List<String> keys = kvsList.stream().map(Kvs::getKey).collect(Collectors.toList());
对集合进行分页处理
List<PfmReviewRules> reviewRulesList=reviewRules1.stream().skip((current-1)*pageSize).limit(pageSize).collect(Collectors.toList());
PageUtils pageUtils = new PageUtils(reviewRulesList, reviewRules1.size(), pageSize,current);
分割符使用
String str= "ORACLE_WENBENWENJIAN.PRO";
String[] splitStr= str.split(".");
这个时候得到的是一个空数组。
后来跟高姓同事深♂入交流后知道了应该这样写
String[] splitStr= str.split("\\.");
其他需要转义的字符有:
() [] {} + - * / . ^ ? $ ¦
pfmDataDietaryVos.stream().collect(Collectors.groupingBy(PfmDataDietaryVo::getGradeLevel,LinkedHashMap::new, Collectors.toList()));
查询指定时间范围的日志数据
cat 666.log | grep "123456" | grep "m:10011111" | sed -n '/2022-08-27 17:00:*/,/2022-08-27 17:05:*/p'