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

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

2

Only use lowercase keys:

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

2 Comments

And if he absolutely needs the strings to have Uppercases and Lowercases? Like names for example...
this has the benefit of avoiding duplicate keys that differ only by case.
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

Comments

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

Comments

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

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

1 Comment

if pref contains "Hello". what would be the result for "HELLO" or "hello"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.