What I’m doing is storing classes into an ArrayList and retrieve them by its index number. But are there any list classes in Java where I can retrieve a list element by, lets say, its name? Like this:
ArrayList<string> myArr = new ArrayList<string>();
myArr.add( "ID_name", "String to store" );
ands then retrieve it by:
myArr.get( "ID_name" );
Also, are there any other alternatives to ArrayList? I need a list class to be optimized for:
- Random access
- Only need to push items into the list
- Never need to delete anything from the list
If all you want to store is key-value pairs, and don’t care about iteration order, I think you might like the HashMap class: