0

Is there any way to parameterize Apache Commons' MultiKeyMap with multiple keys? i.e.

MultiKeyMap<String, String, Integer, Float> m = new MultiKeyMap<String, String, Integer, Float>();

I originally had raw MultiKeyMaps but because of the frequency of these maps in my program I want to ensure consistency by parameterizing them.

I don't want to use MultiKeys because the data I am storing in the MultiKeyMap requires MultiKey<String, String, Integer>. However, MultiKey only supports keys of one type (MultiKey<String> or MultiKey<Integer>). If there is a workaround to this I am open to suggestions though.

To be clear, the overall problem I want to solve is this: I want to make sure the MultiKeyMaps in my program have the same types of keys by parameterizing them, but with different classes within the key (<K, K, K, V> = <String, String, Integer, Float>).

EDIT: It was suggested (in a now-deleted answer) I use a Triple<String, String, Integer>, and I tried:

private static MultiKeyMap<Triple<String, String, Integer>, Float> multiKeyMap = new MultiKeyMap<Triple<String, String, Integer>, Float>();
Triple<String, String, Integer> t = Triple.of("asdf", "asdf", 4);
multiKeyMap.put(t, 5);

However, at the third line of this statement it seems to expect something in the form of put(Triple<String, String, Integer> key1, Triple<String, String, Integer> key2, Float value). My question is now this: With this answer, do I now have no need for a MultiKeyMap? Should I be using a Map<Triple<String, String, Integer>, Float> now?

It is also worth noting that I need to iterate across this map frequently (with a large amount of data), which was easy when I had a non-parameterized MultiKeyMap because I could just change the parameters in the get() method as needed. Using a Triple, I believe I would have to create a new Triple instance each iteration; is this going to resource-heavy or is there a way around this?

17slim
  • 1,233
  • 1
  • 16
  • 21
  • 1
    MultiKeyMap allows only one type for the key, so options I can see: 1) use it without generics 2) collapse your key types to string 3) use the map with Triple 4) write your own wrapper around a MultiKeyMap that allows for different key types (i.e. contain the type warnings). Constructing the triple (#3) will not be a huge performance impact, and would be the simplest way forward from a code perspective. If you have access to ready performance testing, try it before and after, see what the impact is. Otherwise you may be prematurely optimizing. – Taylor Dec 21 '15 at 15:34
  • @Taylor So, if I use the `Triple` class, do I have to use a `HashMap` instead of a `MultiKeyMap` then? – 17slim Dec 21 '15 at 15:37
  • 1
    If you're using Triple for the key, any Map impl will do (except MultiKeyMap which expects a MultiKey in the standard map methods). – Taylor Dec 21 '15 at 15:41
  • @Taylor Ok, thank you! Could you post the `Triple` part as an answer? – 17slim Dec 21 '15 at 15:48

1 Answers1

1

You can use the Triple with any map impl (e.g. HashMap).

You'll need to construct a Triple for each lookup, this will have a non-zero performance impact but shouldn't be a huge deal and will have the cleanest code imo.

Suggest you run performance test before and after applying the Triple change and see if the impact is acceptable, and move forward with that if it is.

Taylor
  • 3,942
  • 2
  • 20
  • 33