Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Java data structure is best for two-way multi-value mapping

I'm relatively new to Java and I have a question about what type of data structure would be best for my case. I have a set of data which are essentially key-value pairs, however each value may correspond to multiple keys and each key may correspond to multiple values. A simplified example would be:

  • Red-Apple
  • Green-Apple
  • Red-Strawberry
  • Green-Grapes
  • Purple-Grapes

Considering the above example, I need to be able to return what color apples I have and/or what red fruits I have. The actual data will generated dynamically based upon an input file where each set will be anywhere from 100-100,000 values and each value may correspond to hundreds of values in the other set.

What would be the most efficient way to store and parse this data? I would prefer a solution as native to java as possible rather than something such as an external database.

This question is related, but I'm not sure how to apply the solution in my case given that I would need to assign multiple values to each key in both directions.

like image 812
user4588937 Avatar asked Feb 20 '15 17:02

user4588937


People also ask

Which data structure is most appropriate for map data structure?

The map data type is referred to as an associative array because, like an array, it is a collection of values rather than a single value, as an Int or a String is. Furthermore, each distinct key is associated with a value, resulting in an associative array.

What is multi value map in Java?

A MultiValueMap decorates another map, allowing it to have more than one value for a key. A MultiMap is a Map with slightly different semantics. Putting a value into the map will add the value to a Collection at that key. Getting a value will return a Collection, holding all the values put to that key.

Can we store multiple values in HashMap?

In Java, HashMap is used to store the data in Key – Value pairs. One key object is associated with one value object.

Can Java map have multiple keys?

Unfortunately, the Java Map interface doesn't allow for multiple key types, so we need to find another solution.


2 Answers

As you can't have duplicate keys in a Map, you can rather create a Map<Key, List<Value>>, or if you can, use Guava's Multimap.

Multimap<String, String> multimap = ArrayListMultimap.create();
multimap.put("Red", "Apple");
multimap.put("Red", "Strawberry");

System.out.println(multimap.get("Red"));  // Prints - [Apple, Strawberry]

But the problem is you can't ask for the keys of a given object, I'll keep looking and make and edit if I find something else, hope it helps.

Still, you can make the reverse yourself by iterating the map and finding the keys for the object.

like image 138
Luke SpringWalker Avatar answered Oct 06 '22 03:10

Luke SpringWalker


I suggest you use Guava's Table structure. Use color as your row keys and fruit as your column key or the other way round. Specifically, HashBasedTable is well suited for your case.

As per your use case, you wouldn't need to store anything for the values. However, these Tables don't allow null values. You could use a dummy Boolean or any other statistical useful value, i.e. date and time of insertion, user, number of color/fruit pairs, etc.

Table has the methods you need, such as column() and row(). Bear in mind that the docs say that these structures are optimized for row access. This might be OK for you if you plan to access by one key more than by the other.

like image 39
fps Avatar answered Oct 06 '22 05:10

fps