我有一个 ArrayList,我将其字符串用作 HashMap>
当我最终放置键时和我的 HashMap
中的值,我实际上不知道特定的字符串最终去了哪里,即我的 ArrayList
中的索引位置(因为字符串只得到根据某些条件添加)
我知道每个 put
应该使用哪个字符串作为键,但我不知道如何从 ArrayList
中获取它因为我不知道它在哪里。
I have an ArrayList<String>
, the strings of which I'm using as keys in a HashMap<String, ArrayList<Object>>
When I finally put the keys and values in my HashMap
, I don't actually know where a particular string ended up going, i.e. what index position in my ArrayList<String>
(because the strings only get added depending on certain conditions)
I know which string each put
should use as the key, but I don't know how to grab hold of it from the ArrayList<String>
since I don't know where it is.
发布评论
评论(2)
数组有一个 indexOf 方法,您可以将字符串传递给该方法来查找索引。
但是,如果您知道要在 Map 中放入哪个字符串,为什么需要键列表,或者为什么需要将字符串从列表中取出,因为您已经拥有它了?
arrays have an indexOf method, to which you can pass your string to find the index.
But if you know which String to put in the Map, why do you need the list of keys, or why do you need to get the String out the list, since you have it already?
简而言之,如果您想维护散列以及插入顺序,请查看 java.util.LinkedHashMap
In short, if you want to maintain hashing and also the order of insertion, then look at
java.util.LinkedHashMap