Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Creating Case-Insensitive Maps in Java: Simplifying Lookups with Lowercase Keys

In software development, particularly in Java, there are situations where case sensitivity in data lookups can be a trouble. Consider scenarios where you need to retrieve values from a collection, such as a Map, where the keys may vary in their case sensitivity. To address this issue, developers often depends on solutions like case-insensitive maps.

 


A case-insensitive map is a data structure that enables developers to perform lookups without considering the case of the keys. In this context, all keys are converted to lowercase strings, ensuring uniformity in the lookup process. However, it's crucial to maintain the original keys intact for preserving data integrity and reference.

 

Use Cases:

1.   User Authentication: When dealing with user authentication systems, usernames often need to be case insensitive. A case-insensitive map can be employed to store user credentials, allowing users to log in regardless of the case used in their usernames.

2.   Configuration Management: In applications where configuration settings are stored in a map-like structure, a case-insensitive map can ensure consistency in accessing these settings. Developers can retrieve configuration values without worrying about the case sensitivity of the keys.

3.   URL Routing in Web Applications: Web applications often handle URLs where the paths may vary in case sensitivity. By utilizing a case-insensitive map to store route mappings, developers can efficiently route incoming requests to the appropriate handlers without considering the case of the URLs.

4.   Data Processing and Filtering: In data processing tasks, especially when dealing with textual data, a case-insensitive map can be invaluable. It allows for efficient filtering and retrieval of data based on case-insensitive criteria, enhancing the robustness and usability of data processing pipelines.

 

By incorporating case-insensitive maps into the Java applications, developers can simplify data lookups, improve system reliability, and enhance overall user satisfaction by providing a seamless and intuitive experience, irrespective of the case sensitivity of the input data.

 

Find the sample implementation of Case insensitive map.

 

CaseInsensitiveHashMap.java

package com.sample.app.collections.map;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class CaseInsensitiveHashMap extends HashMapString, Object> {
	private static final long serialVersionUID = 123291348765l;

	private final Map lowerCaseMap = new HashMap();

	public CaseInsensitiveHashMap(final int initialCapacity) {
		super(initialCapacity);
	}

	@Override
	public boolean containsKey(final Object key) {
		final Object realKey = lowerCaseMap.get(key.toString().toLowerCase(Locale.ENGLISH));
		return super.containsKey(realKey);
	}

	@Override
	public Object get(final Object key) {
		final Object realKey = lowerCaseMap.get(key.toString().toLowerCase(Locale.ENGLISH));
		return super.get(realKey);
	}

	@Override
	public Object put(final String key, final Object value) {
		final Object oldKey = lowerCaseMap.put(key.toLowerCase(Locale.ENGLISH), key);
		final Object oldValue = super.remove(oldKey);
		super.put(key, value);
		return oldValue;
	}

	@Override
	public void putAll(final Map extends String, ?> m) {
		m.forEach(this::put);
	}

	@Override
	public Object remove(final Object key) {
		final Object realKey = lowerCaseMap.remove(key.toString().toLowerCase(Locale.ENGLISH));
		return super.remove(realKey);
	}
}

CaseInsensitiveHashMapDemo.java

package com.sample.app;

import java.util.Map;

import com.sample.app.collections.map.CaseInsensitiveHashMap;

public class CaseInsensitiveHashMapDemo {
	public static void main(String[] args) {
		Map map = new CaseInsensitiveHashMap(10);
		
		map.put("India", "New Delhi");
		map.put("Japan", "Tokyo");
		
		System.out.println("Capital of India is :");
		System.out.println(map.get("India"));
		System.out.println(map.get("INDIA"));
		System.out.println(map.get("INDia"));
		
		
	}

}

Output

Capital of India is :
New Delhi
New Delhi
New Delhi



You may like

Interview Questions

Utility class to get primitive type from wrapper type and vice versa

FNV hash algorithm implementation in Java

Controlling Randomness in Java: Exploring the Role of Seeds in java.util.Random

Calculating initial capacity from expected size and load factor

A Beginner's Guide to UUID Types

Ensuring Platform-Independent Accuracy: A Guide to strictfp in Java



This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Creating Case-Insensitive Maps in Java: Simplifying Lookups with Lowercase Keys

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×