java - Is it OK to use a HashMap to keep indices of list elements? -
i have list of objects.
list<myobject> mylist;
this list populated in beginning , no update done after that.
the order of objects in list significant because need iterate on list in order later.
during execution of program, need find index of given myobject
in mylist
;
i know can use mylist.indexof(object)
worried performance. going use hashmap
map each myobject
in mylist
index in mylist
can find index efficiently.
but not sure whether missing trivial , going use 2 containers redundant data done using different container.
so, can see better way this?
you can use linkedhashmap
contain myobject
instances.
map<string, myobject> map = new linkedhashmap<string, myobject>();
hash table , linked list implementation of map interface, predictable iteration order.
this allow have 1 collection both.
Comments
Post a Comment