zoukankan      html  css  js  c++  java
  • Stream<T>

    返回低热量的菜肴名称,并按照卡路里排序:

    package com.ant.jdk8.chap04;
    
    import java.util.Arrays;
    import java.util.Comparator;
    import java.util.List;
    
    public class StreamDemo {
        public static void main(String[] args) {
            List<Dish> menu = Arrays.asList(
                    new Dish("pork", false, 800, Type.MEAT),
                    new Dish("beef", false, 700, Type.MEAT),
                    new Dish("chicken", false, 400, Type.MEAT),
                    new Dish("french fries", true, 530, Type.OTHER),
                    new Dish("rice", true, 350, Type.OTHER),
                    new Dish("season fruit", true, 120, Type.OTHER),
                    new Dish("pizza", true, 550, Type.OTHER),
                    new Dish("prawns", false, 300, Type.FISH),
                    new Dish("salmon", false, 450, Type.FISH) );
            menu.stream()
                    .filter(d->d.getCalories()<400)
                    .sorted(Comparator.comparing(Dish::getCalories))
                    .map(Dish::getName)
                    .forEach(name-> System.out.println(name));
        }
    }
    

    按照类别对菜肴进行分组:

    package com.ant.jdk8.chap04;
    
    import java.util.Arrays;
    import java.util.Comparator;
    import java.util.List;
    import java.util.Map;
    import java.util.stream.Collectors;
    
    public class StreamDemo {
        public static void main(String[] args) {
            List<Dish> menu = Arrays.asList(
                    new Dish("pork", false, 800, Type.MEAT),
                    new Dish("beef", false, 700, Type.MEAT),
                    new Dish("chicken", false, 400, Type.MEAT),
                    new Dish("french fries", true, 530, Type.OTHER),
                    new Dish("rice", true, 350, Type.OTHER),
                    new Dish("season fruit", true, 120, Type.OTHER),
                    new Dish("pizza", true, 550, Type.OTHER),
                    new Dish("prawns", false, 300, Type.FISH),
                    new Dish("salmon", false, 450, Type.FISH) );
            Map<Type,List<Dish>> dishesByType = menu.stream().collect(Collectors.groupingBy(Dish::getType));
            System.out.println(dishesByType);
        }
    }
    

  • 相关阅读:
    php之面向对象(2)
    PHP ON 阿里云的环境配置攻略
    InitPHP框架搭建高可用WEB应用
    PHP移动互联网开发笔记(6)——MySQL数据库基础回顾[1]
    5 个不用 Bootstrap 的理由
    http中get与post的区别
    django的CSRF保护机制
    博客园项目开发中的难点
    centos7.5静态网页基于nginx建站(python3.6 mysql5.7 nginx安装以及配置)
    python3面向对象常用特性知识点
  • 原文地址:https://www.cnblogs.com/i-hard-working/p/9576562.html
Copyright © 2011-2022 走看看