java8 stream常用操作

filter

List<Employee> employeeIs1 = list.stream().filter(e -> e.getName().contains("Alex")).collect(Collectors.toList());

map

 List<Leader> leaders = list.stream().filter(employee -> employee.getSalary() == 2000).map(employee -> {
            Leader leader = new Leader();
            leader.setName(employee.getName());
            leader.setSalary(employee.getSalary());
            return leader;
        }).peek(System.out::println).collect(Collectors.toList());

flatMap

 protected static final List<List<Employee>> listFlat = Arrays.asList(
            Arrays.asList(new Employee(1, "Alex", 1000),
                    new Employee(2, "Michael", 2000)),
            Arrays.asList(new Employee(3, "Jack", 1500),
                    new Employee(4, "Owen", 1500)),
            Arrays.asList(new Employee(5, "Denny", 2000)));
List<Employee> employeesAll = listFlat.stream().flatMap(Collection::stream).collect(Collectors.toList());

sorted

 //根据Id反向排序 从大到小排序
        list.stream().sorted(Comparator.comparingLong(Employee::getId).reversed()
        ).collect(Collectors.toList()).forEach(System.out::println);

max min

 Employee employeeMaX = list.stream().max(Comparator.comparing(Employee::getSalary)).orElse(null);

 Employee employeeMin = list.stream().min(Comparator.comparing(Employee::getSalary)).orElse(null);
allMatch noMatch anyMatch
//列表中所有元素都比配我们的条件
boolean isAllMatch = list.stream().allMatch(e -> e.getSalary() >=1000);

//列表中任一元素比配我们的条件
boolean isManyMatch = list.stream().anyMatch(e -> e.getSalary() ==1500);

//列表中没有一个元素比配我们的条件
boolean isNoneMatch2 = list.stream().noneMatch(e -> e.getSalary() > 3000);

distinct

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

reduce

double dSum4 = list.stream().map(Employee::getSalary).reduce(AddUtils::add).get();

double dSum5 = list.stream().map(Employee::getSalary).reduce(0.00,Double::sum);

collectors

//转换成连接字符串
        String strName = list.stream().map(Employee::getName).collect(Collectors.joining(","));

//转换成Set集合
        Set<String> setName = list.stream().map(Employee::getName).collect(Collectors.toSet());

//转换成Vector
        Vector<String> vectorName = list.stream().map(Employee::getName).collect(Collectors.toCollection(Vector::new));

//转换成List
        List<String> listName = list.stream().map(Employee::getName).collect(Collectors.toList());

//转换成Map
        Map<Long, String> mapName = list.stream().collect(Collectors.toMap(employee -> employee.getId(), employee -> employee.getName()));

count

long lCount2 = list.stream().filter(employee -> employee.getSalary() == 2000).count();

partitioningBy

  //把工资大于1500放在true的列表里,小于或等于1500放在false列表里
       Map<Boolean, List<Employee>> map = list.stream().collect(Collectors.partitioningBy(employee -> {
           return employee.getSalary() > 1500;
       }));

groupingBy

//根据薪酬获取员工列表
Map<Double,List<Employee>> map = list.stream()
                .collect(Collectors.groupingBy(Employee::getSalary));

//根据薪酬获取员工数量
        Map<Double,Long> map2 = list.stream()
                .collect(Collectors.groupingBy(Employee::getSalary,Collectors.counting()));

//根据薪酬获取员工薪酬总数
        Map<Double,Double> map3 = list.stream()
              .collect(Collectors.groupingBy(Employee::getSalary,Collectors.summingDouble(Employee::getSalary)));

file

//写入文件
        PrintWriter printWriter = new PrintWriter(Files.newBufferedWriter(Paths.get("E://text.txt")));
        list.stream().forEach(printWriter::println);
        printWriter.close();
//读取文件
        List<String> content = Files.lines(Paths.get("E://text.txt")).peek(System.out::println).collect(Collectors.toList());

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值