arrays of HashMaps

Java thinks

Why does Java throw up its hands when you mix generics and arrays?
What are you supposed to do instead?  Are they imagining you will have
a differ type in each slot?


private HashSet[ numberOfHashSets ];
--
Roedy Green Canadian Mind Products http://mindprod.com
Computers are like Old Testament gods; lots of rules and no mercy.
~ Joseph Campbell (born: 1904-03-26 died: 1987-10-31 at age: 83)

3 comments
Why does Java throw up its hands when you mix generics and arrays?

Because in Java, arrays are covariant but generics are not. Array covariance has been present since Java 1.0. When generics were added in 1.5, they were made invariant since generics were implemented to ensure type safety at compile time.

Are they imagining you will have a differ type in each slot?

Given that arrays are covariant, that is a possibility as long as the type put into the slot inherits from the type the array represents. For example, putting a String and a Date in an Object[] array, or a String and a StringBuilder in a CharSequence[] array. If the type put into the array is not in the type hierarchy of the array, for example putting a Date into a String[] array, an ArrayStoreException is thrown. This is a Runtime exception. So the issue is not caught until Runtime. With Generics, it would be caught at compile time.

What are you supposed to do instead?  

Use a List<Map<KeyType, ValueType>> backed by an ArrayList of HashMaps.

0

On Tue, 29 Apr 2014 02:00:18 MSK, Mark Vedder <no_reply@jetbrains.com>
wrote, quoted or indirectly quoted someone who said :

>

Why does Java throw up its hands when you mix generics and arrays?

>Because in Java, arrays are http://en.wikipedia.org/wiki/Covariance_and_contravariance_%28computer_science%29 but generics are not. Array covariance has been present since Java 1.0. When generics were added in 1.5, they were made invariant since generics were implemented to ensure type safety at compile time.
>
>

Are they imagining you will have a differ type in each slot?

>Given that arrays are covariant, that is a possibility as long as the type put into the slot inherits from the type the array represents. For example, putting a String and a Date in an Object[] array, or a String and a StringBuilder in a CharSequence[] array. If the type put into the array is not in the type hierarchy of the array, for example putting a Date into a String[] array, an http://docs.oracle.com/javase/8/docs/api/index.html?java/lang/ArrayStoreException.html is thrown. This is a Runtime exception. So the issue is not caught until Runtime. With Generics, it would be caught at compile time.
>
>

What are you supposed to do instead?  
>

>Use a List

I have incorporated your answer into the entry in the Java glossary at
http://mindprod.com/jgloss/generics.html#ARRAYS

I think you meant Map, rather than Set.


--
Roedy Green Canadian Mind Products http://mindprod.com
Computers are like Old Testament gods; lots of rules and no mercy.
~ Joseph Campbell (born: 1904-03-26 died: 1987-10-31 at age: 83)

0

I think you meant Map, rather than Set.

It looks like I intermixed Sets -- because your example code snippet used HashSet: private HashSet[ numberOfHashSets ]; -- and Maps as that was the thread title had.  I've edited my post to correct the issue.

0

Please sign in to leave a comment.