让
List<String> data = Arrays.asList("key1=value1", "key2=value2");
Java中有没有办法使用流api将其转换为HashMap?
即。 {{key1 - &gt; value1},{key2 - &gt;值2}}
HashMap<String, String> dataMap = data.stream().map(s -> s.split("=")).//some stuff here//.collect(//some stuff here//);
答案 0 :(得分:2)
HashMap<String, String> map = data.stream()
.map(s -> s.split("="))
.collect(Collectors.toMap(s -> s[0], s -> s[1], (a, b) -> a, HashMap::new));