简体   繁体   English

如何做一个哈希图数组?

[英]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[ ]类型安全:HashMap[] 类型的表达式需要未经检查的转换以符合 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<K, V>>代替。

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: Java 语言规范第 15.10 节指出:

An array creation expression creates an object that is a new array whose elements are of the type specified by the PrimitiveType or ClassOrInterfaceType.数组创建表达式创建一个对象,该对象是一个新数组,其元素属于 PrimitiveType 或 ClassOrInterfaceType 指定的类型。 It is a compile-time error if the ClassOrInterfaceType does not denote a reifiable type (§4.7).如果 ClassOrInterfaceType 不表示可具体化的类型(第 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. Java 不希望您创建一个 HashMap 数组,但它可以让您创建一个对象数组。 So, just write up a class declaration as a shell around your HashMap, and make an array of that class.因此,只需编写一个类声明作为 HashMap 的外壳,并创建该类的数组。 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.如果您愿意,这可以让您存储一些关于 HashMap 的额外数据——这可能是一个好处,因为您已经有了一个有点复杂的数据结构。

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.改用List

Regarding the @alchemist's answer, I added some modifications using only HashMap and ArrayList:关于@alchemist 的回答,我仅使用 HashMap 和 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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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