Is there a better functional way of converting an array of Strings in the form of "key:value" to a Map
using the Java 8 lambda syntax?
Arrays.asList("a:1.0", "b:2.0", "c:3.0")
.stream()
.map(elem -> elem.split(":")
.collect(Collectors.toMap(keyMapper?, valueMapper?));
The solution I have right now does not seem really functional:
Map<String, Double> kvs = new HashMap<>();
Arrays.asList("a:1.0", "b:2.0", "c:3.0")
.stream()
.map(elem -> elem.split(":"))
.forEach(elem -> kvs.put(elem[0], Double.parseDouble(elem[1])));
elem -> elem.split(":", 2)
instead of adding thefilter
step. This way erroneous input will be manifested either byArrayIndexOutOfBoundsException
(if no colon was found) or byNumberFormatException
(if more than one colon was found). – Themis