java - How to change the key of one map corresponding to a value in another map? -
i working on project in having map of string , object coming below method. , suppose if data map contains -
a = p b = q c = r d = s e = t f = u g = v h = w = x where a, b, c, d, e, f, g key , p, q, r, s, t, u, v there corresponding values in above map.
below method -
private void datacheck(map<string, object> data) { } now have static mapping in map of string , string -
public static final map<string, string> static_mapping = collections.unmodifiablemap(new linkedhashmap<string, string>() {{ put("a","hello"); put("b","world"); put("c","titan"); put("d","david"); put("e","elephant"); put("f","fire"); put("g","gel"); }}); here value of key a hello, value of key b world, value of key c titan , same thing other key value pairs.
now in below method have mentioned above, getting values in map contain data shown above -
private void datacheck(map<string, object> data) { // how extract value of key // static_mapping map corresponding data map key. // print out new map here latest mappings } now data map have a = p, replace hello = p because a mapped hello shown in static_mapping , same other fields well. how can efficiently?
and suppose if don't have mapping key in data map, keep is.
update:-
so new map should have data -
hello = p world = q titan = r david = s elephant = t fire = u gel = v h = w = x as there no mapping key h , i keep is..
(map.entry<string, object> entry : data.entryset()) { string key = entry.getkey(); object value = entry.getvalue(); string staticmappingvalue = (string) static_mapping.get(key); if (staticmappingvalue != null) { newmap.put(staticmappingvalue, value); } else { newmap.put(key, value); } } so newmap:
["hello":"p", "world":"q", etc..., "h":"w"] edited: followed antoine wils suggestion , reduced accesses maps, since question highlighted efficiency.
Comments
Post a Comment