dictionary - multiple lists into a map -
i had question regarding code below. i'm reading file containing lots of data of stuff irrelevant. data written out on 1 line, cannot use nextline or something.
for each vertex
, save relevant information dataperpoint
. when go next vertex, want clear list fill new relevant information.
the issue have each time clear dataperpoint
, values in map cleared. when try fill it, previous positions in map same values.
how can , make sure each vertex
own list?
looking forward suggestions!
public static map<integer, list<double>> readdata(file f) // throws io exception? { // create map store vertex , list relevant information in list<double> dataperpoint = new arraylist<double>(); map<integer, list<double>> data = new hashmap<>(); // open scanner try (scanner in = new scanner(f)) { // make sure correct localization used in.uselocale(locale.us); // first 6 integers contain irrelevant information (int step = 1; step <= 6; step++) { in.nextint(); } // information vertex 0 separately, since has 1 data point less int vertex = in.nextint(); (int doubleinfo = 1; doubleinfo <= 4; doubleinfo++) // 6 relevant variables { dataperpoint.add(in.nextdouble()); } // irrelevant information (int irrelevantinfo = 1; irrelevantinfo <= 2; irrelevantinfo++) { in.nextint(); } // opening , closing of time window dataperpoint.add((double) in.nextint()); dataperpoint.add((double) in.nextint()); data.put(vertex, dataperpoint); while (in.hasnext()) // think of different statement later { dataperpoint = new arraylist<double>(); vertex = in.nextint(); (int doubleinfo = 1; doubleinfo <= 4; doubleinfo++) // 6 relevant variables { dataperpoint.add(in.nextdouble()); } // irrelevant information (int irrelevantinfo = 1; irrelevantinfo <= 3; irrelevantinfo++) { in.nextint(); } // opening , closing of time window dataperpoint.add((double) in.nextint()); dataperpoint.add((double) in.nextint()); data.put(vertex, dataperpoint); } in.close(); } catch (filenotfoundexception e) { e.printstacktrace(); }
use linkedhashmap<> instead of hashmap<> should solve problem. read difference between hashmap, linkedhashmap , treemap
Comments
Post a Comment