Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java array of Hashtables

Tags:

java

hashtable

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

like image 713
Donal.Lynch.Msc Avatar asked Mar 27 '11 14:03

Donal.Lynch.Msc


3 Answers

... 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>>();
like image 114
Fred Foo Avatar answered Oct 17 '22 14:10

Fred Foo


just use

    @SuppressWarnings("unchecked")
    Hashtable<String,Double>[] h = (Hashtable<String,Double>[])new Hashtable<?,?>[10];
    h[0] = new Hashtable<String, Double>();
like image 44
Bala R Avatar answered Oct 17 '22 14:10

Bala R


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.

like image 29
Dan Avatar answered Oct 17 '22 15:10

Dan