简体   繁体   中英

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

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 .

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

}


}

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 :-)

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;

...

}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM