Lambda
Lambda链式查询
List<UserEntity> users = new LambdaQueryChainWrapper<UserEntity>(userMapper)
.like(User::getName, "雨").ge(User::getAge, 20).list();
删除
public Boolean removeTopic(TopicTopicRequestVo topicTopicRequestVo) {
return this.remove(Wrappers.<TopicTopic>lambdaQuery().eq(TopicTopic::getAuthor, topicTopicRequestVo.getAuthor()));
}
修改
public Boolean updateByCondition(TopicTopicRequestVo topicTopicRequestVo) {
TopicTopic topicTopic = new TopicTopic();
BeanUtils.copyProperties(topicTopicRequestVo, topicTopic);
return this.update(topicTopic, Wrappers.<TopicTopic>lambdaQuery().eq(TopicTopic::getAuthor, topicTopicRequestVo.getAuthor()));
}
oldList转newList
public static List<AccountVo> oldListToNewList(List<Account> list){
return list.stream().map(e -> {
AccountVo vo = new AccountVo();
BeanUtils.copyProperties(e,vo);
return vo;
}).collect(Collectors.toList());
}
list 转 HashMap 一个key
public static Map<Long, Account> getIdAccountMap2(List<Account> list) {
return list.stream().collect(Collectors.toMap(Account::getId, Function.identity(), (val1, val2) -> val2));
}
list 转 HashMap 多个key,如果key重复取第1个
public static Map<String, Account> getIdAccountMap2(List<Account> list) {
return list.stream().collect(Collectors.toMap(k->k.getId()+k.getName(), Function.identity(), (val1, val2) -> val1));
}
list 转 HashMap 多个key,如果key重复取第2个
public static Map<String, Account> getIdAccountMap2(List<Account> list) {
return list.stream().collect(Collectors.toMap(k->k.getId()+k.getName(), Function.identity(), (val1, val2) -> val2));
}
list转LinkedHashMap
public static Map<Integer, List<Student>> listToMap1(List<Student> list) {
return list.stream().collect(Collectors.toMap(k->k.getId(), v ->
Lists.newArrayList(v),(List<Student> newValueList, List<Student> oldValueList)->
{newValueList.addAll(oldValueList);
return newValueList;
},LinkedHashMap::new));
}
//map转为list
private static List<Student> mapToList(Map<Integer,List<Student>> map) {
List<Student> lists = new ArrayList<>();
map.forEach((k,v)->{
List<Student> collect = v.stream().map(o -> {
Student student = new Student();
student.setId(Integer.valueOf(k));
student.setNames(objectToNameList(v));
student.setJobs(objectToJobList(v));
return student;
}).collect(Collectors.toList());
lists.addAll(collect);
});
return lists.stream().distinct().collect(Collectors.toList());
}
//list转map
public static Map<Integer, List<Student>> listToMap(List<Student> list) {
return list.stream().collect(Collectors.groupingBy(Student::getId));
}
//把对象某个属性转为集合
public static List<String> objectToNameList(List<Student> list){
return list.stream().map(Student::getName).collect(Collectors.toList());
}
public static List<String> objectToJobList(List<Student> list){
return list.stream().map(Student::getJob).collect(Collectors.toList());
}