1

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.

sammy333
  • 1,384
  • 6
  • 21
  • 39
  • "I want to operate over array of integers, and I can solve the problem using hashtable" - **wut?** Question: Why are you not just using an array?? (`Integer[] arr = new Integer[capacity];`) – Vogel612 Sep 05 '14 at 09:35
  • 6
    If you need only keys and not values then you're using the wrong data structure. You need a Set instead of a Hashtable. – janos Sep 05 '14 at 09:55
  • If you do not worry about values then Use `HashSet` where you can store the objects – Shirishkumar Bari Sep 05 '14 at 10:00
  • 1
    `Hashtable ht = new Hashtable<>();ht.put(arri[],null);` – kai Sep 05 '14 at 10:46
  • @Vogel612, because I need O(1) access to elements by value, not by their index – sammy333 Sep 05 '14 at 12:20

2 Answers2

2

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)) {
    ...
}
Community
  • 1
  • 1
Marco13
  • 53,703
  • 9
  • 80
  • 159
1

`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.

rolfl
  • 17,539
  • 7
  • 42
  • 76