为下面的代码获取每个 loop/stream 的等价物
Getting equivalent for each loop/stream for below code
我想将这段代码转换为等效的流或 for-each 循环
for (Studentauditlog st: studentauditloglist) {
String columnValue = callEntityGetterMethod(st, column);
if (StringUtils.isNotBlank(columnValue)) {
if (response.containsKey(columnValue)) {
Long count = (response.get(columnValue) == null)? 0L : response.get(columnValue);
response.put(columnValue, count + 1);
} else {
response.put(columnValue, 1L);
}
}
}
现有代码计算一个 response
映射,其中非空白字符串列是键,映射值是键的频率。
这可以实现为 Stream::map
、Stream::filter
的序列以获取列值,这些列值使用 Collectors.groupingBy
和 Collectors.counting
作为下游收集器进行分组。
参见下面的实现:
Map<String, Long> response = studentauditloglist
.stream() // Stream<Studentauditlog >
.map(st -> callEntityGetterMethod(st, column))
.filter(StringUtils::isNotBlank) // Stream<String>
.collect(Collectors.groupingBy(
cv -> cv, // or Function.identity()
Collectors.counting()
));
我想将这段代码转换为等效的流或 for-each 循环
for (Studentauditlog st: studentauditloglist) {
String columnValue = callEntityGetterMethod(st, column);
if (StringUtils.isNotBlank(columnValue)) {
if (response.containsKey(columnValue)) {
Long count = (response.get(columnValue) == null)? 0L : response.get(columnValue);
response.put(columnValue, count + 1);
} else {
response.put(columnValue, 1L);
}
}
}
现有代码计算一个 response
映射,其中非空白字符串列是键,映射值是键的频率。
这可以实现为 Stream::map
、Stream::filter
的序列以获取列值,这些列值使用 Collectors.groupingBy
和 Collectors.counting
作为下游收集器进行分组。
参见下面的实现:
Map<String, Long> response = studentauditloglist
.stream() // Stream<Studentauditlog >
.map(st -> callEntityGetterMethod(st, column))
.filter(StringUtils::isNotBlank) // Stream<String>
.collect(Collectors.groupingBy(
cv -> cv, // or Function.identity()
Collectors.counting()
));