java - Comparing values using hashtable -
there table like:
code(starts with) value 1 aaa 101d bbb 101dg ccc 2 ddd
the above table has columns code , corresponding values. code column signifies string starts code given , on comparing input code of user, corresponding value assigned value column of table.
for example:
- if user's code 100000, value should aaa......
- for user's code 101d1111, value should bbb(not aaa if starts 1 consider more significant comparison 101d)......
- for user's code 101dg222, value should ccc(not bbb if starts 101d notice significant comparison).....
- for user cose 23333, value=ddd.....
i have put following table in hashtable key code column , value value column.
hashtable hash= new hashtable(); hash.put("1","aaa"); hash.put("101d","bbb"); hash.put("101dg","ccc"); hash.put("2","ddd"); string comp="101d1111";//string compare iterator itr= hash.entryset().iterator(); while(itr.hasnext()) { map.entry e=(map.entry)itr.next(); string key= (string).getkey(); //**here logic needed compare comp , key , retrieve corrsponding value here bbb** }
kindly me logic.
you have first sort entries key length, longest ones being first , shortest ones last. because first want check whether code starts 101dg
, , check whether starts 101dg
, 1
.
then check matches use this:
if (comp.substring(0,key.length()).equals(key)) { // it's match — use value }
Comments
Post a Comment