Java 如何做一个哈希图数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2792731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 12:52:49  来源:igfitidea点击:

How to do an array of hashmaps?

javaarrayshashmap

提问by Joren

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

采纳答案by BalusC

What gives? It works. Just ignore it:

是什么赋予了?有用。直接忽略它:

@SuppressWarnings("unchecked")

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

不,你不能参数化它。但是,我宁愿使用 aList<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.

要了解有关集合和地图的更多信息,请查看本教程

回答by Tom

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

您不能拥有泛型类型的数组。使用List来代替。

回答by meriton

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. It is a compile-time error if the ClassOrInterfaceType does not denote a reifiable type (§4.7).

数组创建表达式创建一个对象,该对象是一个新数组,其元素属于 PrimitiveType 或 ClassOrInterfaceType 指定的类型。如果 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 :-)

你的版本显然更好:-)

回答by alchemist

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

}


}

回答by Brian Peterson

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.

Java 不希望您创建一个 HashMap 数组,但它可以让您创建一个对象数组。因此,只需编写一个类声明作为 HashMap 的外壳,并创建该类的数组。如果您愿意,这可以让您存储一些关于 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;

...

}