inzero
inzero

Reputation: 75

Can I store 50 mb of string type key value pair data in Java Hash Map

I want to store around 5 million unique strings in Java hash map. Key and value will be the same string. String maximum length will be 15 characters (ASCII).Later I want to lookup whether a particular string exists in the hash map.

Do i need to worry about the memory size issue for the above scenario. I am guessing like it wont need more than 75 mb to 150 mb memory.

Thanks in advance.

Upvotes: 1

Views: 834

Answers (1)

Jacob G.
Jacob G.

Reputation: 29680

Key and value will be the same string.

Using a Map when its keys and values will be the same is redundant. You should use a Set instead, especially if you only plan to use contains.

Regarding your concern about space, let's assume that each String has a length of 15 characters.

Assuming you're using Java 9, Latin 1 characters only require a single byte, so 5 million unique Strings will require at most 75_000_000 bytes or 75 MB.

Java 8 and below back their Strings with char[], so you'd essentially require twice as much memory in that case.

Upvotes: 2

Related Questions