Stream介紹

一、Stream介紹  

現在有這樣的需求:有個菜單list,菜單裏面非常多的食物列表,只選取小於400卡路里的並且按照卡路里排序,然後只想知道對應的食物名字。

代碼:

package com.cy.java8;

public class Dish {

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

    public Dish(String name, boolean vegetarian, int calories, Type type) {
        this.name = name;
        this.vegetarian = vegetarian;
        this.calories = calories;
        this.type = type;
    }


    public String getName() {
        return name;
    }

    public boolean isVegetarian() {
        return vegetarian;
    }

    public int getCalories() {
        return calories;
    }

    public Type getType() {
        return type;
    }

    public enum Type {MEAT, FISH, OTHER}


    @Override
    public String toString() {
        return "Dish{" +
                "name='" + name + '\'' +
                ", vegetarian=" + vegetarian +
                ", calories=" + calories +
                ", type=" + type +
                '}';
    }
}
View Code
package com.cy.java8;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class SimpleStream {

    public static void main(String[] args) {
        //have a dish list (menu)
        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, 300, Dish.Type.FISH),
                new Dish("salmon", false, 450, Dish.Type.FISH));

        List<String> lowerCaloriesDish = getLowerCaloriesDish(menu);
        System.out.println(lowerCaloriesDish);

        List<String> lowerCaloriesDish2 = getLowerCaloriesDish2(menu);
        System.out.println(lowerCaloriesDish2);
    }

    /**
     * 用Stream的方式去寫
     */
    private static List<String> getLowerCaloriesDish2(List<Dish> menu){
        return menu.stream().filter(dish -> dish.getCalories() < 400)
                            .sorted(Comparator.comparing(d->d.getCalories()))
                            .map(Dish::getName)
                            .collect(Collectors.toList());
    }

    /**
     * 以前的寫法
     * 獲取卡路里小於400的食物列表,並且排好序,只返回食物的名字;
     * @param menu
     * @return
     */
    private static List<String> getLowerCaloriesDish(List<Dish> menu){
        List<Dish> lowerCalories = new ArrayList<>();
        //filter and get calories less 400
        for(Dish  dish : menu){
            if(dish.getCalories() < 400 ){
                lowerCalories.add(dish);
            }
        }
        //sort
        lowerCalories.sort((o1, o2) -> o1.getCalories() - o2.getCalories());

        List<String> lowerCaloreisDishList = new ArrayList<>();
        for(Dish dish : lowerCalories){
            lowerCaloreisDishList.add(dish.getName());
        }
        return lowerCaloreisDishList;
    }
}

console打印:

[season fruit, prawns, rice]
[season fruit, prawns, rice]

Stream:升級版的api,處理集合等等,有個好處是可以並行處理,而且不需要關心並行處理的細節,

 

 

 

 

--

 

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