java - Implementing Priority queue using hashmap -
this may naive question or not create sense. confused implementation of priority queue. why cannot utilize hashmap key priority , value info priority. missing basic here. eg: if priority of 1, b 3 , c 4 can maintain 1,3,4 keys , a,b,c values of hashmap respectively priority queue implementation. please clear logic
the problem implementation doesn't take advantage of hashmap capabilities access element given key. since making queue, won't have priorities 1, 3, 4 later, , need find highest priority of map, having iterate exclusively so!
a map works this: creates gigantic array null values in positions (very, vey big). then, uses formula take key, string or string representation , turn number. simple way sum char codes of each letter, though bad selection map, can why later. then, caps number modulus (remainder) size of array (e.g., n % size in java), becomes valid position in array, , set element there. way, easy create intensive search, because don't need search, know each element is.
therefore, implementing queue map memory intensive, without clear advantage of hashmap search. furthermore, iterating on very, expensive, because need iterate on gigantic array , ignore null values; not sure actual values are. imagine in example, priorities go 0 100. though never have 100 tasks, have 100 position array iterate over, checking on each position if there task priority.
a improve way utilize simple list , store priority within data. assuming priority doesn't alter on time, need iterate 1 time upon adding, find right place, , access first (or last) element, 1 known highest priority.
on final note o performance, improve iterate upon adding, though adding exact same amount of time searching. because when search highest priority, need go until end, every time, because maybe lastly element bigger one. note hashmap won't store items organized in neat crescent order, if keys numeric strings or integer. designed specially not that, avoid conflicts (two similar objects having same key, , requiring need add together list particular position of big array). when add, however, assuming list ordered, need iterate until reach right destination.
java hashmap priority-queue
No comments:
Post a Comment