在内部地图键和值对上进行过滤,以便对属性值进行求和。

huangapple go评论56阅读模式
英文:

Filter on inner map key and value pair in order to sum on an attribute value

问题

以下是翻译好的内容:

我正在学习Java 8的流式处理(streams),并且想要理解如何实现以下操作:

我有一个嵌套的Map,我想要在这个Map的键(keys)所对应的内部Map的属性上应用过滤器(filter)。

class RegionDate {
    LocalDate regionDate;
    String region;
}

enum StudentType {
    ACTIVE,
    INACTIVE;
}

class Score {
    double grade;
    boolean isValid;
}

// 我拥有的集合
Map<RegionDate, Map<StudentType, List<Score>>> groupedTotals;
  • i) 我想要根据RegionDate键中的LocalDate匹配日期进行过滤。
  • 然后,在内部的Map的键中过滤出StudentType为ACTIVE的。
  • 接着,在分数(Score)的列表中过滤出isValid属性为true的分数。
  • 对grade属性进行求和,返回学生在特定日期和类型下所有有效分数的总和。

我尝试了一些方法,但我不清楚如何进入内部Map并访问键和值以进行过滤。我在第一个过滤器后得到了一个Map的Stream,然后我应该如何在第二个Map的键和值上进行过滤呢?

groupedTotals
    .entrySet().stream()
    .filter(e -> e.getKey().regionDate.equals(date))  // 根据日期过滤
    .map(Map.Entry::getValue)  // 获取内部Map
    .flatMap(m -> m.entrySet().stream())  // 将内部Map转换为流
    .filter(innerEntry -> innerEntry.getKey() == StudentType.ACTIVE)  // 过滤学生类型为ACTIVE的
    .flatMap(innerEntry -> innerEntry.getValue().stream())  // 将分数列表转换为流
    .filter(Score::isValid)  // 过滤有效的分数
    .mapToDouble(score -> score.grade)  // 提取分数的成绩
    .sum();  // 返回所有成绩的总和

希望这可以帮助你理解如何在嵌套的Map中应用过滤器和操作。

英文:

I am learning java 8 streams and want to understand how to accomplish the below

I have a map of map and I want to apply filter on attributes on the attributes within the keys of the map of maps

class RegionDate {
LocalDate regionDate;
String region;
}

enum StudentType {
ACTIVE,
INACTIVE;
}

class Score {
double grade;
 boolean isValid;
 }

Collection that I have

Map&lt;RegionDate, Map&lt;StudentType, List&lt;Score&gt;&gt;&gt; groupedTotals
  • i) I want to filter for a date that matches the localdate in
    RegionDate key
  • then filter on the StudentType to be active on the
    inner map key
  • then filter on the isValid attributes on the list
    of scores
  • do a sum of the grade attribute to return the sum of
    grades of students for a date and type and having valid scores.

I tried a few options but I am not clear how to get to the inner map and access the key and values and filter on them. I end up building a Steam of Map after the first filter and then how do I filter on the key of the second map and the values as well?

 groupedTotals
.entrySet().stream()
.filter(e -&gt; e.getKey().date().equals(date))
//filter on inner map key for student Type Active
//filter on inner map values for isValid is true
.filter(s -&gt; s.isValid())
.map(v -&gt; v.grade())
.mapToDouble(Double::doubleValue)
.sum();    //return a sum of the double values of all grades 

答案1

得分: 0

groupedTotals
    .entrySet().stream()
    .filter(e -> e.getKey().date().equals(date))
    .map(Entry::getValue)
    .map(m -> m.get(StudentType.ACTIVE))
    .filter(Objects::nonNull)
    .flatMap(List::stream)
    .filter(Score::isValid)
    .mapToDouble(Score::grade)
    .sum()
英文:
groupedTotals
 .entrySet().stream()
 .filter(e -&gt; e.getKey().date().equals(date)) // same so far
 .map(Entry::getValue) // get out the Map&lt;StudentType, List&lt;Score&gt;&gt;
 .map(m -&gt; m.get(StudentType.ACTIVE)) // get the List&lt;Score&gt;
 .filter(Objects::nonNull) // remove nulls from maps without ACTIVE
 .flatMap(List::stream) // get just a Stream&lt;Score&gt;
 .filter(Score::isValid) // filter the valid scores
 .mapToDouble(Score::grade) // map directly to double
 .sum() // sum results

huangapple
  • 本文由 发表于 2020年10月1日 02:58:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64144090.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定