I have two following LinkedHashMaps sorted by values:
Map1 && Map2
3 | 2 && 3 | 4
2 | 2 && 5 | 2
4 | 1 && 7 | 1
If first value is unique (like value = 4 in Map2) -> return math expression: value* 10000* key = 4 * 10000 * 3
If first value is duplicated (double 2s in Map1) -> return value* 100* key + value* 100* key = 2 * 100 * 3 + 2 * 100 * 2
(It is important to know the first value (if equal to 1/2/3/4/etc.), because it will change the multiplication value (1/10/100/1000/etc.).
So what I am trying to do is obtain some specific integer result that is depended on the first and second (if needed) entryset - keys and values.
It is worth mentioning, that only numbers '2' can be duplicated (and only duplicated! not trippled or sth), that just comes out from the rules of my program.
I am looking for some clever and optimal solution for it but cannot come up with nothing that wouldn't require many lines of code...
Although, the only way that would allow me to go through keys and values of a LinkedHashMap is to iterate through it until it finds what I am looking for which cannot work here due to the possible duplicates.
int i = 0;
Map.Entry<Integer, Long> result = null;
for (Map.Entry<Integer, Long> entry: lhm.entrySet()) {
if (i == searched)
result = entry;
i++;
}
return Math.toIntExact(result.getValue());