Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / July 2008

Tip: Looking for answers? Try searching our database.

How to create array of hash table with correct types

Thread view: 
jonbbbb - 04 Jul 2008 00:38 GMT
Hi,

I want to create an array of 3 hash tables that has a string key and a
NodeAddress value.

I tried the following, which does not compile:

private Hashtable<String, NodeAddress>[] cache = {new
Hashtable<String, NodeAddress>(),new Hashtable<String,
NodeAddress>()};

private Hashtable<String, NodeAddress>[] cache = new Hashtable<String,
NodeAddress>()[3];

Any help on the syntax to get it to work would be great.

Thanks.
Jon.
Roedy Green - 04 Jul 2008 01:15 GMT
>I tried the following, which does not compile:
>
[quoted text clipped - 4 lines]
>private Hashtable<String, NodeAddress>[] cache = new Hashtable<String,
>NodeAddress>()[3];

Create the array slots  in one step, then initialise the slots in a
statement each.
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

jonbbbb - 04 Jul 2008 02:07 GMT
> Create the array slots  in one step, then initialise the slots in a
> statement each.

Thanks for your reply. I am still not sure I get it.

Hashtable<String, NodeAddress>[] cache = new Hashtable;
cache[0] = new Hashtable<String, NodeAddress>();
cache[1] = new Hashtable<String, NodeAddress>();
cache[2] = new Hashtable<String, NodeAddress>();

This compiles with warnings since the type of the type of the
Hashtable array is not set correctly, "Uses unsafe operations...".

My experience with the type stuff in Java is a bit rusty. Sorry.

Replacing in the above code:
Hashtable<String, NodeAddress>[] cache = new Hashtable;
with:
Hashtable<String, NodeAddress>[] cache = new Hashtable<String,
NodeAddress>[3];
blows up with "generic array creation"

How can I do it properly?

Jon.
Roedy Green - 04 Jul 2008 03:10 GMT
>How can I do it properly?

Java is simply buggered when it comes to mixing arrays and generics.
Even inside Sun's collection classes they have to cheat with code that
generates warnings.

I ran into a similar problem with this code:

Map.Entry<String, Integer>[] keyValuePairs =
                   justEntries.toArray( new
Map.Entry[justEntries.size()] );

           // Type erasure won't let us say:
           // Map.Entry<String, Integer>[] keyValuePairs =
           // justEntries.toArray ( new
           //  Map.Entry<String,Integer>[justEntries.size()] );
           

Just close your eyes and gag it down.

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Lew - 04 Jul 2008 04:02 GMT
> Hashtable<String, NodeAddress>[] cache = new Hashtable;
> ...
> This compiles with warnings since the type of the type of the
> Hashtable array is not set correctly, "Uses unsafe operations...".

I doubt it.  The expression 'new Hashtable;' should have choked with an error,
not a warning.

Once you fix that, you'll get the warnings you describe.  Those are normal and
acdepted whenever you succumb to the temptation to use raw types.  You'd be
better off avoiding raw types and sticking with generics.

Signature

Lew

Roedy Green - 04 Jul 2008 05:45 GMT
>Hashtable<String, NodeAddress>[] cache = new Hashtable;

other than the problems mixing Generics and arrays you are missing
[3].
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Lew - 04 Jul 2008 03:59 GMT
>> I tried the following, which does not compile:
>>
>> private Hashtable<String, NodeAddress>[] cache = {new
>> Hashtable<String, NodeAddress>(),new Hashtable<String,
>> NodeAddress>()};

How about actually *citing* the error message?

> ~/projects/testit/src/testit/Foo.java:22: generic array creation

I guess that means you can't create a generic array.

Could that be?  Could the error message actually have already answered your
question?

Let's research:
<http://java.sun.com/docs/books/jls/third_edition/html/arrays.html#10.3>
> It is a compile-time error if the element type is not a reifiable type (§4.7)

<http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.7>
> A type is reifiable if and only if one of the following holds:
>
[quoted text clipped - 4 lines]
> * It is a primitive type (§4.2).
> * It is an array type (§10.1) whose component type is reifiable.

So the answer is, no, you cannot do that.

Anyway, arrays and generics don't play well together.  Make a List of Maps
instead.

Additionally, I suggest using HashMap instead of Hashtable, and declaring the
variable in terms of List <Map> rather than a concrete type.

 private List <Map <String, Foo>> cache
       = new ArrayList <Map <String, Foo>> ();
 {
   cache.add( new HashMap <String, Foo> ());
   cache.add( new HashMap <String, Foo> ());
 }

Signature

Lew

Roedy Green - 04 Jul 2008 05:46 GMT
>How about actually *citing* the error message?

http://mindprod.com/jgloss/compileerrormessages.html#GENERICARRAY
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Lew - 04 Jul 2008 05:53 GMT
Lew wrote:
>> How about actually *citing* the error message?

> http://mindprod.com/jgloss/compileerrormessages.html#GENERICARRAY

Yep.

My point to the OP was that it's useful actually to *cite* the error message,
not just say, "... does not compile".  The second point to the OP was that the
error message *actually describes the problem*, albeit tersely.  By citing the
error message, I was able to show how it led to research in the JLS, which in
turn led to the complete answer.

So, OP and others who want to discuss an error message on these forums:  How
about actually *citing* the error message?

Signature

Lew

Roedy Green - 04 Jul 2008 06:36 GMT
>So, OP and others who want to discuss an error message on these forums:  How
>about actually *citing* the error message?

One further advantage is if you have the precise wording of the error,
you can look it up at
http://mindprod.com/jgloss/compileerrormessages.html
or
http://mindprod.com/jgloss/runerrormessages.html

By browsing the page and using your browser find feature.
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

jonbbbb - 04 Jul 2008 10:16 GMT
> >> I tried the following, which does not compile:
>
[quoted text clipped - 41 lines]
>     cache.add( new HashMap <String, Foo> ());
>   }

Thank you very much for the information.

I will try your code.

Sorry for not being more explicit in citing the error message.

Jon.
Roedy Green - 04 Jul 2008 20:04 GMT
>Anyway, arrays and generics don't play well together.  Make a List of Maps
>instead.

At least his sweeps the kludge under the rug inside ArrayList.  This
creates the cleanest code.  In theory, you could use the same
bailingwire that ArrayList does.
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Daniel Pitts - 06 Jul 2008 01:19 GMT
> Hi,
>
[quoted text clipped - 14 lines]
> Thanks.
> Jon.
Effective Java 2nd Ed., Item 25: Prefer Lists to arrays.

Don't use Hashtable, use HashMap

private List<Map<String, NodeAddress>> cache = new
ArrayList<Map<String,NodeAddress>>();

public MyClass() {
  for (int i = 0; i < 3; ++i) {
    cache.add(new HashMap<String, NodeAddress>());
  }
}

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.