将项目收集到方括号中(当前和所有先前的组)

时间:2018-10-16 09:22:59

标签: java java-stream collectors

(对不起,“括号”一词令人困惑,我从荷兰语翻译过来的“税括号”中取了它。)

如何基于属性阈值将项目分为组和每个先前的组

示例:

<div class="outer">
<div class="container-inner">
  <div class="inner">This is is row number 1</div>
  <div class="inner">This is is row number 2</div>
  <div class="inner">This is is rowfddfssdfdfs number 3</div>
  </div>
</div>

结果为Loan A, duration of 6 months Loan B, duration of 10 months Loan C, duration of 12 months

Map<Integer, List<Loan>>

当前,我要走LinkedHashMap路线,然后从流中收集项目之后,遍历所有组以更新它们:

6 -> {A, B, C}
10 -> {B, C}
12 -> {C}

所以我正在做的是地图中的每个后续列表值都将包含所有先前的贷款以及仅与相应的工期键相关的贷款。类似于累计小计。

这可以使用标准API还是自定义收集器之类的方法完成?

2 个答案:

答案 0 :(得分:1)

对于用例,是否有必要使用Map<Integer, List<Loan>>之类的类型?因此,每个列表将拥有所有以前的贷款。这意味着将有对Loan的冗余引用。

还是用例来提供按loans分组的duration上的视图?
在这种情况下,我们可以使用另一种方法:使用Stream(基于Java 9+)查看贷款。

public class Loans {

    private final List<Loan> loans;

    public Loans(List<Loan> loans) {
        this.loans = loans.stream()
                .sorted(Comparator.comparingInt(Loan::getDuration))
                .collect(Collectors.toList());
    }

    public Stream<Loan> getUpTo(int duration) {
        return loans.stream().takeWhile(l -> l.getDuration() <= duration);
    }

}

由于我们有一个按List<Loan>排序的duration,因此我们可以使用Stream.takeWhile来为特定的{{1 }}。

例如像这样:

Stream

在需要使用Loan的情况下,我们仍然可以duration流元素Loans loans = new Loans(List.of(new Loan("A", 6), new Loan("B", 10), new Loan("C", 12)); loans.getUpTo(1); // <empty> loans.getUpTo(5); // <empty> loans.getUpTo(6); // Loan("A", 6) loans.getUpTo(10); // Loan("A", 6), new Loan("B", 10) loans.getUpTo(11); // Loan("A", 6), new Loan("B", 10) loans.getUpTo(12); // Loan("A", 6), new Loan("B", 10), Loan("C", 12) loans.getUpTo(100); // Loan("A", 6), new Loan("B", 10), Loan("C", 12)

答案 1 :(得分:0)

我能想到的简单解决方案就是这样

class Bucket(int limit, List<Loan> elements)

List<Int> buckets = Arrays.asList(6, 10, 12); // The bucket that you want to put items into
buckets.stream.flatMap(bucketLimit -> {
    List<Loan> loans = loans.stream()
        .filter(l -> l.getDuration() <= bucket)
        .collect(Collectors.toList());

    return new Bucket(bucketLimit, loans);
})
.collect(Collectors.toMap(Bucket::limit, Bucket::loans))

这里可能存在一些语法错误,但是您应该可以执行类似的操作。