Choosing the perfect data structure for the below data in java -
i have choose 1 data structure need below explaining conditions there following values
abc,def,rty,ytr,dft map row r1b1 (actully key combination of r1+b1) abeerc,dfffef,rggty map row r1b2 (actully key combination of r1+b2)
key value abc,def,rty,ytr,dft ---> r1b1 abeerc,dfffef,rggty ---> r1b2
now, example, let's say, if ytr
able retrieve r1b1
or, let's say, value rggty
able retrieve r1b2
now case matters of search, complexity , time taken things have go in sequence
for example, first pick first line search ytr
, first match abc
not match have match def
not again match match rty
not match match ytr
, find key r1b1
finally
similarly if second string need searched lets rggty
scan first row in not find value search continue second row , in second row in third element rggty
element retrieve r1b2
value
let's say, if put thing in map sequence search go on key , able find corresponding value
folks please advise best data structure can implement in java in have search keys items find corresponding value in fast time not hit performance ,the kind of data structure performance should high
please advise folks
key-value pairs can accessed in o(1) using hashmap
. if use hashmap<string, string>
updating value painful because strings immutable. means have check entry sets , if value matches update it. create helper class value , let keys point instance of class. here stub important functions, guess can add rest yourself.
public class mydatastructure { private map<string, myvalue> key_value = new hashmap<string, myvalue>(); private map<string, myvalue> value_myvalue = new hashmap<string, myvalue>(); public void set(string key, string value) { myvalue v = value_myvalue.get(value); if (v == null) { // should happen, check containskey v = new myvalue(value); value_myvalue.put(v); } key_value.put(key, v); } public string get(string key) { return key_value.get(key).getvalue(); // key might not exist } public string changevalue(string oldvalue, string newvalue) { myvalue v = value_myvalue.remove(oldvalue); // oldvalue might not exist v.setvalue(newvalue); value_myvalue.put(newvalue, v); // not work if newvalue exists... have merge } private class myvalue() { private string value; public myvalue(string value) { this.value = value; } public string getvalue() { return value; } public void setvalue(string value) { this.value = value; } } }
Comments
Post a Comment