關(guān)于Map的遍歷以及轉(zhuǎn)JsonArray存儲方式
更新時間:2024年11月04日 10:48:37 作者:瘋狂咕嚕咚
在Java開發(fā)過程中,經(jīng)常會遇到需要對復(fù)雜數(shù)據(jù)結(jié)構(gòu)進(jìn)行處理的情況,本案例以List<Map<String,Object>>為例,介紹了如何遍歷該數(shù)據(jù)結(jié)構(gòu),并根據(jù)特定條件篩選出符合要求的元素,通過自定義一個Edit類來模擬形成一個新的Map對象,實現(xiàn)了數(shù)據(jù)的有序存儲
Map的遍歷以及轉(zhuǎn)JsonArray存儲
案例目的將List<Map<String,Object>> 遍歷,按條件取得map里面符合要求的元素
將其有序的放到自定義的一個Edit類中,模擬形成一個新的Map對象:
- Edit類:
public class Edit { private String key; private String value; public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } }
- 測試類:
public class Test { public List<Map<String,Object>> see() { Map<String, Object> map = new HashMap<>(); map.put("wkx", "666"); map.put("laq", "999"); map.put("key", "datetime"); map.put("value", "2000"); Map<String, Object> map2 = new HashMap<>(); map2.put("wkx2", "666"); map2.put("laq2", "999"); map2.put("key", "dateid"); map2.put("value", "1000"); List<Map<String, Object>> listmap = new LinkedList<>(); listmap.add(map); listmap.add(map2); return listmap; } public static void main(String[] args) { Test a = new Test(); List<Edit> edits=new LinkedList<>(); List<List<Edit>> mapplus =new LinkedList<>(); List list = a.see(); for (int i=0;i<list.size();i++){ Edit edit = new Edit(); Map<String, Object> map = (Map) list.get(i); for (Map.Entry entry : map.entrySet()) { if ("key".equals(entry.getKey())) { edit.setKey((String)entry.getValue()); } if ("value".equals(entry.getKey())) { edit.setValue((String) entry.getValue()); } } if (edit.getKey()!=null&&edit.getValue()!=null) { edits.add(edit); } } mapplus.add(edits); System.out.println(list.toString()); } }
將mapplus里面的內(nèi)容全部存儲到數(shù)據(jù)庫中的一個字段中
public String list2string(List<List<Edit>> list){ //但我們得到一個全新的list<map>時,我們要把它里面所有的map(包括key和value都存到數(shù)據(jù)庫中的一個字段中去) // 我們用到j(luò)soAarray JSONArray jsonArray = new JSONArray(); //逐漸遍歷之前取得的mapplus list.forEach(n->{ n.forEach(e -> { String key = e.getKey(); String value = e.getValue(); }); jsonArray.add(n); }); return JSONArray.toJSONString(jsonArray); } //測試 mapplus.add(edits); String ss = new SortByLetter().list2string(mapplus); System.out.println(ss);
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
RabbitMQ實現(xiàn)Work Queue工作隊列的示例詳解
工作隊列(又稱任務(wù)隊列)的主要思想是避免立即執(zhí)行資源密集型任務(wù),而不得不等待它完成。本篇文章將記錄和分享RabbitMQ工作隊列相關(guān)的知識點,希望對大家有所幫助2023-01-01詳談spring中bean注入無效和new創(chuàng)建對象的區(qū)別
這篇文章主要介紹了spring中bean注入無效和new創(chuàng)建對象的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02詳解Spring Boot 部署jar和war的區(qū)別
本篇文章主要介紹了詳解Spring Boot 部署jar和war的區(qū)別,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09SpringBoot整合GitLab-CI實現(xiàn)持續(xù)集成的過程
這篇文章主要介紹了SpringBoot整合GitLab-CI實現(xiàn)持續(xù)集成,本文詳細(xì)講述了 GitLab-CI 持續(xù)集成的安裝、部署、以及配置,需要的朋友可以參考下2022-12-12SpringWebMVC的常用注解及應(yīng)用分層架構(gòu)詳解
這篇文章主要介紹了SpringWebMVC的常用注解及應(yīng)用分層架構(gòu),SpringWebMVC是基于ServletAPI構(gòu)建的原始Web框架,從?開始就包含在Spring框架中,感興趣的朋友可以參考下2024-05-05詳解lombok @Getter @Setter 使用注意事項
這篇文章主要介紹了詳解lombok @Getter @Setter 使用注意事項,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11