Issue
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));
i have to filter these two lists by comparing the ids and subtract b(10,20) from a(100,200) and store the List of Maipulate Object to some new lists using only 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;
});
Thorwing error
Required type:
List
<Manipulate>
Provided:
Stream
<Object>
no instance(s) of type variable(s) R exist so that Stream<R> conforms to List<Manipulate>
Solution
You just forgot to collect the stream back into a list.
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());
Answered By - Robert McKay
Answer Checked By - Terry (JavaFixing Volunteer)