【问题标题】:Check if 'Key' exists in a hashmap [duplicate]检查哈希图中是否存在“键”[重复]
【发布时间】:2014-06-18 12:28:37
【问题描述】:

我有一个键和值是“字符串”的哈希图。我想通过忽略密钥中“$”之后的字符串来检查特定密钥是否存在。

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

【问题讨论】:

  • 为什么不直接使用堆叠地图?像Map<String, Map<String,String>> 这样的东西会让访问变得更容易和更快。然后您可以检查myMap.get(firstPart).get(secondPart)。当然,如果第一个不是null,您必须检查。这对您的运行时间也会更好。
  • 使用 if(uiToSrvFldMapList.containsKey(cellId.split("$")[0])){
  • 这种结构不容易,基本上要对整个表进行线性搜索。

标签: java


【解决方案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
}

【讨论】:

    【解决方案2】:

    希望对你有帮助

        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..");
    

    【讨论】:

      【解决方案3】:

      假设在 "acctId$accountId" 中,您将拥有与 "acctId" 相同的字符串和“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);
             }`
      

      【讨论】:

        【解决方案4】:

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

        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(),如果没有值,则返回,对应的键以搜索到的键开始。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-04-05
          • 1970-01-01
          • 2018-03-31
          • 2011-05-19
          • 2011-12-28
          • 2011-05-30
          相关资源
          最近更新 更多