开发小技巧
编程小技巧
1. 将List<Object> 转换为 List<String>
List<User> userList;// [{"name":"wang","age":23},{"name":"liu","age":13}]
List<String> nameList=userList.stream()
.map(t->t.getName())
.collect(Collectors.toList());// ["wang","li"]
2. 将List转换为Map
Map<Long,user> userInfo=user.stream()
.collect(Collectors.toMap(user::getId,Function.identity()));
3. Java将JSON对象或JSON数组转list对象
https://blog.csdn.net/superPojo/article/details/108793355
4.lambda 遍历List<String>替换成另外一个值
estateMaps.stream().map(e-> domainChange(e)).collect(Collectors.toList())
5.Java对象类型集合单独取出对象中一个属性成为集合或数组
List<String> stateNameList = dictEntityList.stream().map(DictEntity::getName).collect(Collectors.toList());
pg序列自动生成 bigserial

@EnumValue
https://baomidou.com/pages/8390a4/
单元测试
- jacoco插件配置生成单元测试覆盖率报告:https://blog.csdn.net/weixin_38384296/article/details/122655287
PG删库提示“There are 2 other sessions using the database”信息
select pg_terminate_backend(pid) from (select pid from pg_stat_activity where datname = '数据库名' ) a;- 删库
分组 groupingBy
public class Groupingby {
@Data
@AllArgsConstructor
public static class User {
private String name;
private Integer age;
private String sex;
}
public static void main(String[] args) {
// 初始化测试数据
User user1 = new User("张三", 18, "男");
User user2 = new User("李四", 19, "女");
User user3 = new User("王五", 20, "男");
User user4 = new User("赵六", 21, "女");
User user5 = new User("钱七", 22, "男");
List<User> users = Arrays.asList(user1, user2, user3, user4, user5);
// 进行男女分组 <sex,List<User>>
Map<String, List<User>> collect = users.stream().collect(Collectors.groupingBy(User::getSex));
// 如果能保证名称唯一,可以快速生成map进行快速查找 <name,User>
Map<String, User> collect1 = users.stream().collect(Collectors.toMap(User::getName, Function.identity()));
// 进行男女计数 <sex,Integer>
Map<String, Long> collect2 = users.stream().collect(Collectors.groupingBy(User::getSex, Collectors.counting()));
// 进行男女分组并统计年龄总和 <sex,Integer>
Map<String, Integer> collect3 = users.stream().collect(Collectors.groupingBy(User::getSex, Collectors.summingInt(User::getAge)));
// 根据姓氏进行分组 <lastName,List<User>>
Map<String, List<User>> collect4 = users.stream().collect(Collectors.groupingBy(user -> {
return user.getName().substring(0, 1); // 自定义 Map key 逻辑
}));
}
}
本文来自博客园,作者:帅气的涛啊,转载请注明原文链接:https://www.cnblogs.com/handsometaoa/p/16832423.html

浙公网安备 33010602011771号