JDK8 stream流學習

 jdk8之後引入對集合操作的工具steam,大大的簡化了collection的各種日常操作,結合lambda表達式,很大程度提高了開發效率

package com.ks3.cdn.util;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Test;

/**
 *JDK8  Stream特性
 * Created by chengbx on 2018/5/27.
 * Java 8 中的 Stream 是對集合(Collection)對象功能的增強,它專注於對集合對象進行各種非常便利、高效的聚合操作(aggregate operation),
 * 或者大批量數據操作 (bulk data operation)。Stream API 藉助於同樣新出現的 Lambda 表達式,極大的提高編程效率和程序可讀性。
 * 同時它提供串行和並行兩種模式進行匯聚操作,併發模式能夠充分利用多核處理器的優勢,使用 fork/join 並行方式來拆分任務和加速處理過程。
 * 通常編寫並行代碼很難而且容易出錯, 但使用 Stream API 無需編寫一行多線程的代碼,就可以很方便地寫出高性能的併發程序。
 * 所以說,Java 8 中首次出現的 java.util.stream 是一個函數式語言+多核時代綜合影響的產物。
 *  一、Stream API 的操作步驟:
 *
 * 1. 創建 Stream
 *
 * 2. 中間操作
 *
 * 3. 終止操作(終端操作)
 *
 * 4.   接口中的默認方法
 *      接口默認方法的”類優先”原則
 *      若一個接口中定義了一個默認方法,而另外一個父類或接口中
 *      又定義了一個同名的方法時
 *      1.選擇父類中的方法。如果一個父類提供了具體的實現,那麼
 *      接口中具有相同名稱和參數的默認方法會被忽略.
 *      2.接口衝突。如果一個父接口提供一個默認方法,而另一個接
 *      口也提供了一個具有相同名稱和參數列表的方法(不管方法
 *      是否是默認方法),那麼必須覆蓋該方法來解決衝突
 * 5.   新增的重複註解@Repeatble和類型註解
 *         java8新增了重複註解,其使用方式爲:
 @Repeatable(Authorities.class)
 public @interface Authority {
 String role();
 }
 public @interface Authorities {
 Authority[] value();
 }
 public class RepeatAnnotationUseNewVersion {
 @Authority(role="Admin")
 @Authority(role="Manager")
 publicvoiddoSomeThing(){ }
 }
 2.Java8爲ElementType枚舉增加了TYPE_PARAMETER、TYPE_USE兩個枚舉值,
 從而可以使用@Target(ElementType_TYPE_USE)修飾註解定義,這種註解被稱爲類型註解,
 可以用在任何使用到類型的地方
 */
/**
 * @author ning_mx
 * @date 15:56 2018/10/31
 * @description   JDK 8 stream操作測試
 */
public class StreamTest {
    Employee eee = new Employee("eeeeee",15,7120d) ;
    List<Employee> employees = Arrays.asList(
            new Employee("aaa",11,400d),
            new Employee("bbb",21,5200d),
            new Employee("ccc",13,5500d),
            new Employee("ddd",54,6400d),
            new Employee("eee",16,7100d),
            new Employee("fff",74,7120d),eee,eee,eee,
            new Employee("ggg",12,7150d)
    );
    /**
     * 創建stream
     */
    @Test
    public void test1(){
        //1. Collection 提供了兩個方法  stream() 與 parallelStream()
        List<String> list = new ArrayList<>();
        Stream<String> stream =  list.stream();
        Stream<String> parallelStream = list.parallelStream(); //獲取一個並行流
        //2. 通過 Arrays 中的 stream() 獲取一個數組流
        Integer[] nums = new Integer[10];
        Stream<Integer> stream1 = Arrays.stream(nums);
        //3. 通過 Stream 類中靜態方法 of()
        Stream<Integer> stream2 = Stream.of(1,2,3,4,5,6);
        //4. 創建無限流
        //迭代
        Stream<Integer> stream3 = Stream.iterate(0, (x) -> x + 2).limit(10);
        stream3.forEach(System.out::println);

        //生成
        Stream<Double> stream4 = Stream.generate(Math::random).limit(2);
        stream4.forEach(System.out::println);
    }
    /*
      篩選與切片
        filter——接收 Lambda , 從流中排除某些元素。
        limit——截斷流,使其元素不超過給定數量。
        skip(n) —— 跳過元素,返回一個扔掉了前 n 個元素的流。若流中元素不足 n 個,
        則返回一個空流。與 limit(n) 互補
        distinct——篩選,通過流所生成元素的 hashCode() 和 equals() 去除重複元素
     */
    //內部迭代:迭代操作 Stream API 內部完成
    @Test
    public void test2(){
        //中間操作,不會執行任何操作
        Stream<Employee> str = employees.stream().filter(e -> e.getAge()>30);
        //終止操作,一次性執行全部內容,即"惰性求值"
        //str.forEach(e -> System.out.println(e.getName()));
         employees.stream().filter((e) -> e.getAge()<30 && e.getSalary()<900d ).forEach((employee) -> {System.out.println(employee);System.out.println(employee.getName());});
    }
    //外部迭代
    @Test
    public void test3(){
        Iterator<Employee> it = employees.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }
    @Test
    public void test4(){
        employees.stream()
                .filter((e) -> {
                    System.out.println("短路!"); // &&  ||
                    return e.getSalary() >= 5000;
                }).limit(3)
                .forEach(System.out::println);
        employees.stream()
                .filter((e) ->  e.getSalary() >= 5000
                 ).limit(3)
                .forEach(System.out::println);
    }

    @Test
    public void test5(){
        employees.parallelStream()
                .filter((e) -> e.getSalary() >= 5000)
                .skip(2)
                .forEach(System.out::println);
    }
    @Test
    public void test6(){
        employees.stream()
                .distinct()
                .forEach(System.out::println);
    }

    /**
     * 映射
     * map -接收lambda,將元素轉換成其他形式獲取信息,接收一個函數作爲參數,
     該函數會被應用在每個元素上,並將其映射成一個新的元素。
     * flatmap-接收一個函數作爲參數,將流中的每個值都換成另一個流,然後把所有流連接成一個流
     */
    @Test
    public void test7(){
        List<String> list = Arrays.asList("aaa","bbb","ccc","ddd");
        list.stream().map((str) -> str.toUpperCase())
                .forEach((str) -> System.out.println(str));
        System.out.println("---------------");
        employees.stream().map(Employee::getName)
                .forEach((name) ->System.out.println(name));
    }
    /**
     * 排序
     * sorted()--自然排序(comparable)
     * sorted(Comparator com)--定製排序(Comparator)
     */
    @Test
    public void test8(){
        List<String> list = Arrays.asList("eee","ggg","ccc","ddd");
        list.stream().sorted().forEach(System.out::println);
        System.out.println("-------------------以下是定製排序-------------");
        employees.stream().sorted((e1,e2) ->{
            if (e1.getAge() ==e2.getAge()) {
                return e1.getName().compareTo(e2.getName());
            }else{
                return Integer.compare(e1.getAge(),e2.getAge());
            }
        }).forEach((employee) -> System.out.println(employee));
    }
    //3. 終止操作
    /*
        allMatch——檢查是否匹配所有元素
        anyMatch——檢查是否至少匹配一個元素
        noneMatch——檢查是否沒有匹配的元素
        findFirst——返回第一個元素
        findAny——返回當前流中的任意元素
        count——返回流中元素的總個數
        max——返回流中最大值
        min——返回流中最小值
        注意:流進行了終止操作後,不能再次使用
     */

    @Test
    public void test9(){
        /*boolean b = employees.stream().allMatch((emp) -> emp.getAge()>1);
        System.out.println(b);
        System.out.println("---------------");*/
        /*boolean b1 = employees.stream().anyMatch((emp) -> emp.getAge()==15);
        System.out.println(b1);
        System.out.println("---------------");
        boolean b2 = employees.stream().noneMatch((emp) -> emp.getAge()==10);
        System.out.println(b2);
        System.out.println("---------------");
        Optional<Employee> optional = employees.stream()
                .sorted((emp1, emp2) -> Double.compare(emp2.getSalary(),emp1.getSalary()))
                .findFirst();
        System.out.println(optional.get());
        System.out.println("---------------");
        Optional<Employee> optional1 = employees.parallelStream()
                .filter((emp) -> emp.getAge() >15).findAny();
        System.out.println(optional1);
        System.out.println("---------------");*/
        Long count = employees.parallelStream().filter((emp) -> emp.getAge() >15).count();
        System.out.println(count);
        Optional<Double> optiona3 = employees.stream()
                .map((emp) -> emp.getSalary()).max(Double::compareTo);
        System.out.println(optiona3.get());
        System.out.println("---------------");
        Optional<Employee> optiona4 = employees.stream()
                .min((e1,e2) ->Double.compare(e1.getSalary(),e2.getSalary()));
        System.out.println(optiona4);
    }

    /**
     * 歸約
     reduce(T identity, BinaryOperator) / reduce(BinaryOperator) ——可以將流中元素反覆結合起來,得到一個值。
     */
    @Test
    public void test10(){
        List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
        Integer sum = list.stream().reduce(0,(x,y) -> x+y);
        System.out.println(sum);//55
        System.out.println("---------------------");
        Optional<Double> sumSal = employees.stream().map(Employee::getSalary).reduce(Double::sum);
        System.out.println(sumSal);
    }
    /**
     * 收集:
     * collect——將流轉換爲其他形式。接收一個 Collector接口的實現,用於給Stream中元素做彙總的方法
     */
    //將employee集合中name值取出來放入集合中 aaa bbb ccc ddd eee fff ggg
    @Test
    public void test11(){
        List list = employees.stream().map(Employee::getName).collect(Collectors.toList());
        list.forEach(System.out::println);
    }
    @Test
    public void test12(){
        Set set = employees.stream().map(Employee::getName).collect(Collectors.toSet());
        set.forEach(System.out::println);
    }
    @Test
    public void test13(){
        HashSet hashSet = employees.stream().map(Employee::getName)
                .collect(Collectors.toCollection(HashSet::new));
        hashSet.forEach(System.out::println);
    }
    //獲取集合中元素的個數  7
    @Test
    public void test14(){
        long count =  employees.stream().collect(Collectors.counting());
        System.out.println(count);
        System.out.println("----------------");
        //獲取工資平均值
        Double avgMoney = employees.stream()
                .collect(Collectors.averagingDouble((emp) -> emp.getSalary()));
        System.out.println(avgMoney);//6210.0
        System.out.println("----------------");
        //工資總和
        Double sumMoney = employees.stream()
                .collect(Collectors.summingDouble(Employee::getSalary));
        System.out.println(sumMoney);
        //最大值
        Optional<Employee> optional= employees.stream()
                .collect(Collectors.maxBy((emp1,emp2) -> Double.compare(emp1.getSalary(),emp2.getSalary())));
        System.out.println(optional.get());//Employee{name='ggg', age=12, salary=7150.0}
        //最小值
        Optional<Double> minMoney = employees.stream()
                .map(Employee::getSalary).collect(Collectors.minBy(Double::compare));
        System.out.println(minMoney.get());
    }
    //分組
    @Test
    public void test15(){
        Map<Integer, List<Employee>> collect = employees.stream().collect(Collectors.groupingBy(Employee::getAge));
        for (Integer key : collect.keySet()){
            System.out.println(key +"--->"+collect.get(key));
        }
    }
    //分區
    @Test
    public  void test16(){
        Map<Boolean,List<Employee>> map =employees.stream()
                .collect(Collectors.partitioningBy((e) -> e.getSalary() >6000));
        System.out.println(map);
        System.out.println(map.get(false));
        //{false=[Employee{name='aaa', age=11, salary=5000.0}, Employee{name='bbb', age=21, salary=5200.0},
        // Employee{name='ccc', age=13, salary=5500.0}],
        // true=[Employee{name='ddd', age=54, salary=6400.0}, Employee{name='eee', age=16, salary=7100.0},
        // Employee{name='fff', age=74, salary=7120.0}, Employee{name='ggg', age=12, salary=7150.0}]}

    }

    @Test
    public void test17(){
        DoubleSummaryStatistics dss =employees.stream()
                .collect(Collectors.summarizingDouble(Employee::getSalary));
        //求平均值
        System.out.println(dss.getAverage());
        //求最大值
        System.out.println(dss.getMax());
        //求和
        System.out.println(dss.getSum());
    }
    @Test
    public void test18(){
        String name = employees.stream().map(Employee::getName).collect(Collectors.joining(","));
        System.out.println(name);//aaa,bbb,ccc,ddd,eee,fff,ggg
    }
}

class Employee{
    private String name ;
    private int age;
    private Double salary;

    public Employee(String name, int age, Double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Double getSalary() {
        return salary;
    }

    public void setSalary(Double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", salary=" + salary +
                '}';
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章