我将如何将下面的代码重构为 1 个通用方法?
(背景信息,用于从与 GSON 库一起使用的 Json 字符串中获取值)
ArrayList<Map<Object, Object>> theme = new ArrayList<Map<Object, Object>>();
for (int i = 0; i < obj.getThemeList().size(); i = i + 1) {
if(Boolean.parseBoolean(obj.getThemeList().get(i).getChecked())){
Map<Object,Object> map = new HashMap<Object,Object>();
map.put("id", obj.getThemeList().get(i).getId());
map.put("name", obj.getThemeList().get(i).getName());
theme.add(map);
}
}
ArrayList<Map<Object, Object>> tag = new ArrayList<Map<Object, Object>>();
for (int i = 0; i < obj.getTagList().size(); i = i + 1) {
if(Boolean.parseBoolean(obj.getTagList().get(i).getChecked())){
Map<Object,Object> map = new HashMap<Object,Object>();
map.put("id", obj.getTagList().get(i).getId());
map.put("name", obj.getTagList().get(i).getName());
tag.add(map);
}
}