java8 stream小实例

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class Person {

    String userName;
    double gongzi;
    int age;
    int sex;


}
import org.checkerframework.checker.units.qual.C;

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

public class tt {

    public static void main(String[] args) {
        List<Person> personList = new ArrayList<Person>();
        personList.add(new Person("Tom", 8900, 12, 1));
        personList.add(new Person("Jack", 7000, 44, 2));
        personList.add(new Person("Lily", 7800, 123, 1));
        personList.add(new Person("Anni", 8200, 222, 1));
        personList.add(new Person("Owen", 9500, 33, 1));
        personList.add(new Person("Alisa", 7900, 66, 1));
        //3900+2000+2800+3200+4500+3900=8700+
        List<Integer> list = Arrays.asList(7, 6, 9, 3, 8, 2, 1);
        // 遍历输出符合条件的元素
        list.stream().filter(x -> x > 6).forEach(System.out::println);
        // 匹配任意(适用于并行流)返回第一个
        System.out.println(list.parallelStream().filter(x -> x > 6).findFirst());
        // 匹配任意(适用于并行流)
        boolean b = list.stream().anyMatch(x -> x > 6);
        System.out.println(b);
        // 是否包含符合特定条件的元素
        //高于8000的员工姓名
        List<String> collect = personList.stream().filter(x -> x.getGongzi() > 8000).map(Person::getUserName).collect(Collectors.toList());
        System.out.println(collect);
        List<String> Stringlist = Arrays.asList("adnm", "admmt", "pot", "xbangd", "weoujgsd");
        //最长的字符串
        System.out.println(Stringlist.stream().max(Comparator.comparing(String::length)));

        List<Integer> list3 = Arrays.asList(7, 6, 9, 4, 11, 6);

        // 自然排序
        Optional<Integer> max22 = list.stream().max(Integer::compareTo);
        System.out.println(max22);
        // 自定义排序
        Optional<Integer> max1 = list.stream().max(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        });
        //获取工资最大的人
        Optional<Person> max2 = personList.parallelStream().max(Comparator.comparingDouble(x -> x.getGongzi()));
        System.out.println(max22.get() + "-------" + max2.get());
        //计算Integer集合中大于6的元素的个数。
        long count = list.parallelStream().filter(x -> x > 6).count();
        System.out.println(count);


//        映射(map/flatMap)
        // 改变原来员工集合的方式
        List<Person> collect1 = personList.stream().map(x -> {
            x.setGongzi(x.getGongzi() + 999);
            return x;
        }).collect(Collectors.toList());
        System.out.println(collect1);

        List<String> list33 = Arrays.asList("m,k,l,a", "1,3,5,7");
        //放进去还是两个单独的字符串
        System.out.println(list33.size());

        List<String> collect2 = list33.stream().flatMap(s -> {

            String[] split = s.split(",");
            Stream<String> stream = Arrays.stream(split);
            return stream;
        }).collect(Collectors.toList());
        System.out.println(collect2);
        //求Integer集合的元素之和、乘积和最大值。
        List<Integer> list44 = Arrays.asList(0, 3, 2, 8, 11, 4);
        //求和1
        System.out.println(list44.stream().reduce((x, y) -> x + y));
        //求和2
        System.out.println(list44.stream().reduce(Integer::sum));
        //求和3
        System.out.println(list44.stream().reduce(0, Integer::sum));
        //乘积
        System.out.println(list44.stream().reduce((x, y) -> x * y));
        //求最大值
        System.out.println(list44.stream().reduce((x, y) -> x > y ? x : y));
        //最大值2
        System.out.println(list44.stream().reduce(1, Integer::min));
        //求所有员工的工资之和
        System.out.println("求所有员工的工资之和" + personList.stream().collect(Collectors.summarizingDouble(Person::getGongzi)));
        //和最高工资
        //1.过滤金额
        System.out.println(personList.stream().map(Person::getGongzi).reduce(Double::sum));
        //2.不过滤金额
        System.out.println(personList.stream().reduce(0.0, (sum, p) -> sum += p.getGongzi(), (sum1, sum2) -> sum1 + sum2));
        //
        System.out.println(personList.stream().reduce(0.0, (sum, p) -> sum += p.getGongzi(), Double::sum));
        //求工资最大值
//        Double reduce = personList.stream().reduce(0.0, (max, p) -> max > p.getGongzi() ? max : p.getGongzi(), Double::max);
        System.out.println(personList.stream().reduce(0.0, (max, p) -> max > p.getGongzi() ? max : p.getGongzi(), Double::max));
        //Collectors提供了一系列用于数据统计的静态方法:
//        计数:count
        System.out.println(personList.stream().collect(Collectors.counting()));
//        平均值:averagingInt、averagingLong、averagingDouble
        System.out.println(personList.stream().collect(Collectors.averagingDouble(Person::getGongzi)));
//        最值:maxBy、minBy
        System.out.println(personList.stream().map(Person::getGongzi).collect(Collectors.maxBy(Double::compare)));
//        求和:summingInt、summingLong、summingDouble
//        统计以上所有:summarizingInt、summarizingLong、summarizingDoubl
        DoubleSummaryStatistics collect4 = personList.stream().collect(Collectors.summarizingDouble(Person::getGongzi));
        System.out.println("max---" + collect4.getMax() + "----min" + collect4.getMin());
        // 求总数
        System.out.println(personList.stream().collect(Collectors.counting()));
        System.out.println(personList.stream().collect(Collectors.averagingDouble(Person::getGongzi)));
        //求最大值
        System.out.println(personList.stream().map(Person::getGongzi).collect(Collectors.maxBy(Double::compareTo)));
        //按性别分组
        Map<Integer, List<Person>> collect3 = personList.stream().collect(Collectors.groupingBy(Person::getSex));
        System.out.println(collect3);
//        分组(partitioningBy/groupingBy)
//        partitioningBy分区
        Map<Boolean, List<Person>> collect5 = personList.stream().collect(Collectors.partitioningBy(x -> x.getAge() > 50));
        Map<Integer, Map<Integer, List<Person>>> collect6 = personList.stream().collect(Collectors.groupingBy(Person::getAge, Collectors.groupingBy(Person::getSex)));
        //groupingBy分组

        //人名组串
        System.out.println(personList.stream().map(Person::getUserName).collect(Collectors.joining(",")));
        for (Person person : personList) {
            System.out.println(person);
        }
        //reducing方法
        System.out.println(personList.stream().collect(Collectors.reducing(0.0, Person::getGongzi, (x, y) -> x + y - 5000)));
        System.out.println(personList.stream().map(Person::getGongzi).reduce((x, y) -> y + x - 5000));

        // 按工资升序排序(自然排序)
        System.out.println(personList.stream().sorted(Comparator.comparingDouble(Person::getGongzi)).map(Person::getUserName).collect(Collectors.joining(",")));
        // 按工资倒序排序 reversed代表倒序
        System.out.println(personList.stream().sorted(Comparator.comparing(Person::getGongzi).reversed()).map(Person::getUserName).collect(Collectors.joining(",")));
        // 先按工资再按年龄升序排序
        System.out.println(personList.stream().sorted(Comparator.comparing(Person::getAge).thenComparing(Person::getGongzi)).map(Person::getUserName).collect(Collectors.joining(",")));
        // 先按工资再按年龄自定义排序(降序)
        System.out.println(personList.stream().sorted((p1, p2) -> {
            if (p1.getGongzi() > p2.getAge()) {
                return p1.getAge() - p2.getAge();
            } else {
                return p1.getAge() + p2.getAge();
            }
        }).map(Person::getUserName).collect(Collectors.joining(",")));
        //提取/组合
        String[] arr1 = {"a", "b", "c", "d"};
        String[] arr2 = {"d", "e", "f", "g"};
        Stream<String> arr11 = Stream.of(arr1);
        Stream<String> arr22 = Stream.of(arr2);
        // concat:合并两个流 distinct:去重
        System.out.println(Stream.concat(arr11,arr22).distinct().collect(Collectors.toList()));
        System.out.println(Stream.iterate(1,x->x+2).limit(5).collect(Collectors.toList()));
        System.out.println(Stream.iterate(1,x->x+2).skip(1).limit(5).collect(Collectors.toList()));
    }
}

数组转集合

int[] a = {1,2,34,5};
Arrays.stream(hand).boxed().collect(Collectors.toList())

数组转集合,再按照规定拆分成几段

List<List<Integer>> collect = Stream.iterate(0, f -> f + 1)
					.limit(hand.length)
					.parallel()
					.map(a -> Arrays.stream(hand).boxed().collect(Collectors.toList()).parallelStream().skip(a * groupSize).limit(groupSize).collect(Collectors.toList()))
					.filter(b -> !b.isEmpty())
					.collect(Collectors.toList());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值