如何在 Java 中实例化一组地图? [英] How does one instantiate an array of maps in Java?

查看:23
本文介绍了如何在 Java 中实例化一组地图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用泛型声明一个地图数组来指定地图类型:

I can declare an array of maps using generics to specify the map type:

private Map<String, Integer>[] myMaps;

但是,我不知道如何正确实例化它:

However, I can't figure out how to instantiate it properly:

myMaps = new HashMap<String, Integer>[count]; // gives "generic array creation" error
myMaps = new HashMap[count]; // gives an "unchecked or unsafe operation" warning
myMaps = (Map<String, Integer>[])new HashMap[count]; // also gives warning

如何在不出现编译器错误或警告的情况下实例化这个映射数组?

How can I instantiate this array of maps without getting a compiler error or warning?

更新:

谢谢大家的回复.我最终选择了 List 建议.

Thank you all for your replies. I ended up going with the List suggestion.

推荐答案

您无法安全地创建通用数组.Effective Java 2nd Edition 在泛型一章中有详细介绍.从第 119 页的最后一段开始:

You can't safely create a generic array. Effective Java 2nd Edition goes into the details in the chapter on Generics. Start at the last paragraph of page 119:

为什么创建泛型是非法的大批?因为它不是类型安全的.如果这是合法的,由编译器以其他方式正确程序可能会在运行时失败ClassCastException.这会违反提供的根本保证泛型类型系统.

Why is it illegal to create a generic array? Because it isn’t typesafe. If it were legal, casts generated by the compiler in an otherwise correct program could fail at runtime with a ClassCastException. This would violate the fundamental guarantee provided by the generic type system.

为了使这更具体,请考虑以下代码片段:

To make this more concrete, consider the following code fragment:

// Why generic array creation is illegal - won't compile!
List<String>[] stringLists = new List<String>[1]; // (1)
List<Integer> intList = Arrays.asList(42); // (2)
Object[] objects = stringLists; // (3)
objects[0] = intList; // (4)
String s = stringLists[0].get(0); // (5)

让我们假设第 1 行,其中创建一个通用数组,是合法的.第 2 行创建并初始化一个List 包含单个元素.第 3 行存储List 数组到Object数组变量,这是合法的,因为数组是协变的.4号线门店List 到唯一Object 数组的元素,其中成功是因为泛型是通过擦除实现:运行时List 实例的类型是简单地List,以及一个的运行时类型List[] 实例是 List[],所以此作业不会生成ArrayStoreException.现在我们在麻烦.我们存储了一个 List实例化为一个数组声明只保存 List实例.在第 5 行,我们检索中唯一列表中的唯一元素这个数组.编译器自动将检索到的元素转换为 String,但它是一个 Integer,所以我们得到一个ClassCastException 在运行时.在为了防止这种情况发生,第 1 行(创建一个通用数组)生成编译时错误.

Let’s pretend that line 1, which creates a generic array, is legal. Line 2 creates and initializes a List<Integer> containing a single element. Line 3 stores the List<String> array into an Object array variable, which is legal because arrays are covariant. Line 4 stores the List<Integer> into the sole element of the Object array, which succeeds because generics are implemented by erasure: the runtime type of a List<Integer> instance is simply List, and the runtime type of a List<String>[] instance is List[], so this assignment doesn’t generate an ArrayStoreException. Now we’re in trouble. We’ve stored a List<Integer> instance into an array that is declared to hold only List<String> instances. In line 5, we retrieve the sole element from the sole list in this array. The compiler automatically casts the retrieved element to String, but it’s an Integer, so we get a ClassCastException at runtime. In order to prevent this from happening, line 1 (which creates a generic array) generates a compile-time error.

因为数组和泛型不能很好地结合(以及其他原因),通常最好使用 Collection 对象(特别是 List 对象)而不是数组.

Because arrays and generics don't combine well (as well as other reasons), it's generally better to use Collection objects (in particular List objects) rather than arrays.

这篇关于如何在 Java 中实例化一组地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆