2017-06-19 99 views
-1

如何訪問Map <String, Object>類型的地圖內的列表?如何訪問地圖中的列表並通過該列表循環

public Map<String, Object> getListInsideMapObject(Long id, Date from) { 

    Map<String, Object> result = new HashMap<>(); 
    List<Map<String, Object>> mapList = new ArrayList<>(); 

    List<MappedList> conTime = new ArrayList<>(); 
    conTime = xxxRepository.findByxxx(id, from); 

    Map<String, Object> map = xxxService.xxx(id); 
    List<String> times = (List<String>) map.get("xxx"); 

    for (MappedList t : conTime) { 
     int num = 0; 
     Map<String, Object> res = new HashMap<>(); 
     res.put("x", ""); 
     res.put("status", null); 

     for (Contraction c : con) { 
      num++; 
      res.put("status", "stat"); 
      res.put("x", new Date()); 
     } 
    } 

    res.put("y", num); 

    mapList.add(res); 

    result.put("mapList", mapList); 
    result.put("mapListA", mapListA); 
    result.put("mapListB", mapListB); 
    //etc 

    return result; 
} 

我想調用這個服務(getListInsideMapObject),並從該圖和遍歷每個列表訪問每個列表。例如在課堂XXX我想打電話給getListInsideMapObject(長ID,從日期)作爲一種服務,並從地圖訪問各列表

+0

你可以發表[MCVE],以減少該代碼的複雜性。從我看到的,你只是有一個'名單'那麼是什麼問題 – AxelH

+0

你似乎已經有代碼,試圖做你在問什麼。它不起作用嗎?如果不是,你會得到什麼錯誤,或者預期與實際產出是什麼?無論您的代碼是否試圖按照您的要求去做,您是否可以更清楚地發佈[mcve]來證明您遇到的問題? – Dukeling

+0

w是什麼,弱是什麼。爲什麼你的方法叫做'getListInsideMapObject',但實際上是建立一個地圖?你應該更清楚。 –

回答

1

我想你想是這樣的:

public NewClass1() { 
    // have an instance from the class that gives you the map 
    ClassThatBuildsTheMap mapClass = new ClassThatBuildsTheMap(); 
    // get the map. must provide id and date 
    Map <String, Object> myMap = mapClass.getListInsideMapObject(id, date); 
    // access the lists inside the map 
    useListInsideAMap(myMap); 
} 

private void useListInsideAMap(Map<String, Object> map){ 
    // Prior to Java8: 
    for (Map.Entry<String, Object> entry : map.entrySet()) { 
     String key = entry.getKey(); // if you want to use the key 
     Object value = entry.getValue(); 
     if(value instanceof List){ 
      // I supose it is a list of sytrings 
      List l = (List) value; 
      for (Object item : l) { 
       // Do something with the item from the list 
      } 
     } 
    } 

    // Java8: 
    // loop through the map 
    map.forEach((String key, Object value)->{ 
     // if you want to use key, just call key 

     // checks if the value (Object) from the map is a list 
     if(value instanceof List){ 
      List l = (List)value; 
      // loop through the list 
      l.forEach((Object item)->{ 
       // Do something with the item from the list 
      }); 
     } 
    }); 
} 
+0

這個解決方案非常完美。謝謝 – joseph

0

你可以投你ObjectList

Map<String, Object> result = new HashMap<>(); 
List<Map<String, Object>> mapList= new ArrayList<>(); 
result.put("1", mapList); 
------------------------------------------------------- 
List<Map<String, Object>> = (List<Map<String, Object>>)result.get("1") 

然後,您可以循環通常使用for/foreach。

0

您也可以使用Optional對象通過鍵,讓您的地圖值和轉換爲所需的類型之前檢查它(避免ClassCastException問題):

Map<String, Object> someMap = new HashMap<>(); 

List<Map<String, Object>> result = Optional.ofNullable(someMap.get("id")) 
     .filter(obj -> obj instanceof List) 
     .map(obj -> (List<Map<String, Object>>)obj) 
     .orElse(Collections.emptyList()); 

在這種情況下,你將有空List(如元素錯過或輸入的不是List)或預期的List值。