gpt4 book ai didi

java - 从 arraylist 获取组作为列表

转载 作者:太空宇宙 更新时间:2023-11-04 06:19:04 25 4
gpt4 key购买 nike

我正在按照 emaze-dysfunction 从数组列表进行分组。在下面的代码中,我可以从 map 中进行分组。控制台显示分组元素。我需要将它们添加到数组列表中。

例如。 repay liat 有 120 个元素,但分组映射包含三组,每组 40 个元素。

        List<LoanRepaymentSchedule> repay = loanService
.getLoanRepaymentScheduleById(groupLoan.getLoanId());

Map<Integer, List<LoanRepaymentSchedule>> map = Groups.groupBy(
repay, new Pluck<Integer, LoanRepaymentSchedule>(
LoanRepaymentSchedule.class, "memberCount"));
System.out.println ("map.keySet().size() "+map.keySet().iterator());

for (Integer key : map.keySet()) {
List<LoanRepaymentSchedule> pro = map.get(key);
System.out.println("Element-******* "+pro.size());
System.out.println("Element-******* "+pro.get(0));

}

如何将这 3 个分组元素保存在单独的数组列表中,例如 list1、list2、list3。

还款 list 如下,

    id 1 val 5
id 1 val 6
id 1 val 1
id 1 val 5
id 1 val 6
id 1 val 1

我需要列表 1 作为

id 1 val 5
id 1 val 5

将 2 列为

id 1 val 6
id 1 val 6

将 1 列为

id 3 val 1
id 1 val 1

最佳答案

如果我理解正确的话,您想将 3 个列表添加到一个列表中。

您可以简单地执行以下操作:

    List<LoanRepaymentSchedule> output = new ArrayList<LoanRepaymentSchedule>();
for (Integer key : map.keySet()) {
output.addAll(map.get(key));
}

顺便说一句,我不熟悉这个 Groups 类,但 Java 8 对于相同的功能有更简单的语法:

Map<Integer, List<LoanRepaymentSchedule>> = 
repay.stream()
.collect(Collectors.groupingBy(LoanRepaymentSchedule::getMemberCount);

假设 LoanRe paymentSchedule 有一个 getMemberCount 方法,您可以通过该方法对 LoanRe paymentSchedule 实例进行分组。

关于java - 从 arraylist 获取组作为列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27686905/

25 4 0