Leyon Gudinho
Leyon Gudinho

Reputation: 91

Ignore case while comparing string with keyvalue in the hashmap

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!!!

Upvotes: 6

Views: 6556

Answers (6)

Yogendra Joshi
Yogendra Joshi

Reputation: 228

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

Upvotes: 2

Lipsyor
Lipsyor

Reputation: 438

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

Upvotes: 2

jMounir
jMounir

Reputation: 495

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

Upvotes: 3

Bon
Bon

Reputation: 3103

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

Upvotes: 1

Jean Logeart
Jean Logeart

Reputation: 53819

Only use lowercase keys:

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

Upvotes: 2

Steven
Steven

Reputation: 604

pref.containsKey(buffSB.toString().toLowerCase())

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

Upvotes: -1

Related Questions