List相关方法

1、将list集合按指定长度进行切分,返回新的List<List<??>>集合

List<List<VO>>  newList = Lists.partition(oldList, size);

2、list转map对象

Map<String, VO> newMap =oldList.stream().collect(Collectors.toMap(VO::getUuid, Function.identity(), (key1, key2) -> key2));

3、list转属性map

Map<Integer, String> newMap = oldList.stream().collect(Collectors.toMap(VO::getUuid, VO::getPersonName));

4、list分组

Map<String, Map<String, List<VO>>> targetMap = oldList.stream().collect(Collectors.groupingBy(VO::getPersonName, Collectors.groupingBy(VO::getPersonId)));

5、list对象转换

List<NewVO> newList = oldList.stream().map(t -> {
  NewVO newVO= new NewVO();
  newVO.setUuid(t.getUuid());
  return newVO;
}).collect(Collectors.toList());

6、list去重

List<Integer> newList = oldList.stream().distinct().collect(Collectors.toList());

7、按照List中对象的id属性升序

oldList.sort(Comparator.comparing(Stu::getId));

8、按照List中对象的id属性降序

oldList.sort(Comparator.comparing(Stu::getId).reversed());

9、多条件升序

oldList.sort(Comparator.comparing(Stu::getId).thenComparing(Stu::getSid));

10、id升序,sid降序

oldList.sort(Comparator.comparing(Stu::getId).reversed().thenComparing(Stu::getSid));

11、集合升序排序

Collections.sort(student, new Comparator(){
  public int compare(StudentVo p1, StudentVo p2) {
  return Integer.parseInt(p1.getStudentCode()) - Integer.parseInt(p2.getStudentCode());
  }
});

 12、去重list中的某个属性

List<VO> newList = oldList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getParam()))), ArrayList::new));

posted @ 2021-12-20 14:06  yuanjianglong  阅读(53)  评论(0编辑  收藏  举报