java - Efficient way to modify a list of objects using maps -
i have list of objects, these objects can shift position in list or removed, object saving, each of them have unique id, lets call id "key". check membership in list have additional map mapped
<object.getkey(), object>
currently call method key, ,
map.contains(key) // delete list.remove(map.get(key)) // edit object oldobj = list.get(map.get(key)) // modify old obj, immutable need set // old position new object list.set(map.get(key), newobject)
for sure code shit, not able wrap head around clean solution this, want avoid o(n) searches on list happening in case of list.remove , list.get!
one of solutions think of map key list position in case map has delete operation or insert operation i'd have update whole map position of change! again o(n). suggestions on efficient way this?
you haven't specified need list, assuming you're looking collection predictable order, consider using linkedhashset
- retains order of insertion while stile allowing operations add
, remove
or contains
in o(1).
Comments
Post a Comment