检查是否'键'存在于hashmap中

时间:2014-06-18 12:28:37

标签: java

我有一个关键字和值为#'字符串'的哈希映射。我想通过在' $'之后忽略字符串来检查特定键是否存在。在钥匙。

Hashmap包含以下关键字:' acctId $ accountId',' acctId $ desc',' acctId $ crncyCode'等

Iterator itx = uiToSrvFldMapList.entrySet().iterator();
if(uiToSrvFldMapList.containsKey(cellId)){
      String sSrvFld = (String) uiToSrvFldMapList.get("acctId");
      System.out.println("sSrvFld :: " +sSrvFld);

4 个答案:

答案 0 :(得分:1)

public static void main(String[] args) {
    String s = "acctId$accountId";
    s = s.replaceAll("\\$.*", "");// remove everything after $
    System.out.println(s);
    // do hm.get(s) here
}

答案 1 :(得分:0)

我希望这可以帮到你

    Map map = new HashMap();
    map.put("abc$def","ABC");
    map.put("ab","A");
    map.put("de","b");
    String key = "abc$def";
    String s[] = key.split("$");
    if(map.containsKey(s[0]))
        System.out.println("Value is: "+map.get(key));
    else
        System.out.println("cannot find..");

答案 2 :(得分:0)

假设在 " acctId $ accountId" 中,您将拥有与 " acctId&#相同的字符串34; " accountId" ,您可以通过以下方式进行搜索:

   `Map<String, String> uiToSrvFldMapList = new HashMap<String, String>();
    uiToSrvFldMapList.put("0000$0000", "test"); // just an example
    uiToSrvFldMapList.put("0000$0001", "description"); // just an example
    uiToSrvFldMapList.put("0001$0000", "2test"); // just an example
    uiToSrvFldMapList.put("0001$0001", "2description"); // just an example

    String acctId = "0000"; // the account id string
    if(uiToSrvFldMapList.containsKey(acctId +"$" + acctId)){
       String sSrvFld = (String) uiToSrvFldMapList.get(acctId + "$" + acctId);                            
       System.out.println("sSrvFld :: " +sSrvFld);
       }`

答案 3 :(得分:0)

这是一个测试程序,它显示了实现此功能的方法:

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;
    }
}

只需在需要此功能的地方编写方法valuesContainingKeys(不需要是静态的)。此方法将返回所有值的列表,其对应的键包含您要查找的字符串。如果没有值,只需检查valuesContainingKey.isEmpty()即可返回,相应的密钥以搜索到的密钥开头。