I have to choose one data structure for my need below i am explaining the conditions there are following values
abc,def,rty,ytr,dft which all are map to row R
I think its best to keep it simple until poor performance shows a need for some kind of improvement. Even if many map values are the same string, that should be ok since Java stores only one copy in heap. If the sets of keys mapped to a single string gets very large some performance improvement may be possible by doing two lookups -- first to determine set membership and second to retrieve the value of the key associated with the set. That would be easy to implement. For now here is an immediately straightforward approach:
import java.util.*;
public class HashMapDemo {
static HashMap map = new HashMap();
public static void lookup(String key, String value) {
if (map.get(key) == value) {
System.out.println(key + " lookup ok");
} else {
System.out.println(key + " lookup produced" + map.get(key));
}
}
public static void main(String[] args) {
// requirements:
// abc,def,rty,ytr,dft ---> R1B1
// abEERc,dFFFef,rGGty ---> abEERc
Set kset1 = new HashSet(Arrays.asList("abc", "def",
"rty", "ytr", "dft"));
Set kset2 = new HashSet(Arrays.asList("abEERc",
"dFFFef", "rGGty"));
for (String s : kset1) {
map.put(s, "R1B1");
}
for (String s : kset2) {
map.put(s, "abEERc");
}
// testing value lookup with key
for (String s : kset1) {
lookup(s, "R1B1");
}
// prints:
// abc lookup ok
// dft lookup ok
// def lookup ok
// rty lookup ok
// ytr lookup ok
for (String s : kset2) {
lookup(s, "abEERc");
}
// prints:
// rGGty lookup ok
// abEERc lookup ok
// dFFFef lookup ok
// change key "R1B1" to "XYZ"
for (String s : kset1) {
map.put(s, "XYZ");
}
// test the change
for (String s : kset1) {
lookup(s, "XYZ");
}
// prints:
// abc lookup ok
// dft lookup ok
// def lookup ok
// rty lookup ok
// ytr lookup ok
}
}