List<CategoryWiseEarnings> data = tripEarnings.getOrders()
.stream()
.flatMap(getCategoryRulesEarnedList -> getCategoryRulesEarnedList.getCategoryRulesEarnedList().stream())
.collect(Collectors.groupingBy(foo -> foo.getCategoryId()))
.entrySet()
.stream()
.map(e -> e.getValue()
.stream()
.reduce((c,c2) -> new CategoryWiseEarnings(
new CategoryWise(
c.getCategoryName(),
c.getCategoryId()
),
c.getBonus()
))
)
.map(f -> f.get())
.collect(Collectors.toList());
将异常获取为
:lambda表达式中的返回类型错误:categorywiseernings不能转换为
CategoryWise
public class CategoryWiseEarnings {
@JsonProperty("category")
private CategoryWise earnings;
@JsonProperty("total_amount")
private String totalAmount;
}
public class CategoryWise {
@JsonProperty("category_id")
Long categoryId;
@JsonProperty("category_name")
String categoryName;
public CategoryWise(String categoryName, Long categoryId) {
this.categoryName = categoryName;
this.categoryId = categoryId;
}
}
这是我想使用流和lambda函数编写的代码,如果我这样写,它可以正常工作:
for (Trips tripsOrders : tripEarnings.getOrders()) {
if (!tripsOrders.getCategoryRulesEarnedList().isEmpty()) {
for (CategoryWise c : tripsOrders.getCategoryRulesEarnedList()) {
if (hashMapCategory.containsKey(c.getCategoryId())) {
// hashmapk.put(c.getCategoryId(),new CategoryWiseEarnings(new CategoryWise(c.getCategoryName(),c.getCategoryId()),c.getBonus()+hashmapk.get(c.getCategoryId()).getTotalAmount()));
CategoryWiseEarnings categoryObject = hashMapCategory.get(c.getCategoryId());
categoryObject.setTotalAmount(Double.toString(
Double.parseDouble(c.getBonus())
+ Double.parseDouble(categoryObject.getTotalAmount())
));
hashMapCategory.put(c.getCategoryId(), categoryObject);
} else {
hashMapCategory.put(c.getCategoryId(), new CategoryWiseEarnings(new CategoryWise(c.getCategoryName(), c.getCategoryId()), c.getBonus()));
}
}
}
}
List<CategoryWiseEarnings> list = new ArrayList<CategoryWiseEarnings>(hashMapCategory.values());
匿名用户
流::reduce(二进制操作符)需要一个双函数
此外,数据模型似乎是不恰当的:在类别智慧收益
字段总
应该是双
以避免冗余转换,类类别智慧
缺少奖金
字段。
因此,解决方案可能是使用收集器。按收集器分组。将double求和以计算映射中的总值,然后将映射条目重新映射到类别收入:
List<CategoryWiseEarnings> result = tripEarnings.getOrders()
.stream() // Stream<Trips>
.flatMap(trips -> trips.getCategoryRulesEarnedList().stream()) // Stream<CategoryWise>
.collect(Collectors.groupingBy(
cw -> Arrays.asList(cw.getCategoryId(), cw.getCategoryName()) // key -> List<Object>
LinkedHashMap::new, // keep insertion order
Collectors.summingDouble(cw -> Double.parseDouble(cw.getBonus()))
)) // Map<List<Id, Name>, Double>
.entrySet()
.stream()
.map(e -> new CategoryWiseEarnings(
new CategoryWise(e.getKey().get(0), e.getKey().get(1)),
String.valueOf(e.getValue()) // assuming the total is String
))
.collect(Collectors.toList());
以下代码在IntelliJ和Eclipse中编译得很好,但JDK编译器1.8.0\u 25对此表示不满。首先,代码。 javac 1.8.0\u 25的输出为: 当我更换时?超级E只需使用E,JDK就能成功编译。 当我将替换为,JDK编译成功。 由于它适用于JDK 1.8.0_60,我怀疑它是编译器错误。 有没有详细说明是什么原因造成的以及何时修复的?
我正在学习Java中的Lambda并试图理解它。例如,我有以下代码: 我不理解这个语句我看到是类型的引用变量。但是我不明白这个到底是什么。这是方法定义吗?这个方法的返回类型是什么?我认为应该是,因为方法的返回类型是。如有任何反馈将不胜感激。
我有一个静态的通用FormBuilder超文本标记语言助手方法(HTMLHelper类上的扩展方法),它接受视图模型类型的通用参数,然后当从数据库传递一个或多个字符串属性名称时,生成一个超文本标记语言形式在ASP. NET MVC 5.1 with. NET 4.5中。 我有一个公共方法来生成表单,还有单独的私有方法来生成表单中的“模块”部分,然后渲染其中的每个字段。类型参数从上到下沿此链传递。
为什么会发生异常'lambda表达式中的ad返回类型:AuthenticatedUser无法转换为User'?如果可选为空,我只想返回此AuthenticatedUser。
我试图通过加速,用java流编写一个查询。当我尝试在select中时,我得到以下错误: lambda表达式中的错误返回类型:BigDecimal不能转换为long。运算符-不能应用于int、BigDecimaljava.math.。 我的代码是这样的: 我该如何解决这个问题?
我正在编写一个lambda表达式来将给定的纬度和经度转换为地址。表达式应该以坐标为参数,并返回其相应的地址。但是,返回的值为null。以下是我的班级: 以下是logcat的输出: 以下是我的用法: 是类的对象,以下是相关接口: 当我尝试从相关适配器打印坐标时,它们会正确打印。因此,位置正在正确设置,但是当我试图从另一个类访问它时,它会显示字符串的空值。你能建议一个替代方法来从表达式中提取位置吗?