5

I need an array of Hashtables in a program that is storing all words from a given set of documents.

Index 1 of the array holds a hashtable of String -> Double which stores a word, and its count for document 1 (array index 100 = document number 100's hashtable).

I dont need help using this data structure, just in creating it. I declare the Hashtable Array as follows:

Hashtable<String,Double>[] h1 = new Hashtable<String,Double>[];

... but this does not compile.

(NOTE: The Double is necessary rather than an Integer in the above declaration for later usage.)

QUESTION: How do you create an array of hashtables which stores String->Double ???

Any suggestions appreciated guys....

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Donal.Lynch.Msc
  • 3,365
  • 12
  • 48
  • 78

8 Answers8

5

... but this does not compile.

That's because the array has no name, new expects a number of elements and you can't just allocate an array of generics. Prefer a List instead:

List<Hashtable<String,Double>> wordCountPerDoc
  = new ArrayList<Hashtable<String,Double>>();
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
4

just use

    @SuppressWarnings("unchecked")
    Hashtable<String,Double>[] h = (Hashtable<String,Double>[])new Hashtable<?,?>[10];
    h[0] = new Hashtable<String, Double>();
Bala R
  • 107,317
  • 23
  • 199
  • 210
3

why don't you use a Map<Integer, Map<String, Double> > ? this way you don't waste space for non-existing documents, and still get O(1) retrieval.

Dan
  • 1,163
  • 14
  • 34
2

you can create like this.

Hashtable<String,Double>[] arr = new Hashtable[10];
GuruKulki
  • 25,776
  • 50
  • 140
  • 201
1

Two things: you can't declare an array with the parameterized types like that; you have to imply declare it a new Hashtable[]. And you need to give the array a length.

Mixing arrays and Collections, although possible, tends to be confusing and lead to problems in my experience; also HashMap is generally preferred to Hashtable. So I would tend to prefer a List<Map<String, Double>> for this application.

Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
0

For fixed size array:

Hashtable<String,Double>[] h1 = new Hashtable[]{new Hashtable< String,Double>(),new Hashtable< String,Double>(),new Hashtable< String,Double>()};
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
0

The reasons why this is an error are covered in Angelika Langer's Generics FAQ: Can I create an array whose component type is a concrete parameterized type?

Can I create an array whose component type is a concrete parameterized type?

No, because it is not type-safe.

Arrays are covariant, which means that an array of supertype references is a supertype of an array of subtype references. That is, Object[] is a supertype of String[] and a string array can be accessed through a reference variable of type Object[].

Arrays and generics can have odd interactions (largely due to implementation compromises to support compatibility). You may be better off (as larsmans suggested) looking at a suitable collection type such as a List of Maps.

Community
  • 1
  • 1
McDowell
  • 107,573
  • 31
  • 204
  • 267
0

An array seems to be an unusual choice of structure here. Perhaps you should consider storing your hashtables in a List. It will dynamically resize for you if you don't know how many document you will have ahead of time. If you use an ArrayList, you will still have constant-time reads of random indeces (like an array.) I think it's much simpler than using an array, and you still get the generic type checking. If you choose a List, you syntax becomes:

List<Map<String,Double>> documentWordCounts = new ArrayList<Map<String,Double>>();

Or choose a LinkedList depending on what kind of read/write pattern you want.

omerkudat
  • 9,371
  • 4
  • 33
  • 42