I have tens of thousands of records in a map. Map keys are string like s3://mybucket/some/path/2021/03/03/file.txt, s3://mybucket/some/path/2021/03/04/file.txt, value is 0 or 1.
So far I use HashMap but the memory usage is too high, I want to reduce it.
I am looking for a something that is key-value and utilizes key part reusability. What comes to mind naturally is the use some tree structure to store the prefixes.
Could somebody point to an appropriate implementation, preferably lightweight?
A
Prefix Treeis a possible solution that will be lightweight in terms of space complexity. Here is aJavaimplementation of aPrefix TreeusingTrie. I used aHashMapfor thechildas I'm not sure of the alphabet size. You can definitely use a fixed length for thechildTrieNodeif you are certain about the alphabet size in your dictionary. EachTrieNodewill store thevalueof the key only if theTrieNodemarks the end of the string. When you find theTrieNodewhich stores the lastcharacterof your key, you can then access thevalue;