Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do an array of hashmaps?

This is what I tried to do, but it gives me a warning:

HashMap<String, String>[] responseArray = new HashMap[games.size()]; 

Type safety: The expression of type HashMap[ ] needs unchecked conversion to conform to HashMap[ ]

like image 403
Joren Avatar asked May 08 '10 02:05

Joren


People also ask

Can we create an array of HashMap?

You cannot, for example, create an array of a concrete parametrized type with the new operator. The issue relates to the runtime system being unable to guarantee type safety. Arrays are covariant and reified. Generics are invariant and non-reified.

Can you have an ArrayList of Hashmaps?

Conversion of HashMap to ArrayList A HashMap contains key-value pairs, there are three ways to convert a HashMap to an ArrayList: Converting the HashMap keys into an ArrayList. Converting the HashMap values into an ArrayList. Converting the HashMap key-value pairs into an ArrayList.

Do Hashmaps use arrays?

Internally, the HashMap uses an Array, and it maps the labels to array indexes using a hash function. There are at least two ways to implement hashmap: Array: Using a hash function to map a key to the array index value.


1 Answers

What gives? It works. Just ignore it:

@SuppressWarnings("unchecked") 

No, you cannot parameterize it. I'd however rather use a List<Map<K, V>> instead.

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

To learn more about collections and maps, have a look at this tutorial.

like image 128
BalusC Avatar answered Sep 28 '22 22:09

BalusC