-2

Am new to java i have formed a set of result in

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

now i want to get the keys and values of each set

How should get this. anyone please suggest me

thanks in advance.

  • Explain a bit more, that's not very clear – Spooky Jan 04 '16 at 20:01
  • [Java Docs for Map](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() – Eric G Jan 04 '16 at 20:03

2 Answers2

2

You will want to look at the documentation for Maps.

myArbitrarilyNamedMap = new Map<String, Map<String, List<String>>>();
//do stuff so that myArbitrarilyNamedMap contains values
Set firstLevelKeys = myArbitrarilyNamedMap.keySet(); //this bit
  • And likely also the docs for `Set`s: https://docs.oracle.com/javase/7/docs/api/java/util/Set.html will prove useful. – John Hascall Jan 04 '16 at 20:24
  • Here also is a Q/A on iterating through Maps http://stackoverflow.com/questions/46898/how-to-efficiently-iterate-over-each-entry-in-a-map which may prove instructive. – John Hascall Jan 04 '16 at 20:25
-1

For me is much easier to study with examples. I can give you a little example. May be it will be usefull.

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());
    }
}}
Toisen
  • 39
  • 1
  • 5