java 8 lambda 表达式

本文详细介绍了Java 8 Stream API的六种创建方法,包括从集合、基础流包装、使用Stream.of()、Stream.generate()、Stream.iterate()以及Stream.Builder。同时,文章探讨了Collectors的各种实用操作,如求平均值、分组、字符串连接、求最大值和最小值,以及各种规约和转换操作。通过实例展示了如何在实际编程中有效利用Stream API进行数据处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、创建流的6种方法

         1.1集合,也是最常见的,

        List<Integer> list = new ArrayList();

        list.add(1);

        list.add(2);

        list.stream().collect(Collectors.summaryInt(Integer::intValue));

        1.2 boxed(),通过基础流包装而来

       IntStream i = IntStream.of(1,2,3);

       i.boxed().collect(Collectors.summingInt(Integer::intValue));

        1.3 Stream.of()

        Stream.of(1,2,3).collect(Collectors.summingInt(Integer::intValue));

        1.4 Stream.generate()

   Stream.generate(()->{return         1;}).limit(10).collect(Collectors.summingInt(Integer::intValue));

        1.5 Stream.itereate(0,x -> x+1).forEach(System.out::println);

        1.6 Stream.Builder

        Stream.Builder builder = Stream.builder();
        builder.accept(5);
        builder.add(1).add(2).add(3).build().forEach(System.out::println);

2、

3、

4、Collectors

4.1 求平均值,流中元素的个数

对流中的元素求平均值,该函数接受一个函数,将流中的元素转化位double,int ,或者long;

流中的元素不能位null,转换的时候会出现空指针异常。

public class Lambda {

    public static void main(String[] args) {

        List<Element> list = new ArrayList<>();
        list.add(new Element(1));
        list.add(new Element(2));
        list.add(new Element(3));
        System.out.println(list.stream().collect(Collectors.averagingInt(Element::getI)));
        System.out.println(list.stream().collect(Collectors.counting()));

        List<Integer> list1 = new ArrayList<>();
        list1.add(1);
        list1.add(2);
        list1.add(3);
        System.out.println(list1.stream().collect(Collectors.averagingInt(Integer::intValue)));
    }

    @Data
    @AllArgsConstructor
    static class Element {
        private int i;
    }
}

2.0
3
2.0

4.2 分组操作

 4.3 字符串的连接join

可以通过map函数抽取指定的字符串列

public static void main(String [] argc) {
List<String> stringList = new ArrayList<>();
        stringList.add("1");
        stringList.add("2");
        stringList.add("3");
        System.out.println(stringList.stream().collect(Collectors.joining()));
        System.out.println(stringList.stream().collect(Collectors.joining(",")));
        System.out.println(stringList.stream().collect(Collectors.joining(",","(",")")));
}

123
1,2,3
(1,2,3)

4.4 求取最大值和最小值

public class Lambda {

    public static void main(String[] args) {

        List<Element> list = new ArrayList<>();
        list.add(new Element(1,1,2));
        list.add(new Element(1,2,2));
        list.add(new Element(1,3,2));
        // 映射成key,原元素集合
        Map<Integer,List<Element>> map = list.stream().collect(Collectors.groupingBy(Element::getKey,Collectors.toList()));

        System.out.println(list.stream().collect(Collectors.maxBy(Comparator.comparingInt(Element::getKey))));
        System.out.println(list.stream().collect(Collectors.minBy(Comparator.comparingInt(Element::getKey))));
    }

    @Data
    @AllArgsConstructor
    static class Element {
        private int i;
        private int key;
        private int value;
    }
}

Optional[Lambda.Element(i=1, key=3, value=2)]
Optional[Lambda.Element(i=1, key=1, value=2)]

汇总

package com.example.demo.springboot.java8feature;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class Lambda {

    public static void main(String[] args) {

        List<Element> list = new ArrayList<>();
        list.add(new Element(1, 1, 2));
        list.add(new Element(1, 2, 2));
        list.add(new Element(1, 3, 2));
        // 映射成key,原元素集合
        Map<Integer, List<Element>> map = list.stream().collect(Collectors.groupingBy(Element::getKey, Collectors.toList()));

        // 分组
        // 分组,value 按照分组后,抽取元素指定的列,归拢成集合
        Map<Integer, List<Integer>> map1 = list.stream().collect(Collectors.groupingBy(Element::getI, Collectors.mapping(Element::getKey, Collectors.toList())));

        // 求最大,最小
        System.out.println(list.stream().collect(Collectors.maxBy(Comparator.comparingInt(Element::getKey))));
        System.out.println(list.stream().collect(Collectors.minBy(Comparator.comparingInt(Element::getKey))));
        // 分区
        Map<Boolean, List<Element>> booleanListMap = list.stream().collect(Collectors.partitioningBy(e -> {
            return e.getKey() % 2 == 0;
        }));
        // 分区,指定list集合类型
        Map<Boolean, List<Element>> booleanListMap1 = list.stream().collect(Collectors.partitioningBy(e -> {
            return e.getKey() % 2 == 0;
        }, Collectors.toList()));

        Map<Boolean, List<Element>> booleanListMap2 = list.stream().collect(Collectors.partitioningBy(e -> {
            return e.getKey() % 2 == 0;
        }, Collectors.toCollection(LinkedList::new)));

        // 规约操作
        list.stream().collect(Collectors.reducing(0, Element::getKey, (key1, key2) -> {
            return key1 + key2;
        }));

        // 转换成map
        list.stream().collect(Collectors.toMap(Element::getKey, Element::getValue));
        list.stream().collect(Collectors.toMap(Element::getKey, Element::getValue, (k1, k2) -> {
            return k1;
        }));
        list.stream().collect(Collectors.toMap(Element::getKey, Element::getValue, (k1, k2) -> {
            return k1;
        }, LinkedHashMap::new));

        // 转换成concurrentHashMap
        list.stream().collect(Collectors.toConcurrentMap(Element::getKey, Element::getValue));
        list.stream().collect(Collectors.toConcurrentMap(Element::getKey, Element::getValue, (k1, k2) -> {
            return k1;
        }));
        list.stream().collect(Collectors.toConcurrentMap(Element::getKey, Element::getValue, (k1, k2) -> {
            return k1;
        }, ConcurrentHashMap::new));

        // 求和 IntSummaryStatistics{count=3, sum=6, min=1, average=2.000000, max=3}
        System.out.println(list.stream().collect(Collectors.summarizingInt(Element::getKey)));
        // 求和 6
        System.out.println(list.stream().collect(Collectors.summingInt(Element::getKey)));

        IntStream i = IntStream.of(1, 2, 3);
        i.boxed().collect(Collectors.summingInt(Integer::intValue));

        Stream.of(1, 2, 3).collect(Collectors.summingInt(Integer::intValue));

        Stream.generate(() -> {
            return 1;
        }).limit(10).collect(Collectors.summingInt(Integer::intValue));
    }

    @Data
    @AllArgsConstructor
    static class Element {
        private int i;
        private int key;
        private int value;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值