2016-01-04 29 views
-2

是新來的Java我已經形成了一套結果在我想用java

 `Map<String, Map<String, List<String>>>` 

獲得各組的鍵和值,現在我想每個鍵和值設置

應該如何得到這個。任何人請建議我

在此先感謝。

+0

解釋一點,這不是很清楚 – Spooky

+0

[Java文檔的地圖](https://docs.oracle.com/javase/7/docs/api/java/util/Map.html)HTTPS ://docs.oracle.com/javase/7/docs/api/java/util/Map.html#keySet(),https://docs.oracle.com/javase/7/docs/api/java/util /Map.html#values() –

回答

-1

對我來說,用例子來學習要容易得多。我可以給你一個小例子。可能會是有用的。

public class MapHierarchy { 
public static void main(String[] args) { 
    // preparation 
    Map<String, Map<String, List<String>>> myTestMap = new HashMap<>(); 
    ArrayList<String> listOfValues = new ArrayList<>(); 
    listOfValues.add("Hello"); 
    listOfValues.add("my"); 
    listOfValues.add("little"); 
    listOfValues.add("friend"); 

    HashMap<String, List<String>> innerMap = new HashMap<>(); 
    innerMap.putIfAbsent("innerMapKey", listOfValues); 
    myTestMap.put("outerKey", innerMap); 

    // where the magic happens 
    System.out.println("Keys of outer map: " + myTestMap.keySet().toString()); 
    for (Map.Entry<String, List<String>> innerMapItem : innerMap.entrySet()) { 
     String innerMapItemKey = innerMapItem.getKey(); 
     System.out.println("Key of inner map: " + innerMapItemKey); 
     System.out.println("Values of inner map: " + innerMapItem.getValue().toString()); 
    } 
}} 
2

您需要查看documentationMap s。

myArbitrarilyNamedMap = new Map<String, Map<String, List<String>>>(); 
//do stuff so that myArbitrarilyNamedMap contains values 
Set firstLevelKeys = myArbitrarilyNamedMap.keySet(); //this bit 
+0

而且'Set's:https://docs.oracle.com/javase/7/docs/api/java/util/Set.html文檔也可能會有用。 –

+0

這裏也是一個關於迭代通過地圖http://stackoverflow.com/questions/46898/how-to-efficiently-iterate-over-each-entry-in-a-map的Q/A,這可能證明有啓發性。 –