How to create a lookup table with one key and 4 values?

Miguel Rozsas :

I manage to create a lookup table with 1 key and 2 values:

public static Map<String, Map.Entry<String, String>> myMap;
myMap= new HashMap<String, Map.Entry<String, String>>();

myMap.put("key 1", new AbstractMap.SimpleEntry<String, String>(" value 1.1", "value 1.2"));
myMap.put("key 2", new AbstractMap.SimpleEntry<String, String>(" value 2.1", "value 2.2"));
// and so on...

// retrieve the key, values
Map.Entry<String, String> pair;
for (String key : myMap.keySet() ) {
            pair = oidMap.get(key);
            value1= pair.getKey();
            value2= pair.getValue();
            ///use the key, value1, value2 as needed
}

But when I've tried to extend this to 4 values I got a bunch of errors that I figure out that the Entry is limited to 2 values only !

public static Map<String, Map.Entry<String, String, Float, String>> myMap;
myMap= new HashMap<String, Map.Entry<String, String, Float, String>>(); 

So, please, how do to something similar for 1 key and 4 values ? I cannot extend this by declaring a inner map.entry for the second argument of the first map.Entry because at that point, the first String is not unique anymore, I mean, all arguments of Map.Entry> may repeat, none can be used as a key.

And you guys, don't need to go for the path I took. You are free to provide any solution/data structure that allows me to store and retrieve 3 values as strings and one float, all associated to a key as String.

best regards,

vlumi :

Java doesn't have tuples built in, although such types are fairly easy to implement, and have been for Spark for instance, with up to ten values I believe.

You can create your own class containing the values. If you don't have a lot of combinations in your program, a plain class with fixed fields would be best, as it would allow to give your fields names, too, providing documentation of their purpose.

If your combination of fields and types need to be more dynamic, just create a generic class with four distinct types, similar to how Map.Entry is done.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326792&siteId=1