多级属性名称转map


public static void main(String[] args) {
        List fields = new ArrayList<>();
        fields.add("age");
        fields.add("name");
        fields.add("cardList.cardNumber");
        fields.add("cardList.createDate");
        fields.add("cardList.cardType.name");
        fields.add("cardList.cardType.pwdType");
        fields.add("cardList.cardType.enabled");
        Map stringObjectMap = BaseCurrentResource.fields2map(fields);
    }

    public static Map fields2map(List fields) {
        Map map = new HashMap<>();
        for (String field : fields) {
            if (field.contains(".")) {
                String[] splits = field.split("\\.");
                field2tree(map, splits, 0, splits.length);
            } else {
                map.put(field, null);
            }
        }
        return map;
    }

    public static Map field2tree(Map map, String[] fields, int i, int n) {
        if (i >= n) {
            return map;
        }
        if (map == null) {
            map = new HashMap<>();
        }
        if (i == n - 1) {
            map.put(fields[i], null);
        } else {
            map.put(fields[i], field2tree((Map) map.get(fields[i]), fields, i + 1, n));
        }
        return map;
    }