java8 集合操作

2022-05-07 16:34:22 浏览数 (3)

1.根据集合的某个属性值,给集合做排序.

1.实体类要实现Comparable接口
代码语言:javascript复制
public class IndustryInfo implements Comparable<IndustryInfo>{

    private String code;

    private String name;

    private Integer num;

    private String str;
    
    @Override
    public int compareTo(IndustryInfo o) {
        return 0;
    }
    //get set 省略
}
2.按照num逆序排列
代码语言:javascript复制
        List<IndustryInfo> listRe = list.stream().sorted(Comparator.comparing(IndustryInfo::getNum).reversed()).collect(Collectors.toList());

2.排除某个特定元素

代码语言:javascript复制
List<Map<String, Object>> list8 = list7.stream().filter(map -> !map.get("name").toString().equals("高新技术企业")).collect(Collectors.toList());

3.获取某个特定元素

代码语言:javascript复制
        Map<String, Object> map1 = list2.stream().filter(map -> map.get("name").toString().equals("黑榜企业")).collect(Collectors.toList()).get(0);

4.根据list中map的某个属性值排序list

代码语言:javascript复制
        List<Map> list44= list4.stream()
                .sorted((map1, map2) -> map1.get("cnt").toString().compareTo(map2.get("cnt").toString())).collect(Collectors.toList());

5.根据list中map的某个属性值进行过滤

代码语言:javascript复制
       List<Map> collect1 = list6.stream().filter(map -> map.get("entStatus").toString().indexOf("在营") != -1).collect(Collectors.toList());

6.根据list中map的某个属性值进行求和

代码语言:javascript复制
      int sumZaiYing = collect1.stream().mapToInt(map -> Integer.valueOf(map.get("cnt").toString())).sum();

7.根据map的某个value来分类List<Map<String,Object>>

代码语言:javascript复制
Map<Object, List<Map<String, Object>>> age = list.stream().collect(Collectors.groupingBy(map -> map.get("age")));

8.根据对象的某个属性值来分类List

代码语言:javascript复制
Map<String, List<User>> collect1 = list2.stream().collect(Collectors.groupingBy(User::getProvince));

9.根据对象属性筛选最大值

代码语言:javascript复制
ApprovalLogVo approvalLogVo = onlyPass.stream().max(Comparator.comparing(ApprovalLogVo::getApprovalTime)).get();

 ApprovalLogVo maxVo= 
 onlyPass.stream().collect(Collectors.maxBy(Comparator.comparing(ApprovalLogVo::getApprovalTime))).get();

10.汇总集合中的某个对象属性

代码语言:javascript复制
public static void main(String[] args) {
    List<DistributedLockDO> list = new ArrayList<>();
    list.add(new DistributedLockDO("key1","ip11:80911",11L));
    list.add(new DistributedLockDO("key2","ip22:80922",11L));
    list.add(new DistributedLockDO("key3","ip33:80933",11L));

    List<Object> collect = list.stream().flatMap(lockDO -> Arrays.stream(lockDO.getLockValue().split(":"))).collect(Collectors.toList());
    System.out.println(collect.toString());
}

或者下面这种拆开写,可以过滤什么的,起手flatmap不好处理其他的逻辑

代码语言:javascript复制
        List<String> collect = list.stream().map(DistributedLockDO::getLockValue).filter(v -> StringUtils.isNotBlank(v))
                .flatMap(v -> Arrays.stream(v.split(":"))).collect(Collectors.toList());

输出为:[ip11, 80911, ip22, 80922, ip33, 80933]

11. Predicate

代码语言:javascript复制
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 5, 7, 12);
        List<Integer> integers = biggerThan5(list, integer -> integer > 5);
        System.out.println(integers);
    }

    public static List<Integer> biggerThan5(List<Integer> list, Predicate<Integer> predicate) {
        return list.stream().filter(predicate).collect(Collectors.toList());
    }

0 人点赞