Java – memory efficient multivalued mapping
Hi, I have the following questions:
The most efficient memory is to store string in sorted string array and provide corresponding two-dimensional int array for values Therefore, the access will be a binary search on the string array and get the corresponding value
Now I have three ways to achieve my goal:
>I use a sorted multivaluemap (treemap) to create everything After I have finished getting all the values, I call map Keyset() to get the string array toArray(new String [0]); Create a two - dimensional int array and get all the values from the multi - valued map Pro: it's easy to implement and still fast in the creation process Con: copying from map to arrays takes up more memory. > I used arrays or ArrayLists from the beginning and stored everything there professionally: minimal memory overhead Con: this will be very slow because I have to sort / copy the array every time I add a new key In addition, I need to implement my own (possibly slower) sorting to keep the order of the corresponding int array the same string Difficult to implement > I use arrays and multivaluemap as buffers After the program completes 10% or 20% of the creation phase, I will add the values to the array and keep the order, and then start a new map Pro: enough speed and enough memory efficiency Scam: difficult to implement
None of these solutions suits me Are you aware of any other solution to this problem that might be a memory efficient (multivalue) map implementation?
I know I can use the database, so don't publish it as an answer I want to know how to do this without using a database
Solution
If you switch to guava's Multimap – I don't know if your application can – you can use trove and get
ListMultimap<String,Integer> multimap = Multimaps.newListMultimap( new HashMap<String,Collection<Integer>>(),new supplier<List<Integer>>() { public List<Integer> get() { return new TIntListDecorator(); } });
This will cause listmultimap to use HashMap to map to the list value supported by int [] array, which should be memory efficient, although you will pay a small speed penalty for boxing You may be able to do something similar for multivaluemap, although I don't know which library it comes from