Nik Nik - 4 months ago 14
Java Question

Populate an array of objects using hashmap

I am new to hashmaps and even though I was trying to read about it, I couldn't really solve my issue.

This is the part of my code where I have this issue.

public HeatmapData(HashMap<Node, ArrayList<ODData>> ODs)
/* get all of the 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];

for (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 you can see I can't really find a way that will copy the data from the original hashmap to my array of objects.

I basically need to find the first node in array "row", find its values from the hashmpap, populate my array "routes[]", and then do the same with the second node in array "row". It seems like every time I try I have miss match error or a null value.

The program reads from two files that look as follows:


100001 200002 6
100001 200003 9


100001 -97.5489 30.5846
100002 -97.6958 30.5986

My node class consists of

int ID;
double longititude;
double lattitude;

And my ODData class consists of

Node origin;
Node destination;
double value;

Each 10000 number has many 20000 numbers relating to it.

Using the hashmap that is being send to the function, I need to populate my ODData routes[] array with these ODData objects so it will look like the original file (the first one).

Any idea how it can be done?
I'll appreciate any help!



You have to iterate the hash map and get the value of map and convert the arraylist (value of your hash map) to the array using toArray() of arraylist. But in your design the routes array has limited size, which will 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];

    for (String key : map.keySet()) {
        alist = map.get(key);
        // this will have an impact as your array size limited to
        // ROUTES_SIZE
        tmpRoutes = alist.toArray(new ODData[alist.size()]);
        System.arraycopy(tmpRoutes, 0, routes, totalLength,
        totalLength = totalLength + tmpRoutes.length;
    for (int i = 0, length = routes.length; i < 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);
    heatMap.put("key1", list1);
    heatMap.put("key2", list2);

    FetchMapData fetchMapDataObj = new FetchMapData();



s5      s6      3.1
s7      s8      4.1
s1      s2      1.1
s3      s4      2.1

You modify the String in hashmap to Node as per your requirement. if you want to maintain the sorting order then use TreeMap instead of HashMap.