简体   繁体   English

从Hashmap返回信息

[英]Returning information from a Hashmap

Basically what I'm trying to do is store two values into a hashmap, i've tried a dictionary and failed with that as-well, anyhow, here we go. 基本上,我想做的是将两个值存储到一个哈希图中,我尝试过一个字典,但无论如何还是失败了,无论如何,我们来了。

private HashMap<Integer, Integer> dropTable = new HashMap<Integer, Integer>();

Then, in my code I have this 然后,在我的代码中

    for(int npc = 0; npc < 10; npc++){
        dropTable.put(npc, Constants.itemDrops[npc][1]);
    }

Basically, what I'm trying to do is save the values in this manner (With the ItemID being what is returned in the itemDrops array 基本上,我想做的就是以这种方式保存值(其中ItemID为itemDrops数组中返回的值)

<ArrayIndex, ItemID>

How,ever when I try to return this information, i can't figure it out. 但是,无论何时尝试返回此信息,我都无法弄清楚。

Here is how I attempted returning this value 这是我尝试返回此值的方式

        for(int i = 0; i < dropTable.size(); i++) {
        System.out.println("NPC: " + dropTable.get((Integer)i));
    }

However, that returns null, and looking at it, it wont give me what I need. 但是,它返回null,并且看它不会给我我所需要的。

How would I go about retrieving the Key/Value separately from the HashMap based on the Index of the HashMap? 我将如何基于HashMap的索引与HashMap分开检索键/值? (If Hashmaps even have Index's, that's what I'm under the impression of) (如果哈希映射甚至具有索引,那就是我的印象)

=============== My idea of a hashmap. ===============我对哈希图的想法。

<Integer>, <Integer> Index: 0
<Integer>, <Integer> Index: 1
etc...

HashMaps are unsorted, do not have indexes and the order features are returned during iteration is not guaranteed to be the order of insertion. HashMap是未排序的,没有索引,并且不能保证迭代过程中返回的顺序特征是插入顺序。

There are other Map objects available which provide some of these features. 还有其他提供这些功能的Map对象。 TreeMap is sorted for example. 例如,对TreeMap进行排序。

All Maps provide a method keySet() which provides the set of Keys in an iterator and a similar values() Collection iterating over these objects is likely to provide the sort of behavour you wish. 所有Map都提供一个方法keySet(),该方法在迭代器中提供一组键,并且类似的values()集合在这些对象上进行迭代很可能会提供您希望的行为。

First thing to aks, is a hashmap realy the data structure you need? 首先,哈希表是否真的是您需要的数据结构?

If not not then just use a simple List and use its get(index) method! 如果不是这样,那么只需使用一个简单的List并使用其get(index)方法即可!

If Yes then you could use a Map implmentation - such as LinkedHashMap - that preserves the insert/put order. 如果是,则可以使用保留插入/放置顺序的Map实现 (例如LinkedHashMap) Then you can call values() of that map and iterate over it using enhanced for-loop, or probably also create a List and passing it the collection reurned if you need index access. 然后,您可以调用该映射的values()并使用增强的for循环对其进行迭代,或者可能还创建一个List并将需要重新访问的集合传递给它,如果需要索引访问。

If you are looking to get a your objects according to an index - what you are really looking for is an ArrayList - which allows access by index, and is basically a dynamic array . 如果您要根据索引获取对象-真正要寻找的是ArrayList它允许按索引访问,并且基本上是一个动态数组

If you want to objects "attached" to the same index, you can use 2 ArrayLists (one for each type of object) - or use a Pair (from apache commons) as an element in the arraylist. 如果要将对象“附加”到同一索引,则可以使用2个ArrayList(每种类型的对象一个)-或使用Pair (来自Apache Commons)作为arraylist中的元素。

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

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