I have this code which is realized with a for loop. I wanted to write it with a .stream and .map() function. I tried using the .map() function. But unfortunately I get the following error:
Incompatible types. Required List> but 'collect' was inferred to R: no instance(s) of type variable(s) exist so that Boolean conforms to List inference variable T has incompatible bounds: equality constraints: List lower bounds: Boolean
Here is the old code:
public Iterable<Record> findAll(final List<Long> id) {
final List<Record> result = new LinkedList<Record>();
final List<List<Long>> partitions = ListUtils.partition(id, 10);
for (List<Long> partition : partitions) {
Iterables.addAll(
result,
this.repository.findAll(partition)
);
}
return result;
}
This is the code when I use the .map()
public Iterable<Record> findAll(final List<Long> id) {
final List<Record> result = new LinkedList<Record>();
final List<List<Long>> partitions = ListUtils.partition(id, 10);
List<List<Long>> allPartitions = partitions.stream().map(partition ->{
return Iterables.addAll(result, this.repository.findAll(partition));
}).collect(Collectors.toList());
return result;
}
Any suggestion on how I could fix this? or on what I should pay attention?
List<List<Long>> allPartitions = partitions.stream().map(partition ->{ Iterables.addAll(result, this.repository.findAll(partition)); return result; }).collect(Collectors.toList());- Hadi JIterables.addAll(result, this.repository.findAll(partition));returns boolean while you need to returnsList<Long>because of that it isn't compatible. so I guess you should returnresultalthough I don't familiar with guava. - Hadi Jreturn partitions.stream() .flatMap(partition->this.repository.findAll(partition).stream()) .collect(Collectors.toList());- Hadi Jthis.repository.findAll(partition)return? - Holger