简体   繁体   中英

How do I iterate over a LinkedHashMap of LinkedHashMaps?

My map looks like this :

LinkedHashMap <LinkedHashMap <String,String>,LinkedHashMap <String,String>> leftRightWords

Where the first map contains left words of a proper noun and second map contains right words of a proper noun. Eg in :

"Following the Rhode Island solution provider Atrion's decision to sell"

map1 will have entries like:

Rhode Island, Following the 
Atrion, solution provider

map2 will have entries like:

Rhode Island, solution provider
Atrion, decision to sell

In both maps the keys are the same but the values differ based on left and right words. How do i iterate over this map to extract the left words and right words to analyze them?

You can use a nested forEach loop to extract the data you desire, like so:

for(LinkedHapMap<String, String> lhm: leftRightWords.keySet()){
    for(String k:lhm.keySet()){
       String left = lhm.get(k);
       String right = leftRightWords.get(lhm).get(k);
       //do something with these Strings
    }
}

You can do it this way:

Iterator<LinkedHashMap<String, String>> resultKeys = leftRightWords
                    .keySet().iterator();
            while (resultKeys.hasNext()) {
                LinkedHashMap<String, String> tempKey = resultKeys.next();
                LinkedHashMap<String, String> tempVal = leftRightWords.get(tempKey);
                System.out.println("Key:");
                for (Map.Entry<String, String> entry : tempKey.entrySet()) {
                    System.out.println(entry.getKey() + "   "
                            + entry.getValue());
                }
                System.out.println();
                System.out.println("Value:");
                for (Map.Entry<String, String> entry : tempVal.entrySet()) {
                    System.out.println(entry.getKey() + "   "
                            + entry.getValue());
                    System.out.println();
                }
            }

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