Java 8 stream map on entry set

前端 未结 5 1729
失恋的感觉
失恋的感觉 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 19:51

    Simply translating the "old for loop way" into streams:

    private Map mapConfig(Map input, String prefix) {
        int subLength = prefix.length();
        return input.entrySet().stream()
                .collect(Collectors.toMap(
                       entry -> entry.getKey().substring(subLength), 
                       entry -> AttributeType.GetByName(entry.getValue())));
    }
    

提交回复
热议问题