背景:今天有个需求,传人多个摄像头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搞定。
代码如下:
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class HashMapUtil {
public static void main(String[] args) {
HashMapUtil.testHashMap("camera01,camera02,camera03");
}
public static void testHashMap(String cameraStrList) {
String[] splitCameraList = cameraStrList.split(",");
HashMap<String, Object> hashMap = new HashMap<>();
//add and delete faster than arrayList when using linkList
LinkedList<Object> linkedList = new LinkedList<>();
for(String splitCamera : splitCameraList) {
hashMap.put("cameraId", splitCamera);
hashMap.put("createBy","tjt");
hashMap.put("startTime", new Date().toLocaleString());
linkedList.add(hashMap);
hashMap = new HashMap<>();
}
HashMap<Object, Object> paramMap = new HashMap<>();
paramMap.put("camera_id_list", linkedList);
paramMap.put("callback", "http://www.baidu.com");
log.info("linkedList: "+linkedList);
log.info("paramMap: "+paramMap);
}
}
执行结果:
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值;
代码如下:
package com.xinyan.springcloud.controller;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.TreeMap;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class TreeMapUtil {
public static void main(String[] args) {
TreeMapUtil.testTreeMap("camera01,camera02,camera03");
}
@SuppressWarnings({ "unchecked", "deprecation" })
public static void testTreeMap(String cameraStrList) {
String[] splitCameraList = cameraStrList.split(",");
LinkedList<Object> linkedList = new LinkedList<>();
@SuppressWarnings({ "unchecked", "unused", "rawtypes" })
TreeMap treeMap = new TreeMap(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
return 100;
}
});
for(String splitCamera : splitCameraList) {
treeMap.put("cameraId", splitCamera);
}
linkedList.add(treeMap);
HashMap<Object, Object> paramMap = new HashMap<>();
paramMap.put("camera_id_list", linkedList);
paramMap.put("callback", "http://www.baidu.com");
log.info("linkedList: "+linkedList);
log.info("paramMap: "+paramMap);
}
}
执行结果:
linkedList: [{cameraId=camera01, cameraId=camera02, cameraId=camera03}]
paramMap: {camera_id_list=[{cameraId=camera01, cameraId=camera02, cameraId=camera03}], callback=http://www.baidu.com}