ARTS-Week6 有序链表合并、DevOps、Json解析、服务熔断

Algorithm


LeetCode原题链接: 合并两个有序链表

将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例:
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4

我的解法:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
   public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        return setNode(null, l1, l2);
    }

    public static ListNode setNode(ListNode currentNode, ListNode l1, ListNode l2){
        if(l1 == null && l2 == null){
            return currentNode;
        }

        if(l1 == null){
            return l2;
        }else if(l2 == null){
            return l1;
        }else {
            if(l1.val < l2.val){
                currentNode = new ListNode(l1.val);
                l1 = l1.next;
            }else {
                currentNode = new ListNode(l2.val);
                l2 = l2.next;
            }
        }

        currentNode.next = setNode(null, l1, l2);
        return currentNode;
    }
}

Review


DevOps: The Journey So Far and What Lies Ahead!
这篇文章主要讲了两部分内容:DevOps目前的发展及带来的改变,作者对DevOps未来发展的预测。

a) Manual => Automated

b) Physical Datacenter => Virtual Private Cloud

c) Outages => High Availability/Zero downtime

d) Enterprise/Web archives => Containers

and the list goes on and on…

Tip


Json串路径解析
输入参数com.alibaba.fastjson.JSONArraycom.alibaba.fastjson.JSONObjectjson格式的字符串
输出:Map Map
利用字段的路径可根据json模板 读取不同格式的json串解析成单个字段将多条数据记录组装成指定格式的字符串

示例:


Json解析

Json解析工具类源码

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Class : JsonUtil
 * Description : json相关工具类
 * @author Jokay
 * Date : 2018/12/26 15:44
 **/
public class JsonUtil {
    private static final String LOG_TEMP = "[Type]: %-20s[Key]: %-30s[Path]:%s";

    /**
     * Description : 对json对象进行解析,获取每个key对应的JsonPath(com.jayway.jsonpath.JsonPath),注:与阿里fastjson中的jsonpath 有差异
     * @param objJson json对象,com.alibaba.fastjson.JSONArray或JSONObject或json字符串
     * @param path json中key的路径,调用时该值传入空字符串:""
     * @return java.util.Map
     * @author Jokay
     * Date : 2018/12/26 18:16
     */
    public static Map analyzeJson(Map map, Object objJson, String path ) {
        // obj为JsonArray
        if (objJson instanceof JSONArray) {
            JSONArray objArray = (JSONArray) objJson;
            if (!objArray.isEmpty()) {
                analyzeJson(map, objArray.get(0), path);
            }
        // obj为JsonObject
        } else if (objJson instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) objJson;
            Iterator it = jsonObject.keySet().iterator();
            while (it.hasNext()) {
                String key = it.next().toString();
                Object object = jsonObject.get(key);
                if (StringUtils.isEmpty(path)) {
                    path += "$." + key;
                } else {
                    path += "." + key;
                }

                if (object != null) {
                    // 如果得到的是数组
                    if (object instanceof JSONArray) {
                        path += "[*]";
                        if(((JSONArray) object).get(0) instanceof JSONObject){
//                            System.out.println(String.format(LOG_TEMP, "ObjectArray", key, path));
                        }else {
                            saveKeyAndPath(map, key, path);
//                            System.out.println(String.format(LOG_TEMP, "Simplerray", key, path));
                        }
                        analyzeJson(map, object, path);
                        path = path.substring(0, path.lastIndexOf("."));
                    } else if (object instanceof JSONObject) {
//                        System.out.println(String.format(LOG_TEMP, "Object", key, path));
                        analyzeJson(map, object, path);
                        path = path.substring(0, path.lastIndexOf("."));
                    } else {
//                        System.out.println(String.format(LOG_TEMP, "Simple->"+object.getClass().getSimpleName(), key, path));
                        // 路径与值
                        saveKeyAndPath(map, key, path);
                        path = path.substring(0, path.lastIndexOf("."));
                    }
                } else {
//                    System.out.println(String.format(LOG_TEMP, "NULL", key, path));
                    saveKeyAndPath(map, key, path);
                    path = path.substring(0, path.lastIndexOf("."));
                }
            }
        }else if (objJson instanceof String) {
            String jsonStr = (String)objJson;
             if(isJsonString(jsonStr)){
                 analyzeJson(map, JSONObject.parse(jsonStr, Feature.OrderedField), "");
             }
        }
        
        return map;
    }

    /**
     * Description : 判断一个字符串是否为JSON格式
     * @param jsonStr 要判断的字符串
     * @return boolean
     * @author Jokay
     * Date : 2018/12/26 16:04
     */
    public static boolean isJsonString(String jsonStr){
        try {
            JSONObject.parseObject(jsonStr,Feature.OrderedField);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * Description : 保存json中每个Key与其对应的JsonPath;若Key同名,则自动重命名为Key1,Key2,Key3...
     * @param key json中的key
     * @param path json中key对应的jsonPath
     * @author Jokay
     * Date : 2018/12/26 16:23
     */
    private  static void saveKeyAndPath(Map map, String key, String path){
        int i = 1;
        String newKey = key;
        while (map.containsKey(newKey)){
            newKey = key + i;
            i++;
        }
        map.put(newKey, path);
    }

    public static void main(String[] args) {
        String jsonStr = "{\"name\":\"BeJson\",\"url\":\"http://www.bejson.com\",\"page\":88,\"isNonProfit\":true,\"address\":{\"street\":\"科技园路.\",\"city\":\"江苏苏州\",\"country\":\"中国\"},\"links\":[{\"name\":\"Google\",\"url\":\"http://www.google.com\"},{\"name\":\"Baidu\",\"url\":\"http://www.baidu.com\"},{\"name\":\"SoSo\",\"url\":\"http://www.SoSo.com\"}]}";
        Map keyAndPathMap = new HashMap<>();
        analyzeJson(keyAndPathMap, jsonStr,"");
        keyAndPathMap.forEach((k,v) -> System.out.println(String.format("[Key]: %-20s[Path]: %-30s",k,v)));
    }
}

Share


用漫画的形式生动形象地讲述了服务熔断的概念与作用。
什么是服务熔断

你可能感兴趣的:(ARTS-Week6 有序链表合并、DevOps、Json解析、服务熔断)