java stream 流对象 总结

一、 引言

Java8/JDK8是Oracle在2014年发布的正式版,我现在才来系统熟识Java8的特性,真是有点感觉有点跟不上节奏呀!
虽说感慨,但依旧为时不晚。不是有催人的语句:只要你愿意开始,什么时候都不晚嘛!
分块来学习吧!
在这里插入图片描述

二、 .stream()

Stream(流)是一个来自数据源的元素队列并支持聚合操作

Stream不会存储元素,而是一系列的操作
流的来源:集合、数组、I/O channel、产生器generator 等
聚合操作:filter, map, reduce, find, match, sorted等。
使用.stream()变成流,然后进行一系列聚合操作。
创建完毕Stream以后,我们就可以给这个Stream添加各种操作了,下表列出了Stream支持的所有操作:
在这里插入图片描述
在这里插入图片描述

生成流的方式有两种方式

stream() − 为集合创建串行流。
parallelStream() − 为集合创建并行流。

三、 聚合函数

3.1 forEach

Stream 提供了新的方法 ‘forEach’ 来迭代流中的每个数据。
代码中System.out::println使用要看这

效果如下:

3.2 map

map 方法用于映射每个元素到对应的结果,以下代码片段使用 map 输出了元素对应的平方数:

List<Integer> list = Arrays.asList(1,2,3,4,5);
list.stream().map(x->x*x).forEach(System.out::println);

执行截图:
在这里插入图片描述

应用:对象中取一个值来形成一个流可用map

List<SortData> sortDataList = new ArrayList<>();
SortData user1 = new SortData(100L);
SortData user2 = new SortData(200L);
sortDataList.add(user1);
sortDataList.add(user2);

sortDataList.stream().map(item -> item.getData()*100).forEach(System.out::println);

效果截图:
在这里插入图片描述

当然前面我创建了SortData 的对象如下:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class SortData implements Serializable {
    
    
    private Long  data;
}

3.3 filter

filter 方法用于通过设置的条件过滤出元素。以下代码片段使用 filter 方法过滤出空字符串:

@Test
public void filterTest() {
    
    
    List<String> list = Arrays.asList("a","","b","","cc","x","dd");
    System.out.println("过滤后展示:");
    list.stream().filter(item -> !item.trim().isEmpty()).forEach(System.out::println);
    System.out.println("过滤前个数:"+list.size());
    System.out.println("过滤后个数:"+list.stream().filter(item -> !item.trim().isEmpty()).count());
}

运行效果:
在这里插入图片描述

3.4 limit

limit 方法用于获取指定数量的流。

List<String> list = Arrays.asList("a","","b","","cc","x","dd");
list.stream().limit(3).forEach(System.out::println);

运行效果截图:得到了前3个
在这里插入图片描述

3.5 sorted

sorted 方法用于对流进行排序。

    @Test
    public void streamTest2() {
    
    
        List<Integer> list = Arrays.asList(1,3,4,2,6,2);
        List<Integer> collect = list.stream().sorted().collect(Collectors.toList());
        System.out.println("Stream自然排序");
        for (Integer i : collect) {
    
    
            System.out.print(i+" ");
        }
        List<Integer> collect2 = list.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        System.out.println(" ");
        System.out.println("Stream倒序");
        for (Integer i : collect2) {
    
    
            System.out.print(i+" ");
        }
        System.out.println(" ");
        System.out.println("List倒序");
        list.sort(((o1, o2) -> o1 > o2 ? -1 : 1));
        list.forEach(System.out::println);
    }

运行效果
在这里插入图片描述

3.6 parallelStream

parallelStream 是流并行处理程序的代替方法。

@Test
public void parallelStreamTest() {
    
    
    List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
    // 获取空字符串的数量
    long count = strings.parallelStream().filter(string -> string.isEmpty()).count();
    System.out.println(count);
}

实现效果如下:

3.7 Collectors

Collectors 类实现了很多归约操作,例如将流转换成集合和聚合元素。Collectors 可用于返回列表或字符串:

@Test
public void collectorsTest() {
    
    
    List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
    //每个非空字符加一个a
    List<String> collect1 = strings.stream().filter(item -> !item.isEmpty()).map(item -> item + 'a').collect(Collectors.toList());
    System.out.println(collect1.toString());
    String collect2 = strings.stream().filter(item -> !item.isEmpty()).collect(Collectors.joining("#"));
    System.out.println(collect2);
}

执行效果:
在这里插入图片描述

3.8 统计

一些产生统计结果的收集器非常有用。它们主要用于int、double、long等基本类型上,它们可以用来产生类似如下的统计结果。

@Test
public void totalStreamTest1() {
    
    
    List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5,12);
    IntSummaryStatistics intSummaryStatistics = numbers.stream().mapToInt(o->o).summaryStatistics();
    System.out.println("列表中平均数 : " + intSummaryStatistics.getAverage());
    System.out.println("列表中个数 : " + intSummaryStatistics.getCount());
    System.out.println("列表中最大数 : " + intSummaryStatistics.getMax());
    System.out.println("列表中平均数 : " + intSummaryStatistics.getMin());
    System.out.println("列表中总数 : " + intSummaryStatistics.getSum());
}

运行效果:
在这里插入图片描述

四、 复杂条件处理

4.1 分组求和

@Test
public void groupAndSumTest() {
    
    
    List<SortData> list = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
    
    
        SortData sortData = new SortData();
        sortData.setData(Math.round(Math.random()*100));
        sortData.setDataType("测试"+i);
        list.add(sortData);
    }
    for (int i = 0; i < 10; i++) {
    
    
        SortData sortData = new SortData();
        sortData.setData(Math.round(Math.random()*100));
        sortData.setDataType("测试"+i);
        list.add(sortData);
    }
    list.forEach(System.out::println);
    Map<String, LongSummaryStatistics> groupByType = list.stream().collect(Collectors.groupingBy(SortData::getDataType, Collectors.summarizingLong(SortData::getData)));
    System.out.println("测试1:"+groupByType.get("测试1"));
    System.out.println("测试1的分数和:"+groupByType.get("测试1").getSum());
    System.out.println("测试1中最大分数:"+groupByType.get("测试1").getMax());
    System.out.println("测试1中最小分数:"+groupByType.get("测试1").getMin());
    System.out.println("测试1中平均分数:"+groupByType.get("测试1").getAverage());
    System.out.println("测试1个数:"+groupByType.get("测试1").getCount());
}

运行结果:
在这里插入图片描述

4.2 组合排序

@Test
public void groupAndSortTest() {
    
    
    Student student1 = new Student("小红", "女", 18,57);
    Student student2 = new Student("小啊", "女", 21,67);
    Student student3 = new Student("小是", "女", 35,79);
    Student student4 = new Student("小的", "女", 14,96);
    Student student5 = new Student("小我", "男", 25,88);
    Student student6 = new Student("小发", "男", 32,88);
    Student student7 = new Student("小给", "男", 16,88);
    Student student8 = new Student("小好", "男", 22,66);
    List<Student> list = new ArrayList<>();
    list.add(student1); list.add(student3); list.add(student5); list.add(student7);
    list.add(student2); list.add(student4); list.add(student6); list.add(student8);
    //在一个班级,每个人都有自己的分数,怎样在java中使用代码对所有人的分数先根据性别排序再根据分数年龄排序。
    System.out.println("组合排序:");
    System.out.println("第一种:");
    List<Student> collectSortedMore = list.stream()
            .sorted(Comparator.comparing(Student::getSex).reversed()
                    .thenComparing(Student::getScore).reversed()
                    .thenComparing(Student::getAge).reversed())
            .collect(Collectors.toList());
    collectSortedMore.stream().forEach(System.out::println);
    System.out.println("第二种:");
    list.sort(Comparator.comparing(Student::getSex).reversed()
            .thenComparing(Student::getScore).reversed()
            //两者比较发现.reversed()作用的是整个结果集不是单个字段
            .thenComparing(Student::getAge));
    list.stream().forEach(student -> System.out.println(student));
}

在这里插入图片描述

4.3 求极值

场景:
在一个班级,每个人都有自己的分数,怎样在java中使用代码求这个班级的平均分、最大分、最小分。

方法:
先使用summarizingLong和summarizingInt对分数汇总,接着分别获取极值
获取平均值——getAverage()
获取最大值——getMax()
获取最小值——getMin()
获取总分——getSum()
获取个数——getCount()

@Test
public void numTest() {
    
    
    Student student1 = new Student("小红", "女", 18,57);
    Student student2 = new Student("小啊", "女", 21,67);
    Student student3 = new Student("小是", "女", 35,79);
    Student student4 = new Student("小的", "女", 14,96);
    Student student5 = new Student("小我", "男", 25,65);
    Student student6 = new Student("小发", "男", 32,85);
    Student student7 = new Student("小给", "男", 16,96);
    Student student8 = new Student("小好", "男", 22,66);
    List<Student> list = new ArrayList<>();
    list.add(student1); list.add(student3); list.add(student5); list.add(student7);
    list.add(student2); list.add(student4); list.add(student6); list.add(student8);
    System.out.println("求极值:");
    LongSummaryStatistics collectStudentsSummary = list.stream().collect(Collectors.summarizingLong(Student::getScore));
    System.out.println("平均值=" + collectStudentsSummary.getAverage());
    System.out.println("个数=" + collectStudentsSummary.getCount());
    System.out.println("最大值=" + collectStudentsSummary.getMax());
    System.out.println("最小值=" + collectStudentsSummary.getMin());
    System.out.println("总和=" + collectStudentsSummary.getSum());

}

结果展示:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/L_994572281_LYA/article/details/120527684
今日推荐