What's confusing me is that both reviewers and several commenters here are suggesting code that is simply incorrect.
If you wait for the time between now and the timeout, you mandate that if the value is not in the hashmap immediately, the program will sleep for ~5 seconds no matter what. "the key was very likely to be there within the first second" is the entire rebuttal to their suggestion. You don't need anything else, why would you sleep for 5 seconds if you can only sleep for 1? Do you not care about runtime performance? Do you have no respect for the user who has to wait for the output?
Why wake up if your condition is not yet met? The wait timeout is the time after it should continue if it was not notified. It is meant to be waken up as soon as possible by a notify. If the value is available almost immediately it will wake up almost immediately whether it is wait(1000) or wait(5000).
If you wait for the time between now and the timeout, you mandate that if the value is not in the hashmap immediately, the program will sleep for ~5 seconds no matter what. "the key was very likely to be there within the first second" is the entire rebuttal to their suggestion. You don't need anything else, why would you sleep for 5 seconds if you can only sleep for 1? Do you not care about runtime performance? Do you have no respect for the user who has to wait for the output?