java - Populate an array of objects using hashmap -
i new hashmaps , though trying read it, couldn't solve issue.
this part of code have issue.
public heatmapdata(hashmap<node, arraylist<oddata>> ods) { /* of origins */ node [] row = ods.keyset().toarray(new node[0]); node [] column = new node[row.length]; system.arraycopy(row, 0, column, 0, row.length); oddata [] routes = new oddata[routes_size]; (int i=0; i<routes_size; i++) { oddata temp = new oddata(); // node n = row[i]; routes[i] = temp; //routes = (oddata[]) ods.get(n).toarray(new oddata[0]); }
as can see can't find way copy data original hashmap array of objects.
i need find first node in array "row", find values hashmpap, populate array "routes[]", , same second node in array "row". seems every time try have miss match error or null value.
the program reads 2 files follows:
first:
100001 200002 6 100001 200003 9 ...............
second:
100001 -97.5489 30.5846 100002 -97.6958 30.5986 ...............
my node class consists of
int id; double longititude; double lattitude;
and oddata class consists of
node origin; node destination; double value;
each 10000 number has many 20000 numbers relating it.
using hashmap being send function, need populate oddata routes[] array these oddata objects original file (the first one).
any idea how can done? i'll appreciate help!
nikita
you have iterate hash map , value of map , convert arraylist (value of hash map) array using toarray() of arraylist. in design routes array has limited size, have memory wastage.
public class fetchmapdata {
static final int routes_size = 10; public void heatmap(hashmap<string, arraylist<oddata>> map) { int totallength = 0; arraylist<oddata> alist = new arraylist<oddata>(); oddata[] routes = new oddata[routes_size]; oddata[] tmproutes = new oddata[routes_size]; (string key : map.keyset()) { alist = map.get(key); // have impact array size limited // routes_size tmproutes = alist.toarray(new oddata[alist.size()]); system.arraycopy(tmproutes, 0, routes, totallength, tmproutes.length); totallength = totallength + tmproutes.length; } (int = 0, length = routes.length; < length; i++) { if (routes[i] != null) { system.out.println(routes[i].getorigin() + " " + routes[i].getdestination() + " " + routes[i].getvalue()); } } } public static void main(string[] args) { hashmap<string, arraylist<oddata>> heatmap = new hashmap<string, arraylist<oddata>>(); arraylist<oddata> list1 = new arraylist<oddata>(); arraylist<oddata> list2 = new arraylist<oddata>(); oddata obj1 = new oddata("s1", "s2", 1.1); oddata obj2 = new oddata("s3", "s4", 2.1); oddata obj3 = new oddata("s5", "s6", 3.1); oddata obj4 = new oddata("s7", "s8", 4.1); list1.add(obj1); list1.add(obj2); list2.add(obj3); list2.add(obj4); heatmap.put("key1", list1); heatmap.put("key2", list2); fetchmapdata fetchmapdataobj = new fetchmapdata(); fetchmapdataobj.heatmap(heatmap); }
}
output:
s5 s6 3.1 s7 s8 4.1 s1 s2 1.1 s3 s4 2.1
you modify string in hashmap node per requirement. if want maintain sorting order use treemap instead of hashmap.
Comments
Post a Comment