Java新特性Stream流

2021/9/21 22:12:04

本文主要是介绍Java新特性Stream流,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

文章目录

    • 一、概述
    • 二、Stream流的作用
    • 三、Stream流的作用对象
    • 四、Stream的案例分析
      • 1.排序
      • 2.过滤
      • 3.修改
      • 4.求总数
      • 5.循环遍历
      • 6.去重
      • 7.求最大


一、概述

Stream是jdk1.8的新特性,可以结合lambda表达式使用提升开发的效率和性能。

二、Stream流的作用

  1. 用于对集合迭代的增强处理。
  2. 可以对集合数组进行更高效的聚合操作,比如:分组、过滤、排序、元素的追加等。
  3. 解决传统开发过程中,jdk对集合或者数组API不足的问题,因为在早期的API的开发过程中,对集合或者Map的操作其实还是比较单一和缺乏,在jdk1.8之后就参考了很多语言的一些对数组和集合的操作的API,比如javascript的数组的reduce、map、sort、filter等等。

三、Stream流的作用对象

集合(List,Set)===(数组 ,map)

四、Stream的案例分析

package com.wcf.stream;

import com.wcf.pojo.Category;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 胡萝卜
 * @createTime 2021/9/21 17:08
 */
public class StreamDemo01 {
    public static void main(String[] args) {
        //创建一个List集合 
        List<Category> categoryList=new ArrayList<>();
        categoryList.add(new Category(1L,"Java","Java",0L,1));
        categoryList.add(new Category(2L,"Php","Php",0L,2));
        categoryList.add(new Category(3L,"JavaScript","JavaScript",0L,3));
        categoryList.add(new Category(4L,"Python","Python",0L,10));
        categoryList.add(new Category(5L,"Go","Go",0L,8));
        categoryList.add(new Category(6L,"Ruby","Ruby",0L,4));
    }
}

1.排序

        //stream完成排序
        List<Category> categories = categoryList.stream().sorted(new Comparator<Category>() {
            @Override
            public int compare(Category o1, Category o2) {
                return o1.getSort() - o2.getSort();
            }
        }).collect(Collectors.toList());
        //打印
        categories.forEach(System.out::println);
    }
}

2.过滤

    //过滤filter 过滤是把过滤条件满足的找到
        List<Category> categories = categoryList.stream().filter(cate -> cate.getId().equals(2L)).collect(Collectors.toList());
        //打印
        categories.forEach(System.out::println);

3.修改

 //map改变集合中每个元素的信息
        List<Category> categories = categoryList.stream().map(cate -> {
            cate.setSubtitle(cate.getSubtitle()+"yy");
            return cate;
        }).collect(Collectors.toList());
        //打印
        categories.forEach(System.out::println);

4.求总数

//求总数
        long count = categoryList.stream().count();
        System.out.println(count);

5.循环遍历

 //循环遍历
        categoryList.stream().forEach(category -> {
            System.out.println(category);
        });

6.去重

 //distinct可以去重元素,如果集合是对象,如果要distinct,就对象要返回相同的hashcode和equals是true
        List<Category> categories = categoryList.stream().distinct().collect(Collectors.toList());
        categories.forEach(System.out::println);

7.求最大

  //求最大
        Optional<Category> optionalCategory = categoryList.stream().max(new Comparator<Category>() {
            @Override
            public int compare(Category o1, Category o2) {
                return o1.getSort() - o2.getSort();
            }
        });
        if (optionalCategory.isPresent()){
            System.out.println(optionalCategory.get());
        }


这篇关于Java新特性Stream流的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程