I need to use some kind of double map in my project. I can imagine this as Map with common key and triple value, but maybe there's better option.
Is there any implementations of this kind of map? Maybe some free library?
Create two small Java POJOs for title and value attributes MyKey and MyValue and you can now have Map<MyKey, MyValue> in this case you can extend the attributes of the Key and Value in future.
You could use a Triplet from http://www.javatuples.org/.
Or, you could create your own class, like shown here: https://stackoverflow.com/a/2671052/5066232.
Related
My application requires a multivaluemap to store in redis cache with a key having 2 different objects saying object1 and object2. Map can return all the values or a single value based on type parameter as in get(key, type).
Couldn't find such feature in either reddison or jedis. Is there any other library providing this data structure or I would have to implement my own?
As I know, there is no this kind of data structure in Redis, you have to do a little design by yourself, using extra keys to store extra information.
I have been using a HashMap which handles key / value pairs. But, now i need to handle key: value , value. Is it possible to have one key with 2 values ?
can you recommend a data structure/collection or strategy for me?
Yes you can, assuming this is c++, you can have
std::unordered_map<key, std::pair<value, value>>;
You can make the std::pair whatever type you'd like them to be.
What'd make more sense to do is to make an object to hold your two values, like HashMap<KeyType, ContainerObject> map.
In container object, you can use something like a list, or your own defined object that's made to just hold whatever two values you need. This way, you can use the HashMap and just access whatever values you need through the object that holds them.
I am aware that Google's guava and even Apache commons offer bi-directional maps. However, I would like to know how they are implemented. From the little I found, the most simplest way is to use 2 separate maps to store key/value and value/key data. However, surely there are better ways? Surely Google's implementation is not this straightforward? Where is the magic?
Thanks!
Apparently that's all it is (Louis Wasserman).
If you think about it, a type of bidirectional map could actually be implemented with a single Object->Object map, with the (key,value) pair inserted into the map twice, once for each direction. As long as you didn't require an explicit inverse view, it would work. In the case where the keys and values were of the same type (allowing the same object to be a key and a value), you could only have a single mapping between a given key and value (O1->O2 implying O2->O1), whereas with traditional BiMaps you could effectively have two (O1->O2 with inverse O2->O1 AND O3->O1 with inverse O1->O3). But for many bidirectional map needs, the single-map solution would suffice.
I want to store information using data structure.
For example, I have a data similar to:
Code Applicable values
001 A,B,C,D
004 C,D
005 P,Q,R,S
007 S,C
..
..
..
1000 (Code, Applicable values pair)
Straightforward solution I can think of is having HashMap with key type String and value type HashSet.
I was informed before by architect that having Set in Map is not a good idea.
Any suggestion on how to go about implementing this?
A HashMap<String, HashSet<String>> looks just fine to me for holding the type of data you've shown.
That "architect" of yours might be right about too much complexity when we're talking about how that data structure is exposed to the rest of your program. For example:
That type signature above does not make any statement about what kind of strings are used as keys, and what kind of values are in the value sets. Do they represent names? Or ISBN numbers? Or any text, or only particular enumeration values? etc.
If you use such a HashMap everywhere in your code, when adding values into the sets you will have to remember everywhere to do two steps: (1) creating an empty set only if no value is in the dictionary for some given key, and (2) adding the new value to the set of a given key. Having to think about routine stuff like that opens up the door for bugs.
Perhaps it would be better to hide your actual data structure behind a nice, simple and easy-to-use "collection" interface, perhaps similar to the following:
interface ApplicableValuesCollection
{
void Add(int code, String value);
void Remove(int code, String value);
bool Contains(int code, String value);
Iterable<String> GetValuesOfCode(int code);
…
}
I need a map that has two keys, e.g.
Map2<String /*ssn*/, String /*empId*/, Employee> _employees;
So that I can
_employees.put(e.ssn(), e.empId(), e)
And later
_employees.get1(someSsn);
_employees.get2(someImpId);
Or even
_employees.remove1(someImpId);
I am not sure why I want to stop at two, why not more, probably because that's the case I am I need right now :-) But the type needs to handle fixed number of keys to be type-safe -- type parameters cannot be vararg :-)
Appreciate any pointers, or advice on why this is a bad idea.
I imagine the main key would be empId, so I would build a Map with that as the key, i.e. empId ---> Employee. All other unique attributes (e.g. ssn) would be treated as secondary and will use separate Maps as a lookup table for empId (e.g. ssn ---> empId).
This implementation makes it easy to add/remove employees, since you only need to change one Map, i.e. empId ---> Employee; the other Maps can be rebuilt only when needed.
My first thought was: the easiest way to do this, I think, would be two maps.
Map< String, Map< String,Employee> > _employees;
But from what it looks like, you just want to be able to look up an employee by either SSN or ID. What's to stop you then from making two maps, or at worst a class that contains two maps?
As a clarification, are you looking for a compound key being employees are uniquely identified by the combination of their SSN and ID, but not either one by itself, or are you looking for two different ways of referencing an employee?
The Spiffy Framework appears to provide exactly what you`re looking for. From the Javadocs:
A two-dimensional hashmap, is a
HashMap that enables you to refer to
values via two keys rather than one
The relevant class is TwoDHashMap. It also provides a ThreeDHashMap.