Donal.Lynch.Msc
Donal.Lynch.Msc

Reputation: 3605

Java array of Hashtables

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

Upvotes: 5

Views: 14336

Answers (8)

user4407884
user4407884

Reputation: 1

For fixed size array:

Hashtable<String,Double>[] h1 = new Hashtable[]{new Hashtable< String,Double>(),new Hashtable< String,Double>(),new Hashtable< String,Double>()};

Upvotes: 0

Carl Manaster
Carl Manaster

Reputation: 40326

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.

Upvotes: 1

omerkudat
omerkudat

Reputation: 10051

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.

Upvotes: 0

McDowell
McDowell

Reputation: 108859

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.

Upvotes: 0

Dan
Dan

Reputation: 1161

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.

Upvotes: 3

Fred Foo
Fred Foo

Reputation: 363477

... 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>>();

Upvotes: 5

GuruKulki
GuruKulki

Reputation: 26418

you can create like this.

Hashtable<String,Double>[] arr = new Hashtable[10];

Upvotes: 2

Bala R
Bala R

Reputation: 108937

just use

    @SuppressWarnings("unchecked")
    Hashtable<String,Double>[] h = (Hashtable<String,Double>[])new Hashtable<?,?>[10];
    h[0] = new Hashtable<String, Double>();

Upvotes: 4

Related Questions