Java 在分隔符上拆分列表的元素。将原始元素存储为键,将拆分子字符串列表存储为hashmap中的值
我有一份清单:Java 在分隔符上拆分列表的元素。将原始元素存储为键,将拆分子字符串列表存储为hashmap中的值,java,arrays,collections,hashmap,Java,Arrays,Collections,Hashmap,我有一份清单: “客户活动”、“MRkt客户”、“独立客户账户”、“Opty法案” 我希望分割“389;”上的每个元素,并将原始元素存储为hashmap的键,将分割的元素存储为值 预期结果: {“Cust_Gnrc_活动”:[“Cust”、“Gnrc”、“活动”],“Mrkt_Cust”:[“Mrkt”、“Cust”]}等。大致如下: source.stream() .collect(toMap(Function.identity(), e -> e.split("_")));
“客户活动”、“MRkt客户”、“独立客户账户”、“Opty法案”
我希望分割“389;”上的每个元素,并将原始元素存储为hashmap的键,将分割的元素存储为值
预期结果:
{“Cust_Gnrc_活动”:[“Cust”、“Gnrc”、“活动”],“Mrkt_Cust”:[“Mrkt”、“Cust”]}
等。大致如下:
source.stream()
.collect(toMap(Function.identity(), e -> e.split("_"))); // Map<String, String[]>
source.stream()
.collect(toMap(Function.identity(),e->e.split(“”));//地图
或者,如果希望将值作为列表
:
source.stream()
.collect(toMap(Function.identity()),
e->Arrays.stream(e.split(“”))
.collect(toList());//地图
大致如下:
source.stream()
.collect(toMap(Function.identity(), e -> e.split("_"))); // Map<String, String[]>
source.stream()
.collect(toMap(Function.identity(),e->e.split(“”));//地图
或者,如果希望将值作为列表
:
source.stream()
.collect(toMap(Function.identity()),
e->Arrays.stream(e.split(“”))
.collect(toList());//地图
您可以使用流API执行此操作:
list.stream() // stream over List
// split by "_" and convert it to List<String>
.collect(Collectors.toMap(s -> s, s -> Arrays.asList(s.split("_"))));
您可以使用流API执行此操作:
list.stream() // stream over List
// split by "_" and convert it to List<String>
.collect(Collectors.toMap(s -> s, s -> Arrays.asList(s.split("_"))));
试着用这个:
Map<String, List<String>> result = new HashMap<>();
list.forEach(str -> result.put(str, Arrays.asList(str.split("_"))));
试着用这个:
Map<String, List<String>> result = new HashMap<>();
list.forEach(str -> result.put(str, Arrays.asList(str.split("_"))));