LinkedHashMap
日常工作通常都是HashMap,有时候与前端对接返回,按顺序显示map结果,这时需要考虑有序;而java.util
中提供了LinkedHashMap
, 可参考学习:linked_hash_map
反射的小应用
属性类,json存到数据库中,可能有增减; 与前端对接时,希望一个json传递过来设置,而后端也最好不用麻烦的写个中间DO转换设置
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* @Author mubi
* @Date 2021/10/3 10:26
*/
public class ProjectProperty {
private String name;
private Integer projectId;
private List<String> projectItems;
private List<Integer> ids;
public static ProjectProperty constructFromJson(String jsonStr){
Map<String, Object> map = JSON.parseObject(jsonStr, Map.class);
ProjectProperty projectProperty = new ProjectProperty();
Class clz = projectProperty.getClass();
Field[] fields = clz.getDeclaredFields();
Map<String, Field> fieldNameMap = new HashMap(fields.length);
for (Field field : fields) {
fieldNameMap.put(field.getName(), field);
}
for (Map.Entry<String, Object> entry : map.entrySet()) {
String property = entry.getKey();
Object value = entry.getValue();
if (fieldNameMap.containsKey(property)) {
try {
Field field = fieldNameMap.get(property);
field.setAccessible(true);
// String
if (field.getType() == String.class) {
field.set(projectProperty, String.valueOf(value));
}
// Integer
else if (field.getType() == Integer.class) {
field.set(projectProperty, Integer.parseInt(String.valueOf(value)));
}
// List
else if (field.getType() == List.class) {
JSONArray array = (JSONArray) value;
List<Object> list = new ArrayList<>();
array.forEach(o -> list.add(o));
field.set(projectProperty, list);
}
else {
field.set(projectProperty, value);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
LinkedHashMap
return projectProperty;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getProjectId() {
return projectId;
}
public void setProjectId(Integer projectId) {
this.projectId = projectId;
}
public List<String> getProjectItems() {
return projectItems;
}
public void setProjectItems(List<String> projectItems) {
this.projectItems = projectItems;
}
public List<Integer> getIds() {
return ids;
}
public void setIds(List<Integer> ids) {
this.ids = ids;
}
}
测试
import com.alibaba.fastjson.JSON;
import com.example.demoapi.domain.ProjectProperty;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Author mubi
* @Date 2021/6/5 10:59
*/
public class ProjectPropertyTest {
@Test
public void test() throws Exception {
Map<String, Object> map = new HashMap();
map.put("name", "abc");
map.put("projectId", 123);
List<String> projectItems = new ArrayList(){{
add("pro1");
add("pro2");
}};
map.put("projectItems", projectItems);
List<Integer> ids = new ArrayList(){{
add(1);
add(2);
}};
map.put("ids", ids);
String jsonStr = JSON.toJSONString(map);
ProjectProperty projectProperty = ProjectProperty.constructFromJson(jsonStr);
Assert.assertNotNull(projectProperty);
}
}