JAVA高级篇--Stream流

1.方法应用

对象方法引用: 类名::实例方法. (参数1,参数2)->参数1.实例方法(参数2)

package com.lpt.demo01;

import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

public class Test01 {
    public static void main(String[] args) {
//        Function function=(str)->{
//            return str.length();
//        };
//        Function function=String::length;
//        Integer l = function.apply("hello");
//        System.out.println(l);

//        BiFunction booleanBiFunction=(t,u)->{
//            return t.equals(u);
//        };
//        Boolean apply = booleanBiFunction.apply("hello", "world");
//        System.out.println(apply);

//        Supplier supplier=()->{
//            return new String("hello");
//        };
        //Function function=(n)->new People(n);
        Function function=People::new;
        People people = function.apply("hello");
        System.out.println(people);
    }

}
class People{
    private String name;

    public People() {
    }

    public People(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                '}';
    }
}

2.Stream流

Java8的两个重大改变,一个是Lambda表达式,另一个就是本节要讲的Stream API表达式。==Stream 是Java8中处理集合的关键抽象概念==,它可以对集合进行非常复杂的查找、过滤、筛选等操作.

2.1 为什么使用Stream流

当我们需要对集合中的元素进行操作的时候,除了必需的添加、删除、获取外,最典型的就是集合遍历。我们来体验 集合操作数据的弊端,需求如下:

一个ArrayList集合中存储有以下数据:
需求:1.拿到所有姓张的 2.拿到名字长度为3个字的 3.打印这些数据

代码如下:

public class My {
    public static void main(String[] args) {
// 一个ArrayList集合中存储有以下数据:张无忌,周芷若,赵敏,张强,张三丰
// 需求:1.拿到所有姓张的 2.拿到名字长度为3个字的 3.打印这些数据
        ArrayList list = new ArrayList<>();
        Collections.addAll(list, "张无忌", "周芷若", "赵敏", "张强", "张三丰");
// 1.拿到所有姓张的
        ArrayList zhangList = new ArrayList<>(); // {"张无忌", "张强", "张三丰"}
        for (String name : list) {
            if (name.startsWith("张")) {
                zhangList.add(name);
            }
        }
// 2.拿到名字长度为3个字的
        ArrayList threeList = new ArrayList<>(); // {"张无忌", "张三丰"}
        for (String name : zhangList) {
            if (name.length() == 3) {
                threeList.add(name);
            }
        }
// 3.打印这些数据
        for (String name : threeList) {
            System.out.println(name);
        }
    }
}

分析:

这段代码中含有三个循环,每一个作用不同:

  1. 首先筛选所有姓张的人;

  2. 然后筛选名字有三个字的人;

  3. 最后进行对结果进行打印输出。

每当我们需要对集合中的元素进行操作的时候,总是需要进行循环、循环、再循环。这是理所当然的么?不是。循环 是做事情的方式,而不是目的。每个需求都要循环一次,还要搞一个新集合来装数据,如果希望再次遍历,只能再使 用另一个循环从头开始。

那Stream能给我们带来怎样更加优雅的写法呢?

 Stream的更优写法

public class Test {
    public static void main(String[] args) {
        ArrayList list = new ArrayList<>();
        Collections.addAll(list, "张无忌", "周芷若", "赵敏", "张强", "张三丰","何线程");
        list.stream()
                .filter(item->item.startsWith("张"))
                .filter(item->item.length()==3)
                .forEach(item-> System.out.println(item));
    }
}

2.2Stream流的原理

注意:Stream和IO流(InputStream/OutputStream)没有任何关系,请暂时忘记对传统IO流的固有印象!

Stream流式思想类似于工厂车间的“生产流水线”,Stream流不是一种==数据结构==,==不保存数据==,而是对数据进行==加工 处理==。Stream可以看作是流水线上的一个工序。在流水线上,通过多个工序让一个原材料加工成一个商品。

JAVA高级篇--Stream流_第1张图片

 Stream不存在数据,只对数据进行加工处理。

2.3如何获取Stream流对象

JAVA高级篇--Stream流_第2张图片

 

package com.lpt.demo02;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;

public class Test03 {
    public static void main(String[] args) {
        List list = new ArrayList<>();
        list.add("刘德华");
        list.add("张学友");
        list.add("郭富城");
        list.add("黎明");
        Stream stream = list.stream();

        //通过Arrays数组工具类获取Stream对象
        int[] arr={3,4,5,63,2,34};
        IntStream stream1 = Arrays.stream(arr);

        //使用Stream类中of方法
        Stream stream2 = Stream.of("hello", "world", "spring", "java");

        //LongStream
        LongStream range = LongStream.range(1, 10);

        //上面都是获取的串行流。还可以获取并行流。如果流中的数据量足够大,并行流可以加快处速度。
        Stream stringStream = list.parallelStream();

        //
//        stringStream.forEach(item-> System.out.println(item));
        stringStream.forEach(System.out::println);

    }
}

2.4 Stream流中常见的api

中间操作api: 一个操作的中间链,对数据源的数据进行操作。而这种操作的返回类型还是一个Stream对象。

终止操作api: 一个终止操作,执行中间操作链,并产生结果,返回类型不在是Stream流对象。

JAVA高级篇--Stream流_第3张图片

 (1)filter / foreach / count

static List personList = new ArrayList();
private static void initPerson() {
    personList.add(new Person("张三", 8, 3000));
    personList.add(new Person("李四", 18, 5000));
    personList.add(new Person("王五", 28, 7000));
    personList.add(new Person("孙六", 38, 9000));
}

class Person {
    private String name;
    private Integer age;
    private String country;
    private char sex;

    public Person(String name, Integer age, String country, char sex) {
        this.name = name;
        this.age = age;
        this.country = country;
        this.sex = sex;
    }
}
List personList = new ArrayList<>();
personList.add(new Person("欧阳雪",18,"中国",'F'));
personList.add(new Person("Tom",24,"美国",'M'));
personList.add(new Person("Harley",22,"英国",'F'));
personList.add(new Person("向天笑",20,"中国",'M'));
personList.add(new Person("李康",22,"中国",'M'));
personList.add(new Person("小梅",20,"中国",'F'));
personList.add(new Person("何雪",21,"中国",'F'));
personList.add(new Person("李康",22,"中国",'M'));

(2) map | sorted

map--接收Lambda,将元素转换成其他形式或提取信息。接收一个函数作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素。

JAVA高级篇--Stream流_第4张图片

 //对流中元素排序 
        personList.stream()
                  .sorted((o1,o2)->o1.getAge()-o2.getAge())
                  .forEach(System.out::println);


        //集合中每个元素只要名.map--->原来流中每个元素转换为另一种格式。
//        personList.stream()
//                 .map(item->{
//                     Map m=new HashMap<>();
//                     m.put("name",item.getName());
//                     m.put("age",item.getAge());
//                     return m;
//                 })
//                  .forEach(System.out::println);

 (3) min max

JAVA高级篇--Stream流_第5张图片

 

获取员工中年龄最大的人

        //查找最大年龄的人. max终止操作
        Optional max  = personList.stream().max((o1, o2) -> o1.getAge() - o2.getAge());
        System.out.println(max.get());

(4)规约reduce

JAVA高级篇--Stream流_第6张图片

 //求集合中所有人的年龄和。
        Optional reduce = personList.stream()
                .map(item -> item.getAge())
                .reduce((a, b) -> a + b);
        System.out.println(reduce.get());
        
        
                Integer reduce = personList.stream()
                .map(item -> item.getAge())
                .reduce(10, (a, b) -> a + b);
        System.out.println(reduce);

(5)collect搜集 match find

        //findFirst  match
//        Optional first = personList.parallelStream().filter(item -> item.getSex() == 'F').findAny();
        boolean b = personList.parallelStream().filter(item -> item.getSex() == 'F').noneMatch(item -> item.getAge() >= 20);

        System.out.println(b);
        //搜集方法 collect 它属于终止方法
        //年龄大于20且性别为M
//        List collect = personList.stream()
//                .filter(item -> item.getAge() > 20)
//                .filter(item -> item.getSex() == 'M')
//                .collect(Collectors.toList());
//
//        System.out.println(collect);

3. 新增了日期时间类

旧的日期时间的缺点:

  1. 设计比较乱: Date日期在java.util和java.sql也有,而且它的时间格式转换类在java.text包。

  2. 线程不安全。

JAVA高级篇--Stream流_第7张图片

新增类:

LocalDate: 表示日期类。yyyy-MM-dd

LocalTime: 表示时间类。 HH:mm:ss

LocalDateTime: 表示日期时间类 yyyy-MM-dd t HH:mm:ss sss

DatetimeFormatter:日期时间格式转换类。

Instant: 时间戳类。

Duration: 用于计算两个日期类

package com.lpt.demo03;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class Test05 {
    public static void main(String[] args) {
        LocalDate now = LocalDate.now();
        LocalDate of = LocalDate.of(2000, 1, 18);
//        System.out.println(now);
//        System.out.println(of);
        LocalTime now1 = LocalTime.now();
        LocalTime of1 = LocalTime.of(20, 4, 40);
//        System.out.println(now1);
//        System.out.println(of1);
        LocalDateTime now2 = LocalDateTime.now();
        LocalDateTime of2 = LocalDateTime.of(2000,1,18,20,6,50);
        Duration between = Duration.between(of2,now2);
        System.out.println(between.toDays());

        DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate parse = LocalDate.parse("1999-12-12", dateTimeFormatter);//把字符串转换为日期格式
        String format = parse.format(dateTimeFormatter);
        System.out.println(format);
    }
}

你可能感兴趣的:(java,开发语言)