2017-08-25 78 views
4
Map<String, Map<String, String>> myValues; 

myValues.entrySet().stream.collect(
    Collectors.toMap(entry -> getActualKey(entry.getKey()), 
        entry -> doCalculation(entry.getValue())) 
        ); 

有没有一种方法可以让我在doCalculation功能中获得钥匙?我知道我可以再次将getActualKey(entry.getKey())作为参数进行计算,但我不想重复相同的功能两次。获取钥匙在收藏家.to地图

回答

4

您可以将您的条目映射到你的派生密钥新的中间项,然后传递值对doCalculation()

myValues.entrySet() 
    .stream() 
    .map(e -> new SimpleEntry<>(getActualKey(e.getKey()), e.getValue())) 
    .collect(Collectors.toMap(e -> e.getKey(), e -> doCalculation(e.getKey(), e.getValue())));