jdk8使用流去重以及常用函数

本文介绍了如何在JDK8中使用流(Stream)来实现对象和字符串的去重操作,包括利用TreeSet的特性以及toMap函数进行去重。同时,也探讨了在流处理中进行对象过滤和求和的方法。

对象去重TreeSet
去重规则,只要第一次出现的元素(和Map的(oldValue, newValue) -> oldValue一致)

Student student1 = new Student("张三",20,"周");
Student student2 = new Student("李四",30,"上号");
Student student3 = new Student("王五",18,"阿西吧");
Student student4 = new Student("氨纶",19,"阿西吧");
Student student5 = new Student("氨纶",19,"阿西吧1");

List<Student> collect = Arrays.asList(student1,student2,student3,student4,student5);
List<Student> unique = collect.stream().collect(Collectors.collectingAndThen(
               Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Student::getName))), ArrayList::new));

toMap去重

Collectors.toMap(User::getUserId, Function.identity(), (oldValue, newValue) -> oldValue)
这里选择保留的元素:(oldValue第一个元素   newValue第二次出现的元素)

(oldValue, newValue) -> oldValue			//保留旧值(第一次传入的值)
(oldValue, newValue) -> newValue			//保留新值(如果再次出现,新值替换旧值,保留最新的值)
List<User> userList = new ArrayList<>();
        userList.add(new User("a", "xiaoming",12));
        userList.add(new User("b", "xiaoming",13));
        userList.add(new User("d", "xiaoming",15));
        userList.add(new User("a", "xiaoming",14));
       
        System.out.println("利用Collectors.toMap去重:");
        //利用Collectors.toMap去重
        userList.stream()
        		//或者这样写 Collectors.toMap(m -> m.getUserId(), 
                .collect(Collectors.toMap(User::getUserId, 
                			Function.identity(), (oldValue, newValue) -> oldValue))
                .values()
                .stream()
                .forEach(System.out::println); //打印

对象过滤

//获取person集合中的所有大于18周岁,并排序
List<Person> personList= new ArrayList();
personList.add(new Person(1, "name1", 10));
personList.add(new Person(2, "name2", 21));
personList.add(new Person(5, "name5", 55));
personList.add(new Person(3, "name3", 34));
personList.add(new Person(4, "name4", 6));
personList.stream()
        .filter(p -> p.getAge() >= 18)//获取所有18岁以上的用户
        //.sorted(Comparator.comparing(Person::getAge))//依年纪升序排序
        .sorted(Comparator.comparing(Person::getAge).reversed())//依年纪降序排序
        .collect(Collectors.toList());
System.out.println(personList);
//List根据某个字段过滤、排序
listStu.stream().filter(student -> student.getSex().equals("女")).sorted(Comparator.comparing(Student::getName)).collect(Collectors.toList());

//List根据某个字段分组
Map> sexGroupMap = listStu.stream().collect(Collectors.groupingBy(Student::getSex));

//如果Map中多个名称相同,则studentId用逗号间隔
Map studentNameIdMap = listStu.stream().collect(toMap(Student::getName,Student::getStuId,(s,a)->s+","+a));

字符串去重

List<String> list = list.stream().distinct().collect(Collectors.toList());

求和

list.stream().mapToDouble(User::getHeight).sum()//和 
list.stream().mapToDouble(User::getHeight).max()//最大 
list.stream().mapToDouble(User::getHeight).min()//最小 
list.stream().mapToDouble(User::getHeight).average()//平均值
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值