亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

分類器和下游和只使用分類器有什么區別

分類器和下游和只使用分類器有什么區別

森欄 2022-06-04 15:00:29
我是 Java 8 的新手,流式收集器試圖了解兩者之間的基本區別是什么?因為這兩個代碼都產生了相同的結果。一個使用return groupingBy(classifier, toList());并返回 groupingBy(classifier, HashMap::new,downstream);這是代碼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())));    }}輸出: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]}菜.javapublic 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;    }}
查看完整描述

3 回答

?
qq_遁去的一_1

TA貢獻1725條經驗 獲得超8個贊

在你的兩個例子中

.collect(groupingBy(Dish::getType));
.collect(groupingBy(Dish::getType, mapping(Dish::getName, toList())));

返回值相同,因為您toString()Dish類中的方法name僅返回。嘗試向toString()mehtod 添加更多信息,您會看到不同之處。

通常,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>>

通常groupingBy用作雙重分組的下游本身(按類型和是否是素食主義者):

.collect(groupingBy(Dish::getType, groupingBy(Dish::isVegetarian)); // Map<Type, Map<Boolean, List<Dish>>>


查看完整回答
反對 回復 2022-06-04
?
阿晨1998

TA貢獻2037條經驗 獲得超6個贊

如果這是問題

因為這兩個代碼都產生了相同的結果。一種使用 return groupingBy(classifier, toList()); 并返回 groupingBy(classifier, HashMap::new, 下游); ?

groupingBy(函數分類器,下游收集器)

不保證返回的 Map 的類型、可變性、可序列化性或線程安全性。

groupingBy(功能分類器,供應商mapFactory,下游收集器)

Collector 生成的 Map 是使用提供的工廠函數創建的。

唯一的區別是當您使用groupingBy時創建mapFactoryMap是基于您的供應商邏輯(可能是自定義的、不可變的、同步的等。)


查看完整回答
反對 回復 2022-06-04
?
炎炎設計

TA貢獻1808條經驗 獲得超4個贊

兩者的基本區別是什么?


主要區別在于您在完成收集器之前的中間步驟中完成的映射。不過,您使用它們的方式不同的是.groupingBy


一方面,您已將mapperanddownstream共同指定為:


.collect(Collectors.groupingBy(Dish::getType,  // classifier

             Collectors.mapping(Dish::getName,  // mapper <<<< difference here

                 Collectors.toList()))) // downstream

另一方面,的默認實現groupingBy用于


.collect(Collectors.groupingBy(Dish::getType)) 

可以擴展為類似于以下的某種格式:


.collect(Collectors.groupingBy(Dish::getType, // classifier

             Collectors.mapping(Function.identity(),  // mapper

                 Collectors.toList()))); // downstream


查看完整回答
反對 回復 2022-06-04
  • 3 回答
  • 0 關注
  • 206 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號