Java将带有分隔符的List <string>转换为Hashmap <string,string =“”>

时间:2017-01-31 18:42:48

标签: string list hashmap java-8 java-stream

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//);

1 个答案:

答案 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));