• 使用 hashMap和treeMap开启多个摄像头的监控任务


    背景:今天有个需求,传入多个摄像头ID,然后调用接口,开启这些摄像头的监控任务。

     

    方法一:

    如果你的摄像头监控任务格式为:

    {camera_id_list=[{createBy=tjt, cameraId=camera01, startTime=2019-1-10 22:58:19}, {createBy=tjt, cameraId=camera02, startTime=2019-1-10 22:58:19}], callback=http://www.baidu.com}

    这种格式的话,那么可以用hashMap搞定。


    代码如下:

     1 import java.util.Date;
     2 import java.util.HashMap;
     3 import java.util.LinkedList;
     4 
     5 import org.apache.commons.lang.time.DateFormatUtils;
     6 
     7 import lombok.extern.slf4j.Slf4j;
     8 
     9 @Slf4j
    10 public class HashMapUtil {
    11     
    12     public static void main(String[] args) {
    13         HashMapUtil.testHashMap("camera01,camera02,camera03");
    14     }
    15     
    16     public static void testHashMap(String cameraStrList) {
    17         String[] splitCameraList = cameraStrList.split(",");
    18         HashMap<String, Object> hashMap = new HashMap<>();
    19         //add and delete faster than arrayList when using linkList
    20         LinkedList<Object> linkedList = new LinkedList<>();
    21         for(String splitCamera : splitCameraList) {
    22             hashMap.put("cameraId", splitCamera);
    23             hashMap.put("createBy","tjt");
    24             hashMap.put("startTime", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
    25             linkedList.add(hashMap);
    26             hashMap = new HashMap<>();
    27         }
    28         HashMap<Object, Object> paramMap = new HashMap<>();
    29         paramMap.put("camera_id_list", linkedList);
    30         paramMap.put("callback", "http://www.baidu.com");
    31         log.info("linkedList: "+linkedList);
    32         log.info("paramMap: "+paramMap);
    33     }
    34 
    35 }

    执行结果:

    inkedList: [{createBy=tjt, cameraId=camera01, startTime=2019-1-10 22:58:19},{createBy=tjt, cameraId=camera02, startTime=2019-1-10 22:58:19}, {createBy=tjt, cameraId=camera03, startTime=2019-1-10 22:58:19}]
     paramMap: {camera_id_list=[{createBy=tjt, cameraId=camera01, startTime=2019-1-10 22:58:19}, {createBy=tjt, cameraId=camera02, startTime=2019-1-10 22:58:19}, {createBy=tjt, cameraId=camera03, startTime=2019-1-10 22:58:19}], callback=http://www.baidu.com}

     

    方法二:

    如果你的摄像头监控任务格式为:

    {camera_id_list=[{cameraId=camera01, cameraId=camera02, cameraId=camera03}], callback=http://www.baidu.com}

    二叉树treeMap,当传入的两个key值一样,会返回结果0,则会覆盖掉之前的cameraId,返回结果:{camera_id_list=[{cameraId=camera03}], callback=http://www.baidu.com};

    重写了treeMap中的比较方法,当比较结果返回值小于0,则排在二叉树的左边即集合倒序,当比较结果返回值大于0,则排在二叉树的右边亦不会覆盖之前的key值;

    代码如下:

     1 import java.util.Comparator;
     2 import java.util.HashMap;
     3 import java.util.LinkedList;
     4 import java.util.TreeMap;
     5 import lombok.extern.slf4j.Slf4j;
     6 
     7 @Slf4j
     8 public class TreeMapUtil {
     9     
    10     public static void main(String[] args) {
    11         TreeMapUtil.testTreeMap("camera01,camera02,camera03");
    12     }
    13     
    14     @SuppressWarnings({ "unchecked", "deprecation" })
    15     public static void testTreeMap(String cameraStrList) {
    16         String[] splitCameraList = cameraStrList.split(",");
    17         LinkedList<Object> linkedList = new LinkedList<>();
    18         @SuppressWarnings({ "unchecked", "unused", "rawtypes" })
    19         TreeMap treeMap = new TreeMap(new Comparator() {
    20             @Override
    21             public int compare(Object o1, Object o2) {
    22                 return 0;
    23             }
    24         });
    25         for(String splitCamera : splitCameraList) {
    26             treeMap.put("cameraId", splitCamera);
    27         }
    28         linkedList.add(treeMap);
    29         HashMap<Object, Object> paramMap = new HashMap<>();
    30         paramMap.put("camera_id_list", linkedList);
    31         paramMap.put("callback", "http://www.baidu.com");
    32         log.info("linkedList: "+linkedList);
    33         log.info("paramMap: "+paramMap);
    34     }
    35 }

    执行结果:

    linkedList: [{cameraId=camera01, cameraId=camera02, cameraId=camera03}]
    paramMap: {camera_id_list=[{cameraId=camera01, cameraId=camera02, cameraId=camera03}], callback=http://www.baidu.com}

  • 相关阅读:
    玩转Visual Studio Editor篇
    .Net2.0的集合操作 What i know?
    Log文件压缩
    [转]比较高效地实现从两个不同数组中提取相同部分组成新的数组(只支持Int类型) [C#]
    Stream 和 Byte[]互操作
    net 2.0 中如何在线程引发的事件中控制forms controls
    C# 操作Word文档(转)
    利用Lucene.net搜索引擎进行多条件搜索的做法
    c# 添加图片水印,可以指定水印位置+生成缩略图
    SDN第一次作业
  • 原文地址:https://www.cnblogs.com/taojietaoge/p/10253247.html
Copyright © 2020-2023  润新知