Java large datastructure for storing a matrix

后端 未结 9 2050
北恋
北恋 2021-01-13 02:17

I need to store a 2d matrix containing zip codes and the distance in km between each one of them. My client has an application that calculates the distances which are then s

9条回答
  •  旧时难觅i
    2021-01-13 03:06

    A 2d array would be more memory efficient. You can use a small hashmap to map the 952 places into a number between 0 and 951 . Then, just do:

    float[][] distances= new float[952][952];
    

    To look things up, just use two hash lookups to convert the two places into two integers, and use them as indexes into the 2d array.

    By doing it this way, you avoid the boxing of floats, and also the memory overhead of the large hashmap.

    However, 906304 really isn't that many entries, you may just need to increase the Xmx maximum heap size

提交回复
热议问题