You have two hashmaps HM1 and HM2 where key = Id(long) value = timestamp. You need to give a program to return a list of Ids combined from both the hashmaps such that they are sorted as per their timestamps.
My Solution:
a. Wrap the object timestamp and id, in another object. Write a comparator on the basis of timestamp of the object, sort the list of objects and return the ids.
Any more intelligent way to do it?
Sounds like a perfectly reasonable way to go to me. You’ll need to consider the situation where a single ID appears in both maps, but other than that it sounds very straightforward.
Note that you don’t necessarily need to have a separate external comparator – you could make your new class implement
Comparable<T>for itself. That would work equally well. For extra credit you could even potentially implement both solutions and compare and contrast them 😉