java统计

package com.company;

import java.util.*;
import java.util.stream.Collectors;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

public class Main {



    public static void main(String[] args) {
        //Collection和List
        List<String>list =new ArrayList<String>();
        list.add("b1");
        list.add("b2");
        list.add("b3");
        Iterator<String> it =list.iterator();
        while (it.hasNext()){
            String data =it.next();
            if (data.equals("b3")){
                it.remove(); //单个对象去重
            }
        }

        List<User> userList =new ArrayList<>();
        User user1 =new User("1",2);
        User user2 =new User("2",3);
        User user3 =new User("2",3);
        User user4 =new User("2",4);
        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        userList.add(user4);
        HashMap a=new HashMap<>();
        a.put("1",2);
        a.put("1",2);
        System.out.println(a);

        List<User>collect1 =userList.stream().filter(item1->item1.getUsername()>1 &&item1.getUsername()==2).collect(Collectors.toList());//多条件筛选
        List<User>collect =userList.stream().filter(item ->!"2".equals(item.getUsername())).collect(Collectors.toList());//过滤Name等于2
        Set<Integer> test =userList.stream().map(User::getUsername).collect(Collectors.toSet());
        List<User> unique1 = userList.stream().collect(
                collectingAndThen(
                        toCollection(() -> new TreeSet<>(comparing(User::getUsername))), ArrayList::new));//单属性去重
        List<User> collect2 = userList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o-> o.getUsername()+ ";" + o.getId()))),ArrayList::new));//多属性去重
        userList.stream().map(User::getId).collect(Collectors.joining(","));//只可以插入集合中的字符串等类型
        Map<String, User> studentMap = userList.stream().collect(Collectors.toMap(User::getId, s -> s, (s1, s2) -> s1));//list转换为map
        Map<Integer, List<User>> classMap = userList.stream().collect(Collectors.groupingBy(User::getUsername));//排序和删除重复相当于groupby
//多条件分组
        Map<Integer, Map<String, List<User>>> prodMap= userList.stream().collect(Collectors.groupingBy(User::getUsername, Collectors.groupingBy(item -> {
            if(item.getUsername() < 3) {//当username小于3时设置为other
                 item.setId("1");
                return "1";
            }else {
                return "other";//设置值为other
            }
        })));
        Map<String, Long> pr = userList.stream().collect(Collectors.groupingBy(User::getId, Collectors.counting()));//求总数
        Map<Integer, Integer> prg = userList.stream().collect(Collectors.groupingBy(User::getUsername, Collectors.summingInt(User::getUsername)));//求和
        Map<Integer, Set<Integer>> prag = userList.stream().collect(Collectors.groupingBy(User::getUsername, Collectors.mapping(User::getUsername, Collectors.toSet())));//联合收集器

        System.out.println(collect);
        System.out.println(prg);
        System.out.println(pr);
        System.out.println(prodMap);
        System.out.println(classMap);
        System.out.println(collect1);
        System.out.println(test);
        System.out.println(collect2);
        System.out.println(unique1);

//        List<User> list3 =Arrays.asList(
//                new User("1",1),
//                new User("2",1),
//                new User("2",1));
//        NoRepeatList a=new NoRepeatList();
//
//        System.out.println(a.NoRepeatList(list3));




    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值