Best collection to use with type Double as key

后端 未结 4 772
你的背包
你的背包 2020-12-21 14:33

I have this object:

Public Cactus{
    Public Double key;
    Public String value;
}

I have about ~100 Cactus, which all have

4条回答
  •  暖寄归人
    2020-12-21 15:24

    Yes, using a double value as a key seems awkward. Are those double values results of some computations that may have some very tiny round-off errors? If you are going to compute those values and access the collection with those computed values, it is highly likely that there is going to be unwanted results.

    For example, you store an element with key value 1.01. Your computations would result 1.010000000000000123, and will not match the stored key.

    If this is not the case, then I see no problem using a double value as the key of a dictionary, or hashtable collection.

    BTW, using a typed Dictionary (i.e., Dictionary) will be easier to use than a HashTable.

提交回复
热议问题