Stream流 初級使用

Stream流

提供更好操作的集合庫

  1. Guava
  2. Apach Commons Collections
  3. lambdaj - Mario Fusco

image.png

初始操作

stream stream流

List<String> threeHighCaloricDishNames =
            Dish.menu
            //生成stream
            .stream()
            //篩選
            .filter(d->d.getCalories()>300)
            //提取
            .map(Dish::getName)
            //截斷
            .limit(3)
            //生成
            .collect(Collectors.toList());
System.out.println(threeHighCaloricDishNames);

parallelStream 多核流

List<String> threeHighCaloricDishNames =
    Dish.menu
	//多內核使用的Stream
	.parallelStream()
    //篩選
    .filter(d->d.getCalories()>300)
    //提取
    .map(Dish::getName)
    //截斷
    .limit(3)
    //生成
    .collect(Collectors.toList());
System.out.println(threeHighCaloricDishNames);

Map進行流操作

Map<Integer,Integer> map = new HashMap<>(10);
map.put(1,10);
map.put(2,20);
map.put(3,30);
map.put(4,30);
	map.entrySet()
       .stream()
       .forEach(var->{
           val key = var.getKey();
           val value = var.getValue();
           System.out.println("key:"+key+"    value:"+value);
       });

/**
stream 結果:
key:1    value:10
key:2    value:20
key:3    value:30
key:4    value:30

parallelStream 結果:
key:4    value:30
key:1    value:10
key:2    value:20
key:3    value:30

*/

中間操作

image.png

filter 篩選

map 提取

limit 截斷

sorted 排序

List<Integer> list = Arrays.asList(2, 4, 1, 3, 7, 5, 9, 6, 8,7);
List<Integer> sortedList = list.parallelStream()
    //篩選元素大於10
    .filter(number-> number>5)
    //遞減
    .sorted( (i1, i2) -> i2.compareTo(i1) )
    //自然序列排序,遞增
    //.sorted()
    .collect(Collectors.toList());
System.out.println(sortedList);
/**
[9, 8, 7, 7, 6]
*/

distinct 去重

List<Integer> list1 = Arrays.asList(2, 4, 1, 3, 7, 5, 9, 6, 8,7);
List<Integer> sortedList = list1.parallelStream()
    //篩選元素大於10
    .filter(number-> number>5)
    //自然序列排序,遞增
    .sorted()
    //去重
    .distinct()
    .collect(Collectors.toList());

/**
[6, 7, 8, 9]
*/

終端操作

image.png

forEach遍歷

collect 返回集合

.collect(Collectors.toList());

count 返回long

long count =
        //數據源
        Dish.menu.stream()
        //篩選
        .filter(d->d.getCalories()>300)
        //去重
        .distinct()
        //提取三個
        .limit(3)
        //生成
        .count();
System.out.println(count);

菜單實體

package com.java8.stream.entity;
import lombok.Data;

import java.util.*;

@Data
public class Dish {

    private final String name;
    private final boolean vegetarian;
    private final int calories;
    private final Type type;

    public Type getType() {
        return type;
    }

    public enum Type { MEAT, FISH, OTHER }

    public static final List<Dish> menu =
            Arrays.asList( new Dish("pork", false, 800, Dish.Type.MEAT),
                           new Dish("beef", false, 700, Dish.Type.MEAT),
                           new Dish("chicken", false, 400, Dish.Type.MEAT),
                           new Dish("french fries", true, 530, Dish.Type.OTHER),
                           new Dish("rice", true, 350, Dish.Type.OTHER),
                           new Dish("season fruit", true, 120, Dish.Type.OTHER),
                           new Dish("pizza", true, 550, Dish.Type.OTHER),
                           new Dish("prawns", false, 400, Dish.Type.FISH),
                           new Dish("salmon", false, 450, Dish.Type.FISH));
}

打賞一下

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