java - Reducing time complexity -
i have list of objects contains statusenum. now, want return objects falls under specific list of provided statuses.
a simple solution loop on list of objects , loop on provided list of statusenums ... work however, make time complexity of o(n)^2. there way reduce o(n) ?
i can't change map. other solution think of maintaining map based on statusenums key increase space complexity lot.
edit
- i had hashmap of objects (which said list)
here code came others ...
public list<myobjects> getobjectsbasedoncriteria (list<objectstatus> statuses, string secondcriteria){ enumset<objectstatus> enumset = enumset.copyof(statuses); (map.entry<long, myobject> objentry : myobjs.entryset()){ myobjects obj = objentry.getvalue(); if (enumset.contains(obj.getstatus()) && obj.equals(secondcriteria)){ ... } } }
use set hold statusenums (probably enumset), , check if each instance's status in set using set.contains(object.getstatus()), or whatever.
lookups in enumset , hashset o(1), solution linear (assuming 1 status per object). enumset.contains more efficient hashset.contains enum values; however, choice irrelevant overall time complexity.
Comments
Post a Comment