How to read and write a HashMap to a file?

前端 未结 5 437
南笙
南笙 2020-12-09 04:13

I have the following HashMap:

HashMap fileObj = new HashMap();

ArrayList cols         


        
5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-09 04:26

    I believe you're getting what you're saving. Have you inspected the map before you save it? In HashMap:

    /**
     * The default initial capacity - MUST be a power of two.
     */
    static final int DEFAULT_INITIAL_CAPACITY = 16;
    

    e.g. the default HashMap will start off with 16 nulls. You use one of the buckets, so you only have 15 nulls left when you save, which is what you get when you load. Try inspecting fileObj.keySet(), .entrySet() or .values() to see what you expect.

    HashMaps are designed to be fast while trading off memory. See Wikipedia's Hash table entry for more details.

提交回复
热议问题