zoukankan      html  css  js  c++  java
  • 多级分组

    package com.ant.jdk8.chap06;
    
    import java.util.Arrays;
    import java.util.List;
    import java.util.Map;
    import java.util.stream.Collectors;
    
    public class DishGroupDemo {
        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);
        }
    }
    

    package com.ant.jdk8.chap06;
    
    import java.util.Arrays;
    import java.util.List;
    import java.util.Map;
    import java.util.stream.Collectors;
    
    public class DishGroupDemo {
        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<CaloricLevel,List<Dish>> dishesByCaloricLeval =
                    menu.stream().collect(
                            Collectors.groupingBy(
                                    d->{ if(d.getCalories()<=400) return CaloricLevel.DIET;
                                    else if(d.getCalories()<=700) return CaloricLevel.NORMAL;
                                    else return CaloricLevel.FAT;})
                    );
            System.out.println(dishesByCaloricLeval);
        }
    }

    多级分组:

    package com.ant.jdk8.chap06;
    
    import java.util.Arrays;
    import java.util.List;
    import java.util.Map;
    import java.util.stream.Collectors;
    
    public class DishGroupDemo {
        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,Map<CaloricLevel,List<Dish>>> dishesByTypeCaloricLevel =
                    menu.stream().collect(
                            Collectors.groupingBy(Dish::getType,
                            Collectors.groupingBy(d->{
                                if(d.getCalories()<=400) return CaloricLevel.DIET;
                            else if(d.getCalories()<=700) return CaloricLevel.NORMAL;
                            else return CaloricLevel.FAT;})));
            System.out.println(dishesByTypeCaloricLevel);
        }
    }

  • 相关阅读:
    [Python] socket实现TFTP上传和下载
    [Python] socket发送UDP广播实现聊天室功能
    [C#] 建立UDP连接、发送广播
    [Python] Scrapy爬虫框架入门
    [Python] 常见的排序与搜索算法
    [Python] 数据结构--实现顺序表、链表、栈和队列
    [Python] Django框架入门5——静态文件、中间件、上传图片和分页
    [Python] Django框架入门4——深入模板
    [Python] Django框架入门3——深入视图
    [Python] Django框架入门2——深入模型
  • 原文地址:https://www.cnblogs.com/i-hard-working/p/9595755.html
Copyright © 2011-2022 走看看