2

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. E.g. 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?

serendipity
  • 852
  • 13
  • 32
  • 5
    I read this, it seems that you need a better data structure for it. E.g. multiMap or something like that, since you said the key in both inner maps are always same. However i cannot further suggest, because I don't know the exact requirement. – Kent Jan 11 '17 at 13:24
  • Are both `Map`'s `keySet`s guaranteed to be of the same `size`? – CraigR8806 Jan 11 '17 at 13:24
  • 6
    This seems like a weird choice of types, why not have a `Words` class, which holds both left and right, and have a `Map`. Or maybe also store the noun itself and just have a `List`. – Jorn Vernee Jan 11 '17 at 13:24
  • @CraigR8806 Yes..they will be of the same size. – serendipity Jan 11 '17 at 13:32
  • @serendipity see my answer then, it should accomplish what you are requesting – CraigR8806 Jan 11 '17 at 13:33
  • @CraigR8806 Lamda expressions are a part of Java8 right? I'm restricted to Java 6 as I'm building this on top of the Stanford NLP libraries which require Java 6. – serendipity Jan 11 '17 at 13:35
  • @serendipity Any restrictions should be listed in your original post. I will modify it to be compatible with J6, however it will be more verbose – CraigR8806 Jan 11 '17 at 13:37
  • 1
    Are you sure that you need Java6? Even if the library needs to be compiled under java6, you still should be able to use it under java8. – Jorn Vernee Jan 11 '17 at 13:38
  • @CraigR8806 Should I convert this to a multi map like Kent suggested? I want to keep this solution simple. And if multi map is the way to go then how does one implement one? – serendipity Jan 11 '17 at 13:39
  • afaik multimap is not in the Java standard library. Besides, using a class helps keep things simple, because it adds a layer of abstract. – Jorn Vernee Jan 11 '17 at 13:43
  • serendipity I have edited my answer to be compatible with Java 6. It may benefit you more to look into a different data model like @Kent suggested, if you have the luxury of having the ability of changing your model. Sometimes projects are so big that to change a model would mean millions of lines of rewrite – CraigR8806 Jan 11 '17 at 13:44
  • @JornVernee I know this looks unwieldy...This is not an easy probem to solve to begin with i.e. identifying Named Entities and classifying based on surrounding context. The reason why I chose this type is coz I have a method that iterates over a sentence and all the proper nouns found in it to extract left and right words. I wanted to return both left and right words in that method but since we can return only one value I decided to return a map of two maps. The keys help in associating the correct surrounding words to the proper noun as there can be multiple proper nouns in a sentence. – serendipity Jan 11 '17 at 13:46
  • 2
    In that case you should go with the `List`, where `Words` is just a tuple for the 3 values, the class would do the association, but you get rid of this complex data structure. – Jorn Vernee Jan 11 '17 at 13:55
  • Similar to the following questions: http://stackoverflow.com/questions/4120216/map-of-maps-how-to-keep-the-inner-maps-as-maps , http://stackoverflow.com/questions/3093718/map-of-maps-data-structure , http://stackoverflow.com/questions/5056708/storing-hashmap-in-a-hashmap – TiyebM Jan 11 '17 at 16:34

2 Answers2

0

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
    }
}
CraigR8806
  • 1,584
  • 13
  • 21
0

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();
                }
            }
TiyebM
  • 2,684
  • 3
  • 40
  • 66