简体   繁体   中英

Extract String value from a TreeMap

Map 1 is a TreeMap and Map2 is a HashMap

How to extract value (String3[]) in a String array from a TreeMap (Map1) in Java. TreeMap is of type Map1<String1,Map2<String2,String3[]>>

I have used TreeMap.getKey().contains(String2) to get the Key of String 2. But I need to extract the values of the corresponding key ie String2 and store them in a string array.

If you have a Map<A, Map<B, C>> and want to access a specific C , you will need both an A and a B that tell you which C . Then you can use map.get(a).get(c);

Any map in the Java standard library (regardless of the concrete implementation) offers a get(key) method that yields a value for the given key (or null in case the key is not mapped).

In your case this means you need two keys: A String1 and a String2 . With the former you get() a map from which you can get() your desired result using the latter.

Collection entrySet = treeMap.entrySet();

    // Obtain an Iterator for the entries Set
    Iterator it = entrySet.iterator();

    // Iterate through TreeMap entries
    System.out.println("TreeMap entries : ");
    while(it.hasNext())

System.out.println(it.next());
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM