【问题标题】:Ignore case while comparing string with keyvalue in the hashmap将字符串与哈希图中的键值进行比较时忽略大小写
【发布时间】:2016-02-23 02:11:36
【问题描述】:

我正在尝试检查我的 hashmap 键集是否包含字符串“buffSB.toString()”。但我想比较忽略大小写(大写或小写)。

static StringBuilder buffSB = new StringBuilder(); 

buffSB.append(alphabet);

Map<String, String> pref =  new Datamatch().main();  // Getting the Hashmap from other class

if(pref.containsKey(buffSB.toString()))             //This is where I need to ignore case while searching string in the map key set 
  { 
      String val = pref.get(buffSB.toString());
  }

任何帮助将不胜感激!!!

【问题讨论】:

    标签: java hashmap ignore-case


    【解决方案1】:

    您也可以尝试使用 TreeMap 来为其构造函数提供比较器:

    Map<String, String> yourMap= new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);

    Case insensitive string as HashMap key

    【讨论】:

      【解决方案2】:

      您可以使用CaseInsensitiveMap&lt;K,V&gt; 代替Map&lt;K,V&gt;

      它扩展了AbstractHashedMap&lt;K,V&gt;,它忽略了键的大小写。 您可以通过将现有的区分大小写的映射传递给构造函数来创建此映射的新实例。

      考虑这个例子:

      Map<String, String> map = new CaseInsensitiveMap<String, String>();
      map.put("One", "One");
      map.put("Two", "Two");
      map.put(null, "Three");
      map.put("one", "Four");
      

      地图将包含三个元素:

      <one, "Four">
      <two, "Two">
      <null, "Three">
      

      事实上map.put("one", "Four")map.put("One", "One") 覆盖值插入。

      map.get(null) 返回“三”。

      map.get("ONE")map.get("one")map.get("One") 等返回“四”。

      请记住,keySet() 方法返回所有小写键或空值。

      keySet() 等于 {"one", "two", null}

      Further documentation

      【讨论】:

        【解决方案3】:

        如果您看到 HashMap getKey 方法的实现,您必须传递正确的键来生成哈希值并从该存储桶中获取实际值。

            if (key == null)
                return getForNullKey();
            int hash = hash(key.hashCode());
            for (Entry<K,V> e = table[indexFor(hash, table.length)];
                 e != null;
                 e = e.next) {
                Object k;
                if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
                    return e.value;
            }
            return null;
        

        三种解决方案

            Map<String, String> pref = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
        

            Map<String, String> map = new CaseInsensitiveMap<String, String>();
        

            Looping through map with the ignore case
        

        【讨论】:

          【解决方案4】:

          只使用小写键:

          map.put(key.toLowercase(), value);
          // later
          String val = map.get(someKey.toLowerCase());
          

          【讨论】:

          • 如果他绝对需要字符串有大写和小写?比如名字……
          【解决方案5】:

          可以循环遍历map的keySet,对于每个key,使用字符串函数equalsIgnoreCase进行比较:

              Map<String, String> pref = new Datamatch().main();  // Getting the Hashmap from other class
          
              String val;
              for (String key : pref.keySet()) {
                  if (key.equalsIgnoreCase(buffSB.toString())) {
                      val = pref.get(key);
                      break;
                  }
              }
          

          【讨论】:

            【解决方案6】:
            pref.containsKey(buffSB.toString().toLowerCase())
            

            使用 string.toUpperCase() 或 string.toLowerCase() 忽略大小写。

            【讨论】:

            • 如果首选项包含“Hello”。 “HELLO”或“hello”的结果是什么?
            猜你喜欢
            • 2017-04-25
            • 1970-01-01
            • 1970-01-01
            • 2017-03-27
            • 2016-01-29
            • 1970-01-01
            • 1970-01-01
            • 2011-01-03
            • 1970-01-01
            相关资源
            最近更新 更多