Мне нужно снять префикс с ключа и преобразовать значение из одного типа в другой. Мой код берет записи конфигурации из Map и преобразует их в Map (
Код: Выделить всё
AttributeTypeЛучшее, что мне удалось получить с помощью потоков Java 8, это следующее:
Код: Выделить всё
private Map mapConfig(Map input, String prefix) {
int subLength = prefix.length();
return input.entrySet().stream().flatMap((Map.Entry e) -> {
HashMap r = new HashMap();
r.put(e.getKey().substring(subLength), AttributeType.GetByName(e.getValue()));
return r.entrySet().stream();
}).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
Есть ли лучшая альтернатива? Кажется, удобнее сделать это с помощью цикла for:
Код: Выделить всё
private Map mapConfig(Map input, String prefix) {
Map result = new HashMap();
int subLength = prefix.length();
for(Map.Entry entry : input.entrySet()) {
result.put(entry.getKey().substring(subLength), AttributeType.GetByName( entry.getValue()));
}
return result;
}
Подробнее здесь: https://stackoverflow.com/questions/272 ... -entry-set