Java daily coding tips

LinkedHashMap

The daily work is usually HashMap. Sometimes it is connected with the front end and returned, and the map results are displayed in order. At this time, order needs to be considered ; and java.utilit is provided in it LinkedHashMap, you can refer to learning: linked_hash_map

small application of reflection

Attribute class, json stored in the database, may increase or decrease; when docking with the front end, it is hoped that a json will be passed to set, and the back end is best not to bother to write an intermediate DO conversion setting

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;
    }
}

test

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);
    }
}

ALL

Guess you like

Origin blog.csdn.net/qq_26437925/article/details/120624063
Recommended