Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
466 views
in Technique[技术] by (71.8m points)

c# - Best collection to use with type Double as key

I have this object:

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

I have about ~100 Cactus, which all have a unique key, and a value that has some duplicates. I will however have to retrieve the value for a key about ~2000 times. My Key values varies between -10 and 280.

I want this process to be as quick as possible. What would be the best approach for this? I was thinking using a HashTable, although I always used one with Integer and not Double, and for some reason I am worried that it's not good practice to use Double as Key.

Given my scenario, which collection would be the best to use?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using a double as a key in a dictionary is generally a really bad idea. You will run into all sorts of problems with representation errors where you think you stored 0.1 in the dictionary but actually you stored something very close to but not quite equal to 0.1.

I'd suggest using another type (string?) if you need exact matches.

If you don't want exact matches but just finding the closest value, consider something like a SortedList instead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...