简体   繁体   中英

How should I create Hashtable<Key, Value> when I do not care what is stored in Value?

I want to operate over array of integers, and I can solve the problem using hashtable . I want to store the ellements of the array as Key but for Value I really don't care what will be stored there. In such case how should I create my hashtable? My approach is just to set value as 0 for each element like this:

    Hashtable<Integer, Integer> ht = new Hashtable<Integer, Integer>();
    for (int i = 0; i < arr.length; i++) {
        ht.put(arr[i], 0);
    }

Is there any convention what should I do in such cases? For example, is it better to set Value as Boolean and respectively true for all inserted elements? Once again, for me it really doesn't matter what will be stored in Value . I just want to know what is the best approach in such cases.

As already pointed out by rolfl, a Hashtable is a data structure that existed already before the Collections Framework was introduced. It might be considered as an alternative to a Collections.synchronizedMap , but the latter gives some additional flexibility.

So the general recommendation can be to not use a Hashtable in new code, because there are better options now.


Most importantly, regardless of whether you use a Hashtable or not: You should never know that you're using it. Particularly, you should never declare your variable to be a Hashtable , but always to be a Map :

// Don't do this!
Hashtable<Integer, Integer> ht = new Hashtable<Integer, Integer>();

// Do this instead (if you don't need synchronized access)
Map<Integer, Integer> ht = new HashMap<Integer, Integer>();

// Or this, if you need synchronized access:
Map<Integer, Integer> ht = 
    Collections.synchronizedMap(new HashMap<Integer, Integer>());

Also see What does it mean to “program to an interface”?


That being said: It seems like you don't need a Map or Hashtable at all. From the description, it sounds like you just want to know whether a particular key is contained in the map. This could be modeled in different ways. But most likely, this means that you should use a Set instead of a Map

Set<Integer> set = new HashSet<Integer>();
for (int i = 0; i < arr.length; i++) {
    set.add(arr[i]);
}

// To check whether an integer is contained in the set:
if (set.contains(42)) {
    ...
}

`Hashtable is a horrible data structure to use, even when you want to store a real value. You should be using a HashMap instead of a Hashtable, unless you know exactly why a Hashtable is good for your situation.

Hashtable is a synchronized class, it does not allow null values, and is generally slower and less usable than Hashmap.

So, never use Hashtable when a HashMap will do.

Regardless, for your use case, you likely want to be using a HashSet , and just store the values in there. This will give you what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM