60

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

CaptJak
  • 3,592
  • 1
  • 29
  • 50
Joren
  • 9,623
  • 19
  • 63
  • 104

6 Answers6

72

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.

Vili
  • 1,599
  • 15
  • 40
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 2
    Thanks. I got it working using a list of maps. I don't like ignoring warnings. In my experience if you're getting a warning, ur doin' it rong. The only thing I don't understand is why I declare it as a type Map, but when I actually instantiate it I have to use HashMap? Map responseMap; responseMap = new HashMap(); – Joren May 08 '10 at 03:59
  • 1
    It's an interface. A blueprint. A contract. Follow the link :) – BalusC May 08 '10 at 04:02
  • Why do would you rather use List of Maps then a HashMap, please explain. – Daniel Jul 11 '12 at 14:06
  • 2
    Java gemerics and arrays mix poorly. 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. The best solution is to use a List in this case. – scottb Aug 17 '13 at 04:09
  • I didnt follow still why generics are allowed in case of ArrayList but not arrays ? – Diffy Jun 21 '14 at 19:57
16

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

}


}
alchemist
  • 1,081
  • 12
  • 17
9

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

meriton
  • 68,356
  • 14
  • 108
  • 175
4

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;

...

}
Brian Peterson
  • 2,800
  • 6
  • 29
  • 36
4

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

Tom
  • 21,468
  • 6
  • 39
  • 44
  • Actually, you can *have* an array of a generic type, you just can't *create* one directly. (I don't presume to understand the rationale behind this, but that's what the spec says) – meriton May 08 '10 at 02:54
1

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