Java 8 stream map on entry set

前端 未结 5 1736
失恋的感觉
失恋的感觉 2020-12-29 19:22

I\'m trying to perform a map operation on each entry in a Map object.

I need to take a prefix off the key and convert the value from one type to another

5条回答
  •  滥情空心
    2020-12-29 20:03

    Question might be a little dated, but you could simply use AbstractMap.SimpleEntry<> as follows:

    private Map mapConfig(
        Map input, String prefix) {
           int subLength = prefix.length();
           return input.entrySet()
              .stream()
              .map(e -> new AbstractMap.SimpleEntry<>(
                   e.getKey().substring(subLength),
                   AttributeType.GetByName(e.getValue()))
              .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    

    any other Pair-like value object would work too (ie. ApacheCommons Pair tuple).

提交回复
热议问题