Ignore case when comparing string with key in hash map

I am trying to check if my hash map set contains the string 'buffSB.toString ()'. But I wanted to compare the ignoring case (top or bottom).

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 would be greatly appreciated.

+6
source share
6 answers

If you see an implementation of the getKey HashMap method, you must pass the correct key to generate the hash value and get the actual value from this 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 decisions

  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 
+1
source

You can also try using TreeMap, which allows you to provide the comparator with its constructor:

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

see Case-insensitive string as a HashMap key

+2
source

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

It extends AbstractHashedMap<K,V> and ignores key case. You can create a new instance of this card by transferring the existing card to the case-sensitive constructor.

Consider the following 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 contain three elements:

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

Infact map.put("one", "Four") overwrites the insertion of map.put("One", "One") values map.put("One", "One") .

map.get(null) returns "Three".

map.get("ONE") , map.get("ONE") , map.get("ONE") , etc. returns "Four."

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

keySet() is equal to {"one", "two", null} .

Additional documentation

+2
source

Use only lowercase letters:

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

You can scroll the keySet map on 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; } } 
+1
source
 pref.containsKey(buffSB.toString().toLowerCase()) 

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

-1
source

All Articles