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

Posted 涛姐涛哥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用 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}

 

以上是关于使用 hashMap和treeMap开启多个摄像头的监控任务的主要内容,如果未能解决你的问题,请参考以下文章

深入理解HashMap和TreeMap的区别

Map集合:HashMap、TreeMap

HashMap与TreeMap

HashMap,LinkedHashMap,TreeMap的区别

HashMap 和 TreeMap 有啥区别? [复制]

java面试题之HashMap和TreeMap的区别