public class AnswerApp {
private static final String UNDERLINE = "_";
public static void main(String[] args) {
Map<String, Object> params = Maps.newHashMap();
params.put("index_0", 1);
params.put("key_0", "answer");
params.put("value_0", "abc");
params.put("index_1", 2);
params.put("key_1", "jaemon");
params.put("value_1", "def");
params.put("index_2", 3);
params.put("key_2", "answerail");
params.put("value_2", "ghi");
System.out.println(JSON.toJSONString(params));
System.out.println();
Map<String, Map<String, Object>> collect = params.keySet().stream().collect(
groupingBy(e -> e.substring(e.lastIndexOf(UNDERLINE) + 1),
toMap(e -> e.substring(0, e.lastIndexOf(UNDERLINE)), params::get)));
System.out.println(JSON.toJSONString(collect));
System.out.println();
System.out.println(JSON.toJSONString(collect.values()));
}
}
程序运行结果
{"index_1":2,"index_2":3,"key_2":"answerail","key_1":"jaemon","key_0":"answer","index_0":1,"value_2":"ghi","value_0":"abc","value_1":"def"}
{"0":{"index":1,"value":"abc","key":"answer"},"1":{"index":2,"value":"def","key":"jaemon"},"2":{"index":3,"value":"ghi","key":"answerail"}}
[{"index":1,"value":"abc","key":"answer"},{"index":2,"value":"def","key":"jaemon"},{"index":3,"value":"ghi","key":"answerail"}]