This is a test program, which shows a way to achieve this functionality:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class Test {
public static void main(String[] args) {
Map<String, String> uiToSrvFldMapList = new HashMap<String, String>();
uiToSrvFldMapList.put("acctId$accountId", "accid");
uiToSrvFldMapList.put("acctId$desc", "accdesc");
uiToSrvFldMapList.put("acctId$crncyCode", "currencyCode");
uiToSrvFldMapList.put("smthElse$smthElse", "smthElse");
List<String> valuesContainingKey = valuesContainingKeys(
uiToSrvFldMapList, "acctId");
// Returns if the key is contained
if (valuesContainingKey.isEmpty()) {
System.out.println("The key is not contained in the map");
} else {
System.out.println("The part of the key is in the map");
}
System.out
.println("All values, where the corresponding key contains the subkey: ");
for (String s : valuesContainingKey) {
System.out.println(s);
}
}
/**
*
* @param map
* Map containing the key-value pairs
* @param searchString
* A String used as a subkey, for which is searched if it is
* contained as a substring at the beginning of a key in the map
* @return List of all Values from the map, whose corresponding key contains
* searchString
*/
private static List<String> valuesContainingKeys(Map<String, String> map,
String searchString) {
List<String> containingKeys = new ArrayList<String>();
for (Entry<String, String> e : map.entrySet()) {
if (e.getKey().startsWith(searchString)) {
containingKeys.add(e.getValue());
}
}
return containingKeys;
}
}
Simply write the method valuesContainingKeys
(not needed to be static) where you want this functionality. This method will return a list of all values, whose corresponding key contains the string you are looking for. Simply checking valuesContainingKey.isEmpty()
will return if there is no value, for which the corresponding key begins with the searched key.