哈希表的 Java 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5449804/
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-10-30 11:11:08  来源:igfitidea点击:

Java array of Hashtables

javahashtable

提问by Donal.Lynch.Msc

I need an array of Hashtables in a program that is storing all words from a given set of documents.

我需要一个程序中的哈希表数组,该程序存储给定文档集中的所有单词。

Index 1 of the array holds a hashtable of String -> Double which stores a word, and its count for document 1 (array index 100 = document number 100's hashtable).

数组的索引 1 包含一个 String -> Double 的哈希表,它存储一个单词,以及它对文档 1 的计数(数组索引 100 = 文档编号 100 的哈希表)。

I dont need help using this data structure, just in creating it. I declare the Hashtable Array as follows:

我不需要帮助使用这个数据结构,只是在创建它。我声明哈希表数组如下:

Hashtable<String,Double>[] h1 = new Hashtable<String,Double>[];

... but this does not compile.

...但这不能编译。

(NOTE: The Double is necessary rather than an Integer in the above declaration for later usage.)

(注意:上面声明中的 Double 是必需的,而不是 Integer 以供以后使用。)

QUESTION: How do you create an array of hashtables which stores String->Double ???

问题:如何创建存储 String->Double 的哈希表数组?

Any suggestions appreciated guys....

任何建议赞赏家伙....

回答by Fred Foo

... but this does not compile.

...但这不能编译。

That's because the array has no name, newexpects a number of elements and you can't just allocate an array of generics. Prefer a Listinstead:

那是因为该数组没有名称,new需要多个元素,而您不能只分配一个泛型数组。更喜欢一个List

List<Hashtable<String,Double>> wordCountPerDoc
  = new ArrayList<Hashtable<String,Double>>();

回答by Bala R

just use

只是使用

    @SuppressWarnings("unchecked")
    Hashtable<String,Double>[] h = (Hashtable<String,Double>[])new Hashtable<?,?>[10];
    h[0] = new Hashtable<String, Double>();

回答by Dan

why don't you use a Map<Integer, Map<String, Double> >? this way you don't waste space for non-existing documents, and still get O(1) retrieval.

你为什么不使用一个Map<Integer, Map<String, Double> >?这样您就不会为不存在的文档浪费空间,并且仍然可以获得 O(1) 检索。

回答by GuruKulki

you can create like this.

你可以这样创建。

Hashtable<String,Double>[] arr = new Hashtable[10];

回答by Carl Manaster

Two things: you can't declare an array with the parameterized types like that; you have to imply declare it a new Hashtable[]. And you need to give the array a length.

两件事:你不能用这样的参数化类型声明一个数组;您必须暗示将其声明为new Hashtable[]. 你需要给数组一个长度。

Mixing arrays and Collections, although possible, tends to be confusing and lead to problems in my experience; also HashMap is generally preferred to Hashtable. So I would tend to prefer a List<Map<String, Double>>for this application.

混合使用数组和集合,虽然可能,但往往会令人困惑并导致我的经验出现问题;HashMap 通常也优于 Hashtable。所以我倾向于更喜欢List<Map<String, Double>>这个应用程序。

回答by McDowell

The reasons why this is an error are covered in Angelika Langer's Generics FAQ: Can I create an array whose component type is a concrete parameterized type?

Angelika Langer 的泛型常见问题解答中介绍了这是错误的原因:我可以创建一个组件类型为具体参数化类型的数组吗?

Can I create an array whose component type is a concrete parameterized type?

No, because it is not type-safe.

Arrays are covariant, which means that an array of supertype references is a supertype of an array of subtype references. That is, Object[]is a supertype of String[]and a string array can be accessed through a reference variable of type Object[].

我可以创建一个数组,其组件类型是具体的参数化类型吗?

不,因为它不是类型安全的。

数组是协变的,这意味着超类型引用数组是子类型引用数组的超类型。也就是说,Object[]是 的超类型,String[]并且可以通过类型为 的引用变量访问字符串数组Object[]

Arrays and generics can have odd interactions (largely due to implementation compromises to support compatibility). You may be better off (as larsmans suggested) looking at a suitable collection type such as a Listof Maps.

数组和泛型可能有奇怪的交互(主要是由于支持兼容性的实现妥协)。您可能会更好(如larsmans 建议)查看合适的集合类型,例如Listof Map

回答by omerkudat

An array seems to be an unusual choice of structure here. Perhaps you should consider storing your hashtables in a List. It will dynamically resize for you if you don't know how many document you will have ahead of time. If you use an ArrayList, you will still have constant-time reads of random indeces (like an array.) I think it's much simpler than using an array, and you still get the generic type checking. If you choose a List, you syntax becomes:

数组在这里似乎是一种不寻常的结构选择。也许您应该考虑将哈希表存储在列表中。如果您不知道提前有多少文档,它会为您动态调整大小。如果您使用ArrayList,您仍然会获得随机 indeces(如数组)的恒定时间读取。我认为它比使用数组简单得多,并且您仍然可以进行泛型类型检查。如果选择列表,则语法变为:

List<Map<String,Double>> documentWordCounts = new ArrayList<Map<String,Double>>();

Or choose a LinkedListdepending on what kind of read/write pattern you want.

或者LinkedList根据您想要的读/写模式选择一个。

回答by user4407884

For fixed size array:

对于固定大小的数组:

Hashtable<String,Double>[] h1 = new Hashtable[]{new Hashtable< String,Double>(),new Hashtable< String,Double>(),new Hashtable< String,Double>()};