HashMap alternatives for memory-efficient data storage

前端 未结 10 1423
逝去的感伤
逝去的感伤 2020-12-24 06:50

I\'ve currently got a spreadsheet type program that keeps its data in an ArrayList of HashMaps. You\'ll no doubt be shocked when I tell you that this hasn\'t proven ideal.

10条回答
  •  南方客
    南方客 (楼主)
    2020-12-24 07:16

    keeps its data in an ArrayList of HashMaps
    Well, this part seems terribly inefficient to me. Empty HashMap will already allocate 16 * size of a pointer bytes (16 stands for default initial capacity), plus some variables for hash object (14 + psize). If you have a lot of sparsely filled rows, this could be a big problem.

    One option would be to use a single large hash with composite key (combining row and column). Although, that doesn't make operations on whole rows very effective.

    Also, since you don't mention the operation of adding cell, you can create hashes with only necessary inner storage (initialCapacity parameter).

    I don't know much about google collections, so can't help there. Also, if you find any useful optimization, please do post here! It would be interesting to know.

提交回复
热议问题