简体   繁体   中英

String from ArrayList to use as key in HashMap?

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, ie 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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