基于jdk8的常用编程api
这里记录的全部是自己平时开发的项目中所用到的方法或者技巧,其中有的可以完全使用 Hutool 工具包替代
一、基于jdk8的常用编程api
1. list数据返回前端过滤字段方法
// list数据返回前端过滤字段方法
List<ZlDataStandard> queryList = zlDataStandardService.findList(new ZlDataStandard());
List<Dict> dictList = queryList.stream().collect(ArrayList::new, (list, item) -> {
Dict dict = Dict.create()
.set("id", item.getId())
.set("standardName", item.getStandardName())
.set("standardDescribe", item.getStandardDescribe())
.set("standardCode", item.getStandardCode())
.set("standardType", item.getStandardType());
list.add(dict);
}, ArrayList::addAll);
// 查询栏目
CmsContent content = cmsContentService.getById(id);
Dict dict = Dict.create().parseBean(content)
// 过滤出bean中的属性id", "title", "subtitle", "image 其他的字段一概不要
.filter("id", "title", "subtitle", "image")
.set("type", column.getType());
2. java 8 stream 使用sorted排序
List<Dict> collect = list.stream().sorted(Comparator.comparing(ZlEvaluate::getCreateTime, Comparator.reverseOrder())).collect(ArrayList::new, (arrList, item) -> {
Dict dict = Dict.create()
.set("id", item.getId())
.set("modelId", item.getModelId())
.set("evaluateNum", item.getEvaluateNum());
arrList.add(dict);
}, ArrayList::addAll);
3. 对于list集合使用java8中新特性排序,按照指定字段排序
List<ZlDataGovernResult> zlDataGovernResultList = list.stream().sorted(Comparator.comparing(ZlDataGovernResult::getPassRate)
.thenComparing(ZlDataGovernResult::getPassRate,Comparator.reverseOrder())).limit(5).collect(Collectors.toList());
4. 集合 list 转 map
Map<Integer, Apple> appleMap = appleList.stream().collect(Collectors.toMap(Apple::getId, a -> a,(k1,k2)->k1));
public List<PersonVO> selectPersons(List<Long> personIds) {
List<PersonVO> persons = new ArrayList<>(personIds.size());
List<Person> personList = personMapper.selectByIds(personIds);
//批量查询,转换成Map
List<PersonExt> personExtList = personExtMapper.selectByIds(person.getId());
Map<String, PersonExt> personExtMap = personExtList.stream().collect(Collectors.toMap(PersonExt::getPersonId, Function.identity()));
for (Person person : personList) {
PersonVO vo = new PersonVO();
//直接从Map中查找
PersonExt personExt = personExtMap.get(person.getId());
// 组装数据
persons.add(vo);
}
return persons;
}
5. 属性拷贝
// 属性拷贝
BeanUtils.copyProperties(Object source, Object target);
6. 集合中常见操作
// 集合判空
CollectionUtils.isEmpty(details)
// 集合排序
Collections.sort(list,Comparator.comparing(Rule::getPriority).thenComparing(Rule::getPriority).reversed());
// 随机排序
Collections.shuffle(list);
// 集合循环
List<String> list = new ArrayList<>();
coupons.forEach(n -> {
list.add(n.getId());
});
parents.forEach((p) -> {
this.add(o);
});
// 集合分组
Map<Integer, List<Apple>> groupBy = appleList.stream().collect(Collectors.groupingBy(Apple::getId));
// 集合 list 转 map
Map<Integer, Apple> appleMap = appleList.stream().collect(Collectors.toMap(Apple::getId, a -> a,(k1,k2)->k1));
// 集合去重
List list=(List) a.stream().distinct().collect(Collectors.toList());
// 集合取属性
List<String> courseIds= users.stream().map(UserEntity::getUserName).collect(Collectors.toList());
// 集合过滤
list.stream().filter(user-> user.getId() > 5 && "1组".equals(user.group)).collect(Collectors.toList());
// 集合取部分
List newList = list.subList(start, end);
// 集合类型转换
Integer[] aftIdArray = (Integer[]) ConvertUtils.convert(questionIds, Integer.class);
// 时间转换
LocalDateTime midnight = LocalDateTime
.now()
.plusDays(1)
.withHour(0)
.withMinute(0)
.withSecond(0)
.withNano(0);
long seconds = ChronoUnit.SECONDS.between(LocalDateTime.now(), midnight);
7. 异步执行
TimerUtil.instance().addTaskSeconds((timeout) -> {
// TODO 异步执行方法
}, 2);
8. 字符串和list互相转换
// 字符串转list
List<String> payTypes = Arrays.asList(payTypeStr.split(CommonConstants.SIGN_COMMA));
// list转字符串
String s = org.apache.commons.lang.StringUtils.join(list.toArray(), ",");
9. java stream 流的发方式分页加排序
// java stream 流的发方式分页加排序
List<DiScript> collect = scriptList.stream().skip(pageSize * (pageNum - 1)).limit(pageSize).sorted(
Comparator.comparing(DiScript::getCreateDate)).collect(Collectors.toList());
10. 格式化数字
// 格式化数字
public static void main(String[] args) {
System.out.println(String.format("%03d", 1));
// 输出001
}
