javaStream流学习

Stream

Java8 API添加了一个新的抽象称为流 Stream ,可以让

你以一种声明的方式处理数据。

Stream 使用一种类似用 SQL 语句从数据库查询数据的直

观方式来提供一种 Java 集合运算和表达的高阶抽

。Stream API可以极大提高Java程序员的生产力,让

程序员写出高效率、干净、简洁的代码。这种风格将要

处理的元素集合看作一种流, 流在管道中传输, 并且可

以在管道的节点上进行处理, 比如筛选, 排序,聚合

等。

元素流在管道中经过中间操作(intermediate

operation)的处理,最后由最终操作(terminal

operation)得到前面处理的结果。

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

操作

元素是特定类型的对象,形成一个队列。 Java中的

Stream并不会存储元素,而是按需计算

数据源 流的来源。 可以是集合,数组等。

聚合操作 类似SQL语句一样的操作, 比如filter, map,

reduce, find, match, sorted等。Java 8 中的 Stream 是对集合(Collection)对象功能的

增强,它专注于对集合对象进行各种非常便利、高效的

聚合操作(aggregate operation),或者大批量数据操作

(bulk data operation)。

获取流

  1. 可以通过Conllection系列集合提供的顺序

    流stream()或并行流parallelStream()

    public class TestStreamAPI1 { 
    // 创建
    Stream public void test1() { 
    // 1、可以通过Conllection系列集合提供的顺序 流stream()或并行流parallelStream() 
    List<String> list = new ArrayList<>(); Stream<String> stream1 = list.stream(); 
    stream1 = list.parallelStream(); 
    // 2、通过Arrays中的静态方法stream()获取数 据流 Integer ints[] = new Integer[10]; Stream<Integer> stream2 = Arrays.stream(ints); // 3、通过Stream类中的静态方法of() 
    Stream<String> stream3 = Stream.of("aa", "bb", "cc"); 
    String str[] = new String[10]; Stream<String> stream4 = Stream.of(str) } }
    
  2. 通过Arrays中的静态方法stream()获取数

    据流

    package com.lwf.homeWork;
    
    import java.util.Arrays;
    
    /**
     * @author lwf
     * @title: StreamWork
     * @projectName 10_24Code
     * @description: TODO
     * @date 2020/10/2515:35
     */
    public class StreamWork {
        public static void main(String[] args) {
            printAbs(new int[]{2,-5,6,-2,0,-6});
            printPow(new int[]{2,-5,6,-2,0,-6});
        }
        //打印绝对值
        public static void printAbs(int[] num){
            Arrays.stream(num).map(n->Math.abs(n)).forEach(System.out::println);
        }
        //打印平方
        public static void printPow(int[] num){
            Arrays.stream(num).map(n->n*n).forEach(System.out::println);
        }
    }
    
    
  3. 通过Stream类中的静态方法of()

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4gz8ZCEt-1603616308346)(https://raw.githubusercontent.com/lwfqw/shsxt/master/10_24Code/img/image-20201025162946815.png)]

过滤流

fiter过滤

传入一个转化为流的容器存放对象,返回一个布尔值判断是否加入流

package com.lwf.stream;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
 * @author lwf
 * @title: Demo1
 * @projectName 10_24Code
 * @description: 流过滤懒执行:获取结果时才执行
 * @date 2020/10/2411:10
 */
public class Demo1 {
    public static void main(String[] args) {
        List<Panda> list=new ArrayList<>();
        list.add(new Panda(1,"jxa","cd"));
        list.add(new Panda(2,"jxs","cd"));
        list.add(new Panda(3,"jxd","sh"));
        list.add(new Panda(4,"jxf","cd"));
        list.stream().filter(r->r.getLocate().equals("cd")).limit(3).skip(0).forEach(c-> System.out.println(c));
        System.out.println(list.parallelStream().findAny().get());
    }
}

limit(n)

指定流前n条数据

skip(n)

跳过前n条数据

distinct()

去重复

map()

传入一个方法接口,接口处理接收对象提取对象特定信息加入新的流

foreach(System.out::println)

遍历打印流

count()流长度

package com.lwf.homeWork;

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

/**
 * @author lwf
 * @title: Goods
 * @projectName 10_24Code
 * @description: TODO
 * @date 2020/10/2515:48
 */
public class Goods {
    public static void main(String[] args) {
        List<Goods> list=new ArrayList<>();
        list.add(new Choes(12, "adidas", Type.choes));
        list.add(new Phone(43, "huawei", Type.phone));
        list.add(new Phone(66, "chenguang", Type.pen));
        list.add(new Choes(19, "nike", Type.choes));
        list.add(new Choes(18, "nike", Type.choes));
        list.add(new Phone(49, "xiaomi", Type.phone));
        list.add(new Choes(34, "huili", Type.choes));
        list.add(new Phone(36, "meizu", Type.phone));
        list.add(new Choes(7, "hailan", Type.choes));
        //鞋子销量排行
        list.stream().filter(g->g.getType()==Type.choes).sorted((o,a)->{
            return Integer.compare(o.getSell(), a.getSell());
        }).forEach(System.out::println);
        //手机产品
        list.stream().filter(g->g.getType()==Type.phone).forEach(System.out::println);
        //几种鞋子
        System.out.println(list.stream().filter(g -> g.getType() == Type.choes).map(g -> g.getName()).distinct().count());
    }
    private int sell;
    private String name;
    private Type type;

    public Goods(int sell, String name, Type type) {
        this.sell = sell;
        this.name = name;
        this.type = type;
    }

    public int getSell() {
        return sell;
    }

    public void setSell(int sell) {
        this.sell = sell;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    @Override
    public boolean equals(Object obj) {
        Goods goods = (Goods)obj;
        return this.getName().equals(goods.getName());
    }

    @Override
    public String toString() {
        return "Goods{" +
                "sell=" + sell +
                ", name='" + name + '\'' +
                ", type=" + type +
                '}';
    }
}
enum Type{
    phone,choes,pen;
}
class Phone extends Goods{
    public Phone(int sell, String name, Type type) {
        super(sell, name, type);
    }
}
class Choes extends Goods{
    public Choes(int sell, String name, Type type) {
        super(sell, name, type);
    }
}
class Pen extends Goods{
    public Pen(int sell, String name, Type type) {
        super(sell, name, type);
    }
}

流是懒加载,所以对流无论进行多少次操作都会取结果时一起打印·(foreach和count)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值