I was wondering if it is possible to compare items in multiple hashMaps to each other:
HashMap<String,String> valueMap = new HashMap<String, String>();
HashMap<String,Integer> formulaMap = new HashMap<String, Integer>();
What I would basically like to do is something like:
if(the second string in valueMap is the same as the first string in formulaMap){
}
Is there a short way to achieve this or do I have to compare the strings before they are included into the hashMaps. My Integer at this stage of the program is required to take a null value. I can achieve my goals with a multi-dimensional array, but a solution like this would be more elegant and less time consuming.
By using a
LinkedHashMapyou can have a map that respects the insertion order of different values. Everything you have to do is iterate over theentrySetof the map until you reach the position you’re looking for.Plus: If you also need ordering, you can have a look at the
TreeMapwhich inserts elements in order based on a criteria defined by you (You can pass aComparatoras a parameter for the map).This order will apply to the keys of the map tough, so if you need value ordering you’re going to have to come up with a little more complex solution (as in sorting the entry set directly and adding the values to another map, for example).