java - Traversing through a map with nested lists as values -
here map
, string
key (textfile) , value (list
of integers):
aaa.txt : {(the=[11], put=[8], i'm=[1], by=[5], you,=[3, 7], the=[4, 10], key=[6, 9]} bbb.txt : {do.=[12], to=[6], i'm=[1], what=[9], you=[4, 10], want=[11], sure=[2]} ccc.txt : {just=[10], need=[7], you,=[6], it=[5], than=[3], it's=[1], the=[11]}
i want print out in format:
you, 3:7, , 6
as can see, want print above format "you," located in aaa.txt
, ccc.txt
not bbb.txt
should printed out word you, 3:7, ,6
.
if you
not found in bbb.txt, isn't, should have you, 3:7, "space", 6
. space show word not present in second text file.
just want print every word in map's value , keep. here map structure of what's above
hashmap<string, hashmap<string, list<integer>>> coolmap = new hashmap<string,hashmap<string, list<integer>>>(); expected/wanted output 20, 4:20 ,15 , , 16:17 , 16 ,
you try iterating through each map keys. using hashset eliminates duplicates.
hashset<string> keys = new hashset<>(); (entry<string, hashmap<string, list<integer>>> entry : coolmap.entryset()) { (entry<string, list<integer>> innerentry : entry.getvalue().entryset()) { string innerkey = innerentry.getkey(); keys.add(innerkey); }
then use keys search maps values.
string temp; iterator = keys.iterator(); while (it.hasnext()) { temp = (string) it.next(); system.out.print(temp + ", "); (entry<string, hashmap<string, list<integer>>> entry : coolmap.entryset()) { boolean hasvalue = false; (entry<string, list<integer>> innerentry : entry.getvalue().entryset()) { string innerkey = innerentry.getkey(); if (innerkey.equals(temp)) { system.out.print(innerentry.getvalue() + ", "); hasvalue = true; } } if (!hasvalue) { system.out.print(", ,"); } } system.out.println(); }
Comments
Post a Comment