0

I am just going through my notes where I have Doubles as HashMap keys are bad that I vaguely remember using it for some online coding platform.

Are there any use cases and alternatives in dealing with Double as Keys which you have dealt with in real world or interviews or coding problems?

I was thinking of the following, in case of an interview I can argue to use this and make my argument this is the best we can do.?

MyDouble(){
   private final double original;
   private final long value;

   public MyDouble(double original) {
      this.original = original;
      this.value = Double.doubleToLongBits(original);
   }
}
Dexters
  • 2,419
  • 6
  • 37
  • 57
  • 1
    You can do this as long as you implement your own wrapper that implements `equals()` and `hashCode()` in a manner that satisfies your requirements. Internal conversion to big decimal or string with a fixed precision is one possibility. This is not a hard problem if you clearly understand the limitations and the tradeoffs in how you program around them. – Jim Garrison Mar 31 '18 at 06:15
  • great. make sense i can work around the equals issue based on the problem statement. – Dexters Mar 31 '18 at 15:03

0 Answers0