I assume you are trying to use something like single standard key for a map that can be represented by multiple different labels. Like in your example: you expect to use GENDER_ATTRIBUTE
as a key that is equal to sex
and gender
labels, right?
If so, consider following example:
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
final class MapTest {
private static final Map<String, List<String>> similarKeys = new HashMap<String, List<String>>() {{
put("GENDER_ATTRIBUTE", Arrays.asList("sex", "gender"));
}};
public static void main(String[] args) {
final Map<String, Object> dataSet1 = new HashMap<String, Object>() {{
put("name", "ABC");
put("address", "XYZ");
put("gender", "F");
}};
final Map<String, Object> dataSet2 = new HashMap<String, Object>() {{
put("name", "ABC");
put("address", "XYZ");
put("sex", "F");
}};
System.out.println("GENDER_ATTRIBUTE from dataSet1 is " + getValueForKey("GENDER_ATTRIBUTE", dataSet1));
System.out.println("GENDER_ATTRIBUTE from dataSet2 is " + getValueForKey("GENDER_ATTRIBUTE", dataSet1));
}
private static Object getValueForKey(String key, Map<String, Object> dataSet) {
final String finalKey = similarKeys.getOrDefault(key, Collections.emptyList())
.stream()
.filter(it -> dataSet.keySet().contains(it))
.findFirst()
.orElse(key);
return dataSet.get(finalKey);
}
}
What happens here is that firstly we define similarKeys
mapping - a map that associates single key with multiple different labels. Then the whole logic is encapsulated in getValueForKey(String key, Map<String, Object> dataSet)
. This method checks if passed key is a final key or is it pointing to a one similar keys. If so then it checks which key can be found in given dataset. Please let me know if this is what you expect. The question was not precise so I had to make some assumptions to prepare this answer. I hope it helps.