背景:今天有個需求,傳人多個攝像頭ID,然後調用介面,開啟這些攝像頭的監控任務。 方法一: 如果你的攝像頭監控任務格式為: {camera_id_list=[{createBy=tjt, cameraId=camera01, startTime=2019-1-10 22:58:19}, {creat ...
背景:今天有個需求,傳入多個攝像頭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}