什么是分类和下游,只能用分级之间的区别

问题描述 投票:1回答:3

我是新来的Java 8和溪流收藏家试图了解什么是两者之间的基本区别?

因为无论是代码产生相同的结果。一个使用qazxsw POI和返回groupingBy(分类,HashMap中::新,下游);

下面是代码

return groupingBy(classifier, toList());

输出:

public class Grouping {
    enum CaloricLevel { DIET, NORMAL, FAT };

    public static void main(String[] args) {
        System.out.println("Dishes grouped by type: " + groupDishesByType());
        System.out.println("Dish names grouped by type: " + groupDishNamesByType());
    }


    private static Map<Type, List<Dish>> groupDishesByType() {
        return Dish.menu.stream().collect(groupingBy(Dish::getType));
    }

    private static Map<Type, List<String>> groupDishNamesByType() {
        return Dish.menu.stream().collect(groupingBy(Dish::getType, mapping(Dish::getName, toList())));
    }
}

dish.Java

Dishes grouped by type: {MEAT=[pork, beef, chicken], OTHER=[french fries, rice, season fruit, pizza], FISH=[prawns, salmon]}
Dish names grouped by type: {MEAT=[pork, beef, chicken], OTHER=[french fries, rice, season fruit, pizza], FISH=[prawns, salmon]}
java java-8 java-stream collectors
3个回答
0
投票

在你的两个例子

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 name;
    }

    public static final List<Dish> menu = 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));

    public static final Map<String, List<String>> dishTags = new HashMap<>();

    static {
        dishTags.put("pork", asList("greasy", "salty"));
        dishTags.put("beef", asList("salty", "roasted"));
        dishTags.put("chicken", asList("fried", "crisp"));
        dishTags.put("french fries", asList("greasy", "fried"));
        dishTags.put("rice", asList("light", "natural"));
        dishTags.put("season fruit", asList("fresh", "natural"));
        dishTags.put("pizza", asList("tasty", "salty"));
        dishTags.put("prawns", asList("tasty", "roasted"));
        dishTags.put("salmon", asList("delicious", "fresh"));
    }
}

返回值相同,因为只有.collect(groupingBy(Dish::getType)); .collect(groupingBy(Dish::getType, mapping(Dish::getName, toList()))); 类返回toString()Dish方法。尝试添加更多信息以name mehtod,你会看到差别。

一般情况下,使用toString()只分级允许组对象,就像在你的第一个例子。但是,使用groupingBy与分类和下游允许您将不仅仅是你的对象更多。例如,您可以按类型组平均热量:

goupingBy

或者找到最热量的菜每种类型:

.collect(groupingBy(Dish::getType, averagingInt(Dish::getCalories));  // Map<Type, Double>

经常.collect(groupingBy(Dish::getType, maxBy(Comparator.comparingInt(Dish::getCalories))); // Map<Type, Optional<Dish>> 用作下游本身为双重分组​​(按类型和如果它是vegeterian):

groupingBy

1
投票

如果这是一个问题

因为无论是代码产生相同的结果。一个使用返回groupingBy(分类,toList());并返回groupingBy(分类,HashMap中::新,下游); ?

groupingBy(功能分类器,收集器下游)

还有的类型,可变性,串行化,或者地图的线程安全返回任何保证。

groupingBy(功能分类器,供应商mapFactory,集电极下游)

由收集器所产生的地图与所提供的工厂函数创建。

唯一的区别是,当你使用.collect(groupingBy(Dish::getType, groupingBy(Dish::isVegetarian)); // Map<Type, Map<Boolean, List<Dish>>> groupingBy是根据您的供应商的逻辑创建的mapFactory(可能是定制的,一成不变的,同步等。)


0
投票

什么是两者之间的基本区别?

主要的区别是,你已经在整理收集器之前的中间步骤进行的映射。你已经用他们不同,虽然该方法是Map的签名。

虽然一方面,您所指定的groupingBymapper统称为:

downstream

另一方面,在.collect(Collectors.groupingBy(Dish::getType, // classifier Collectors.mapping(Dish::getName, // mapper <<<< difference here Collectors.toList()))) // downstream 在使用

deault implementation of groupingBy

可扩展为类似某种格式:

.collect(Collectors.groupingBy(Dish::getType)) 
© www.soinside.com 2019 - 2024. All rights reserved.