如何在Java8中迭代列表并对其中的值进行分组

如何在Java8中迭代列表并对其中的值进行分组,java,arraylist,java-8,grouping,Java,Arraylist,Java 8,Grouping,我需要一些帮助在一个月内找到低费用和高费用。我有一个费用列表,其中费用类别具有以下值。您能指导我如何对它们进行分组以获得Java-8中的预期输出吗?提前谢谢你的帮助 Expenses type:Grocercies kind:fruits value:20 date:09/15/2020 Expenses type:Grocercies kind:vegetables value:20 date:09/01/2020 Expenses type:Grocercies kind:fruits,

我需要一些帮助在一个月内找到低费用和高费用。我有一个费用列表,其中费用类别具有以下值。您能指导我如何对它们进行分组以获得Java-8中的预期输出吗?提前谢谢你的帮助

Expenses
type:Grocercies
kind:fruits
value:20
date:09/15/2020

Expenses
type:Grocercies
kind:vegetables
value:20
date:09/01/2020

Expenses
type:Grocercies
kind:fruits,pulses
value:10
date:09/25/2020

Expenses
type:Grocercies
kind:fruits,milk
value:18
date:09/23/2020

Expenses
type:Grocercies
kind:vegetables,pulses
value:15
date:09/10/2020

Combined Rates
Fruits,Pulses : 30
Vegetables,pulses : 35
Fruits,milk : 38
预期产量

Low expense : Fruits,Pulses 
High Expense : Fruits,milk

我试着按类型(杂货)进行分组,即
Map
,但最后我又得到了一个列表,我对重复列表有点迷茫。非常感谢您的帮助。

我尝试了以下方法。我不确定这是不是一种有效的方法。有没有其他方法来实现它

公共静态void main(字符串[]args){ 列出费用=a(); Map expensesOfAllKinds=expenses.stream()
在此处输入代码
.collect(collector.groupingBy(expenses::getKind)); 列表
在此处输入code
finalOutPut=expensesofallgends.entrySet().stream() .map(expenseOfAKind->calculateValue(各种费用,各种费用)) .flatMap(bean->bean.stream()).collect(collector.toList()); System.out.println(“最终输出”); ExpenseOutput minexpenseOutput=finalOutPut.stream() .collect(Collectors.minBy(Comparator.comparing(ExpenseOutput::getValue))).get(); ExpenseOutput maxexpenseOutput=finalOutPut.stream() .collect(Collectors.maxBy(Comparator.comparing(ExpenseOutput::getValue))).get(); System.out.println(“minValue:+minexpenseOutput.getKind()+”:“+minexpenseOutput.getValue()); System.out.println(“maxValue:+maxexpenseOutput.getKind()+”:“+maxexpenseOutput.getValue()); }

private static List calculateValue(各种地图费用,
入境费用(种类){
List ExpenseOfAkindOfRocery=expenseOfAKind.getValue();
列出其他种类的支出=所有种类的支出。entrySet().stream()
.filter(expenseOfDifferentKind->!expenseOfDifferentKind.equals(ExpenseOfKind))
.map(expenseOfDifferentKind->expenseOfDifferentKind.getValue())
.flatMap(expenseOfDifferentKind->expenseOfDifferentKind.stream()).collect(Collectors.toList());
返回ExpenseOfGracery.stream()的种类
.map(一种费用->映射适当的种类和附加值(一种费用,其他种类的费用))
.flatMap(x->x.stream()).collect(collector.toList());
}
私有静态列表MapApprovedKindAndValue(种类的费用,
列出其他种类的费用){
返回其他种类的费用。流()
.filter(其他种类的费用->检查它们是否具有相同的类别(其他种类的费用,其他种类的费用))
.map(其他种类的费用->添加值Ifsamekindexists(其他种类的费用,其他种类的费用))
.collect(Collectors.toList());
}
私有静态布尔检查它们是否具有相同的绑定(其他种类的费用,其他种类的费用){
List expenseOfAKindList=Arrays.asList(expenseOfAKind.getKind().split(“,”);
List expenseOfOtherKindList=Arrays.asList(expenseOfOtherKind.getKind().split(“,”);
返回expenseOfOtherKindList.stream().allMatch(费用->ExpenseofKindList.contains(费用));
}
私有静态支出Output addValueIfSameKindExists(其他种类的支出,其他种类的支出){
ExpenseOutput=新ExpenseOutput();
output.setValue(expenseOfOtherKind.getValue()+expenseOfAKind.getValue());
List expenseOfAKindList=新的ArrayList(Arrays.asList(expenseOfAKind.getKind().split(“,”));
List expenseOfOtherKindList=新的ArrayList(Arrays.asList(expenseOfOtherKind.getKind().split(“,”));
其他种类清单的费用。添加所有(种类清单的费用);
Set finalKind=expenseOfOtherKindList.stream().collect(Collectors.toSet());
setKind(String.join(“,”,finalKind));
返回输出;
} 

您不能像在Java8之前那样使用一个很好的for循环吗?如果您想使用streamsPlease,请提供您尝试实现的代码段。这有帮助吗?
private static List<ExpenseOutput> calculateValue(Map<String, List<Expenses>> expensesOfAllKinds,
        Entry<String, List<Expenses>> expenseOfAKind) {
    List<Expenses> expenseOfAKindOfGrocery = expenseOfAKind.getValue();
    List<Expenses> expensesOfOtherKinds = expensesOfAllKinds.entrySet().stream()
            .filter(expenseOfDifferentKind -> !expenseOfDifferentKind.equals(expenseOfAKind))
            .map(expenseOfDifferentKind -> expenseOfDifferentKind.getValue())
            .flatMap(expenseOfDifferentKind -> expenseOfDifferentKind.stream()).collect(Collectors.toList());
    return expenseOfAKindOfGrocery.stream()
            .map(oneKindOfExpense -> mapAppropriateKindAndAddValue(oneKindOfExpense, expensesOfOtherKinds))
            .flatMap(x -> x.stream()).collect(Collectors.toList());
}

private static List<ExpenseOutput> mapAppropriateKindAndAddValue(Expenses expenseOfAKind,
        List<Expenses> expensesOfOtherKinds) {
    return expensesOfOtherKinds.stream()
            .filter(expenseOfOtherKind -> checkWhetherTheyHaveSameKind(expenseOfOtherKind, expenseOfAKind))
            .map(expenseOfOtherKind -> addValueIfSameKindExists(expenseOfOtherKind, expenseOfAKind))
            .collect(Collectors.toList());
}

private static boolean checkWhetherTheyHaveSameKind(Expenses expenseOfOtherKind, Expenses expenseOfAKind) {
    List<String> expenseOfAKindList = Arrays.asList(expenseOfAKind.getKind().split(","));
    List<String> expenseOfOtherKindList = Arrays.asList(expenseOfOtherKind.getKind().split(","));
    return expenseOfOtherKindList.stream().allMatch(expense -> expenseOfAKindList.contains(expense));
}

private static ExpenseOutput addValueIfSameKindExists(Expenses expenseOfOtherKind, Expenses expenseOfAKind) {
    ExpenseOutput output = new ExpenseOutput();
    output.setValue(expenseOfOtherKind.getValue() + expenseOfAKind.getValue());
    List<String> expenseOfAKindList = new ArrayList<>(Arrays.asList(expenseOfAKind.getKind().split(",")));
    List<String> expenseOfOtherKindList = new ArrayList<>(Arrays.asList(expenseOfOtherKind.getKind().split(",")));
    expenseOfOtherKindList.addAll(expenseOfAKindList);
    Set<String> finalKind = expenseOfOtherKindList.stream().collect(Collectors.toSet());
    output.setKind(String.join(",", finalKind));
    return output;
}