使用hashmap填充对象数组

时间:2014-06-26 03:50:01

标签: java arrays for-loop hashmap populate

我不熟悉哈希图,即使我试图阅读它,我也无法解决我的问题。

这是我的代码中我遇到此问题的部分。

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]);            
    }

正如您所看到的,我无法找到将数据从原始hashmap复制到我的对象数组的方法。

我基本上需要找到数组“row”中的第一个节点,从hashmpap中找到它的值,填充我的数组“routes []”,然后对数组“row”中的第二个节点执行相同操作。似乎每次我尝试都会错过匹配错误或空值。

程序从两个文件中读取,如下所示:

首先:

100001 200002 6
100001 200003 9
...............

第二

100001 -97.5489 30.5846
100002 -97.6958 30.5986
...............

我的节点类由

组成
int ID;
double longititude;
double lattitude;

我的ODData类由

组成
Node origin;
Node destination;
double value;

每10000个号码都有许多与之相关的20000个号码。

使用正在发送给函数的hashmap,我需要使用这些ODData对象填充我的ODData routes []数组,使它看起来像原始文件(第一个)。

知道如何做到这一点? 我会感激任何帮助!

尼基塔

1 个答案:

答案 0 :(得分:0)

您必须迭代哈希映射并获取map的值,并使用arraylist的toArray()将arraylist(哈希映射的值)转换为数组。但在你的设计中,路径数组的大小有限,会有内存浪费。

公共类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,
                tmpRoutes.length);
        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);
    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);
}

}

<强>输出:

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

根据您的要求将hashmap中的String修改为Node。如果你想维护排序顺序,那么使用TreeMap而不是HashMap。

相关问题