data structures - Java datastructure to map multiple keys to the same value -


In Java, I am looking for a way to map multiple keys to the same value. Suppose I have a key of 0-9, and the values ​​of "x", "y" and "z" are as follows:

  0-> y 1- & Gt; Y 2 -> y 3-> x 4-> x 5-> y 6-> gt; Z 7-> y 8- & gt; Z 9- & gt; Z  

Now X, Y, and Z are really long strings, and I have millions of keys, so I can not afford to store the string several times. How would you go about it?

I had an idea that two arrays are formed: An artificial second is produced for the key for which the original key is mapped and another array is the key to the actual values. In this way the values ​​are stored only once and the original key can still be indirectly mapped on values:

  0-> K1 1-> K1 2- & gt; K1 3- & gt; K2 4-> K2 5- & gt; K1-6-6; & Gt; Of 3-7-gt; K-8-> K3-9-> K3- k1-> Y k2-> X3- & gt; Z  

Question though: Is there a better data structure for this?

any map and lieutenant; Integer, string & gt; does - you are accumulating only the context of the string, not a copy of it, so no matter how long it is

if you use the same string value multiple times If you are creating, use intern () to get the same string object for each time value.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -