| Thread Closed |
How do you use Java's Hashtable? |
Share Thread | Thread Tools |
| Oct14-06, 05:05 PM | #18 |
|
|
How do you use Java's Hashtable? |
| Oct14-06, 05:17 PM | #19 |
|
Recognitions:
|
Yeah, I guess. It's kind of annoying that they have a containsValue() method in the first place when there's no efficient implementation behind it! It should be marked "deprecated."
|
| Oct14-06, 05:36 PM | #20 |
|
|
It makes sense from both a consistency and convenience viewpoint: every other container class (even ArrayList, for example) implements a contains(Object) method. And searching for a value is something one would sometimes want to do. Map.values().contains(Object) is awkward syntax, and probably execlutes more slowly for most Map types. Some Map classes might even have efficient value lookups, and it would be a shame to force the user to break the container abstraction to access such a useful method.
|
| Oct14-06, 05:55 PM | #21 |
|
Recognitions:
|
Well, if someone needs a hash table then chances are they have thousands of values and they want whatever they are doing to be fast. A linear search in that situation is not practical. values().contains() at least makes the programmer aware of how inefficient it is--it's awkward and should be awkward. In terms of consistency, HashMap does have a containsKey() method, and it does not have a plain contains() method anyway (and shouldn't because it would be ambiguous). One contains-like method would be enough. In the API there isn't even a warning about the time complexity of containsValue() and the documentation is fairly vague about what's actually going on there. As you say, there could be alternate implementations that do have fast lookup for values. It should mention that this is not one of them. Deprecating this particular method in this particular class would be sensible--it is a good idea to deprecate linear time access on a likely large set, or at least mention it in the API.
|
| Thread Closed |
| Thread Tools | |
Similar Threads for: How do you use Java's Hashtable?
|
||||
| Thread | Forum | Replies | ||
| Hashtable for booleans? | Programming & Comp Sci | 18 | ||
| Java's "Scanner" | Computing & Technology | 6 | ||