How to do an array of hashmaps?

JavaArraysHashmap

Java Problem Overview


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[ ]

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

You can use something like this:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class testHashes {

public static void main(String args[]){
	Map<String,String> myMap1 = new HashMap<String, String>();
	
	List<Map<String , String>> myMap  = new ArrayList<Map<String,String>>();
	
	myMap1.put("URL", "Val0");
	myMap1.put("CRC", "Vla1");
	myMap1.put("SIZE", "Val2");
	myMap1.put("PROGRESS", "Val3");
	
	myMap.add(0,myMap1);
	myMap.add(1,myMap1);
	
	for (Map<String, String> map : myMap) {
		System.out.println(map.get("URL"));
        System.out.println(map.get("CRC"));
        System.out.println(map.get("SIZE"));
        System.out.println(map.get("PROGRESS"));
	}
	
	//System.out.println(myMap);
	
}


}

Solution 3 - Java

The Java Language Specification, section 15.10, states:

> An array creation expression creates > an object that is a new array whose > elements are of the type specified by > the PrimitiveType or > ClassOrInterfaceType. It is a > compile-time error if the > ClassOrInterfaceType does not denote a > reifiable type (§4.7).

and

> The rules above imply that the element > type in an array creation expression > cannot be a parameterized type, other > than an unbounded wildcard.

The closest you can do is use an unchecked cast, either from the raw type, as you have done, or from an unbounded wildcard:

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

Your version is clearly better :-)

Solution 4 - Java

You can't have an array of a generic type. Use List instead.

Solution 5 - Java

Java doesn't want you to make an array of HashMaps, but it will let you make an array of Objects. So, just write up a class declaration as a shell around your HashMap, and make an array of that class. This lets you store some extra data about the HashMaps if you so choose--which can be a benefit, given that you already have a somewhat complex data structure.

What this looks like:

private static someClass[] arr = new someClass[someNum];

and

public class someClass {

private static int dataFoo;
private static int dataBar;
private static HashMap<String, String> yourArray;

...

}

Solution 6 - Java

Regarding the @alchemist's answer, I added some modifications using only HashMap and ArrayList:

import java.util.ArrayList;
import java.util.HashMap;
public class ArrayOfHash {

public static void main(String[] args) {
	HashMap<String,String> myMap = new HashMap<String, String>();

	ArrayList<HashMap<String , String>> myArrayMap = new ArrayList<HashMap<String,String>>();

	myMap.put("Key1",	"Val0");
	myMap.put("Key2",	"Val1");
	myMap.put("Key3",	"Val2");
	myMap.put("Key4",	"Val3");

    myArrayMap.add(myMap);
    myArrayMap.add(myMap);

    for (int i = 0; i < myArrayMap.size(); i++) {
    	System.out.println(myArrayMap.get(i).get("Key1") + ","
    		+ "" + myArrayMap.get(i).get("Key2") + ","
    		+ "" + myArrayMap.get(i).get("Key3") + ","
    		+ "" + myArrayMap.get(i).get("Key4"));
    	
    	System.out.println(); // used as new blank line
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJorenView Question on Stackoverflow
Solution 1 - JavaBalusCView Answer on Stackoverflow
Solution 2 - JavaalchemistView Answer on Stackoverflow
Solution 3 - JavameritonView Answer on Stackoverflow
Solution 4 - JavaTomView Answer on Stackoverflow
Solution 5 - JavaBrian PetersonView Answer on Stackoverflow
Solution 6 - JavaVíctor Arango RamiroView Answer on Stackoverflow