我已经列出了一份清单,我想把每一个&25;'列表中的项目并将其发送到返回地图的方法。
下面的batches
方法会给我一个包含25个项目的列表流,并且工作正常
当我尝试将其分配给地图时,代码会抛出不兼容的类型
这就是我写的
public class Processor{
@Inject
private Delivery delivery;
public String process(List<String> item) throws Exception{
Map<String,List<String>> tempMap = batches(item,25).forEach(i -> delivery.process(i));
//the method delivery.process would take a list and returns a map(string,list)
}
public static Stream<List<String>> batches(List<String> source, int length) {
if (length <= 0)
throw new IllegalArgumentException("length = " + length);
int size = source.size();
if (size <= 0)
return Stream.empty();
int fullChunks = (size - 1) / length;
return IntStream.range(0, fullChunks + 1).mapToObj(
n -> source.subList(n * length, n == fullChunks ? size : (n + 1) * length));
}
}
答案 0 :(得分:1)
app/config/routing.yml
未返回forEach
它返回Map
,即使它返回void
,每次都会覆盖Map
,所以你必须做一些其他事情,而不是tempMap
,或者这样做:
forEach
答案 1 :(得分:0)
正如其他人已经提到的那样,代码无法正常工作的原因是forEach
是终端操作,返回 void 。
至于预防问题,我会这样做:
Map<String, List<String>> result =
batches(item, 25)
.map(delivery::process)
.collect(HashMap::new, Map::putAll, Map::putAll);