我有两个逗号分隔的字符串。我希望通过索引绑定值索引将这些值放在映射中。一种方法是使用以下示例:
package com.test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class StringToMap {
public static void main(String[] args) {
String s1 = "1, 2, 3";
String s2 = "a, b, c";
Map<String, String> m = new HashMap<String, String>();
List<String> s1List = new ArrayList<String>(Arrays.asList(s1.split(",")));
List<String> s2List = new ArrayList<String>(Arrays.asList(s2.split(",")));
for (int i = 0; i < s1List.size(); i++) {
m.put(s1List.get(i).trim(), s2List.get(i).trim());
}
System.out.println(m);
}
}
结果是:{3 = c,2 = b,1 = a}。还有其他有效/最好的方法吗?
答案 0 :(得分:1)
{{1}}