【发布时间】:2019-12-12 19:20:55
【问题描述】:
我有以下代码
TreeMap<Integer, List<String>> myMap = new TreeMap<>();
List<Integer> myList = getDataForTest(true);
List<String> wordList = getWordList(true);
List<Integer> intList = Collections.emptyList();
intList = myObj.stream().filter(e->e.getType.equalsIgnoreCase("TEST")).map(e->e.getIntPos()).collect(Collectors.toList())
AtomicInteger pos = new AtomicInteger(0);
myList.stream()
.forEach((entry) ->{
if(CollectionUtils.isEmpty(intList) || intList.contains(pos.get()))
{
myMap.computeIfAbsent(myList.get(pos.get()),k-> new ArrayList<>()).add(wordList.get(pos.get()));
}
pos.incrementAndGet();
});
return myMap;
上面的代码显然会抛出一个编译错误,因为我使用的 intList 在 lambda 中不是最终的。有没有办法让这个 lambda 被流替换。
【问题讨论】:
-
展示你的代码当然很重要。但同样或更重要的是显示带有一些值的起始数据结构是什么样的,以及填充了这些相同值的最终所需数据结构。请提供一个minimal reproducible example 这样做。
标签: java java-stream