![](/img/trans.png)
[英]Filter Objects having the same property from two Lists and Update them and Store it in a 3rd List in Java 8
[英]Filter Objects from two lists and do subtract with the attributes and store it in 3rd list in Java 8
List<Manipulate> a = new ArrayList<>();
a.add(new Manipulate(1,100));
a.add(new Manipulate(2,200));
List<Manipulate> b = new ArrayList<>();
b.add(new Manipulate(1,10));
b.add(new Manipulate(2,20));
我必須通過比較 id 並從 a(100,200) 中減去 b(10,20) 來過濾這兩個列表,並將 Maipulate Object 的列表存儲到僅使用 java 8 的一些新列表中
List<Manipulate> c = a.stream().map(k -> {
b.stream().filter(j -> j.getId() == k.getId())
.forEach(i -> {
int i1 = k.getQuantity() - i.getQuantity();
k.setQuantity(i1);
});
return k;
});
拋錨錯誤
Required type:
List
<Manipulate>
Provided:
Stream
<Object>
no instance(s) of type variable(s) R exist so that Stream<R> conforms to List<Manipulate>
您只是忘記將 stream 收集回列表中。
List<Manipulate> c = a.stream().map(k -> {
b.stream().filter(j -> j.getId() == k.getId())
.forEach(i -> {
int i1 = k.getQuantity() - i.getQuantity();
k.setQuantity(i1);
});
return k;
}).collect(Collectors.toList());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.