java - creating nested hashmap from text file: not all data stored, may be over written -


i need read 2 files, add them hashmap , make comparison. it's large file manually. here java code :

 /**      * @param args command line arguments      */     public static void main(string[] args) throws filenotfoundexception, ioexception {         // todo code application logic here         bufferedreader in1 = new bufferedreader(new filereader("comparetheresults/595231gov_nov_13_assessed.txt"));         bufferedreader in2 = new bufferedreader(new filereader("comparetheresults/627231farsidetect.txt"));            hashmap<string, hashmap<string, arraylist<string>>> hmap1 = new hashmap<string, hashmap<string, arraylist<string>>>();           hashmap<string, hashmap<string, arraylist<string>>> hmap2 = new hashmap<string, hashmap<string, arraylist<string>>>();           arraylist<string>  rel= new arraylist<string>();           string id = null;            string line = null;          string relation = null;          while ((line = in1.readline()) != null){              if(line.contains("id:")){                  hmap1.put(line, new hashmap<string, arraylist<string>>());                  id = line;              }              else if(line.contains("relation:")){                  relation = line;              }              else if(line.contains("result:")){                  if(rel == null)                     rel= new arraylist<>();                  rel.add(line);                  hmap1.get(id).put(relation, rel);              }              else if(line.contains("tp") || line.contains("fp") || line.contains("tn") || line.contains("fn")){                  rel.add(line);                  hmap1.get(id).put(relation, rel);                   rel = null;              }           }          (object e : hmap1.entryset()) {             system.out.print(e);         }         system.out.println();            in1.close();         in2.close();     }  } 

here's format of file read

id: 01
sentence: blah blah blah
relation: blah blah
result: abc
tn

id: 01
sentence: blah blah blah
relation: blah blah
result: xyz
fp

id: 02
sentence: blah blah blah
relation: blah blah
result: blah blah
fp

the problem, can see 1 entry of each id in result. other data may over-written. there wrong did, in hashmap format?

help please.

thanks.

your problem have file same id exists multiple times, , you're not handling fact case.

you check see if result: entry in map, , add result arraylist stored @ key specified in relation: don't check see if id exists in map before creating new hashmap.

this line what's overwriting data each id

if(line.contains("id:")){      hmap1.put(line, new hashmap<string, arraylist<string>>());      id = line; } 

if line contains id: create new hashmap @ whatever key defined in id:, if there exists one.

if want add data stored in key, need same thing you're doing logic result:

if(line.contains("id:")){     if ( hmap1.containskey(line) ) {         // entry ,     }     else {          hmap1.put(line, new hashmap<string, arraylist<string>>());     }     id = line; } 

also, seems have small issue processing of result: field in you're doing odd arraylist store results. way it's coded, doesn't seem want. suppose have result, , add new list, , later want add else list of result, never list out of hashmap add it, make new arraylist each time.

try instead:

else if(line.contains("result:")){     // check if key exists in map, or if value null     if( !hmap1.get(id).containskey(relation) || hmap1.get(id).get(relation) == null ) {         rel = new arraylist<>(); // make new list     }     // otherwise, list map     else {         rel = hmap1.get(id).get(relation); // result list     }     // add result list     rel.add(line);      // add list map...     hmap1.get(id).put(relation, rel); } 

hope helps.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -