Stream Demos

Collection

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

    public String getName() {
        return name;
    }

    public Integer getCalories() {
        return calories;
    }

    public boolean isVegetarian() {
        return vegetarian;
    }

    public Type getType() {
        return type;
    }

    private final String name;

    private final boolean vegetarian;

    private final Integer calories;

    private final Type type;

    public enum Type {MEAT, FISH, OTHER}
}


static 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("prawns", false, 300, Dish.Type.FISH)
            , new Dish("salmon", false, 450, Dish.Type.FISH) );

//dish數量
menu.stream().collect(counting())
menu.stream().count()

//calories最多的dish
menu.stream().collect(maxBy(comparing(Dish::getCalories))).ifPresent(d -> System.out.println(d.getCalories()));

class Transaction{
    private String currency;

    private int value;


    public Transaction(String currency, int value) {
        this.currency = currency;
        this.value = value;
    }

    public Transaction(String currency) {
        this.currency = currency;
    }

    public String getCurrency() {
        return currency;
    }

    public int getValue() {
        return value;
    }

    public Boolean isExpensive(){
        return this.value > 500?Boolean.TRUE:Boolean.FALSE;
    }
}

static List<Transaction> transactions = Arrays.asList(new Transaction("RMB",200)
            , new Transaction("RMB", 600)
            , new Transaction("USD", 100)
            , new Transaction("USD", 800));
//按貨幣分組
Map<String, List<Transaction>> transactionsSameCurrency = transactions.stream().collect(groupingBy(Transaction::getCurrency));

//分成高價和低價兩組
Map<Boolean, List<Transaction>> transactionsSameCurrency = transactions.stream().collect(groupingBy(Transaction::isExpensive));

//交易的總價格
int sum = transactions.stream().collect(summingInt(Transaction::getValue));

//數據
int[] i = {1, 3, 5};
        IntStream intStream = Arrays.stream(i);
        System.out.println(intStream.sum());

//從文件中獲取Stream。計算文本里單詞數量,並且去重
static void linesStream(){
        try (Stream<String> lines
                     = Files.lines(Paths.get("c://work//a.txt"), Charset.defaultCharset())) {
            long count1 = lines
                    .flatMap(line -> Arrays.stream(line.split(" ")))
                    .distinct()
                    .count();
            System.out.println(count1);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
//輸出偶數
static void iterateStream(){
        Stream.iterate(0, n -> n+2)
                .limit(10)
                .forEach(System.out::println);
    }
//fibonnacci,當前值是前兩個數的和
static void fibonnacci(){
        Stream.iterate(new int[]{0, 1}, n -> new int[]{n[1], n[0]+n[1]})
                .limit(20)
                .map(t -> t[0])
                .forEach(System.out::println);
    }
//後去20個隨機數
static void generateStream(){
        Stream.generate(Math::random)
                .limit(20)
                .forEach(System.out::println);
    }

//fibonnacci,當前值是前兩個數的和
static void generateStateStream(){
        IntSupplier intSupplier = new IntSupplier() {

            private int privious = 0;
            private int current = 1;
            
            @Override
            public int getAsInt() {
                int oldPrevious = this.privious;
                this.privious = this.current;
                this.current = oldPrevious + this.current;
                return oldPrevious;
            }
        };

        IntStream.generate(intSupplier)
                .limit(10)
                .forEach(System.out::println);

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