6

I am trying to check if my hashmap key set contains the string 'buffSB.toString()'. But I wanted to compare ignoring case (upper or lower).

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

Any help will be greatly appreciated!!!

Leyon Gudinho
  • 91
  • 2
  • 11

6 Answers6

3

You can also try to use TreeMap which enable providing a comparator to its constructor:

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

see Case insensitive string as HashMap key

Community
  • 1
  • 1
jMounir
  • 495
  • 2
  • 11
2

You may use CaseInsensitiveMap<K,V> instead of Map<K,V>

It extends AbstractHashedMap<K,V> and it ignores case of keys. You can create a new instance of this map by passing the existing case-sensitive map to the constructor.

Consider this example:

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

The map will contains three elements:

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

Infact map.put("one", "Four") overwrites the value insert with map.put("One", "One").

map.get(null) returns "Three".

map.get("ONE"), map.get("one"), map.get("One"), and so on returns "Four".

Keep in mind that the keySet() method returns all lowercase keys, or nulls.

keySet() equals {"one", "two", null}.

Further documentation

Lipsyor
  • 428
  • 1
  • 7
  • 20
2

If you see the implementation of HashMap getKey method you have to pass the proper key to generate the hash value and get the actual value from that bucket.

    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;

Three solutions

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

OR

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

OR

    Looping through map with the ignore case
Yogendra Joshi
  • 228
  • 1
  • 8
1

Only use lowercase keys:

map.put(key.toLowercase(), value);
// later
String val = map.get(someKey.toLowerCase());
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
1

You can loop through the keySet of the map, for each key, use the string function equalsIgnoreCase to compare:

    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;
        }
    }
Bon
  • 3,073
  • 5
  • 21
  • 40
-1
pref.containsKey(buffSB.toString().toLowerCase())

Use string.toUpperCase() or string.toLowerCase() to ignore case.

Steven
  • 604
  • 6
  • 11