Common Java Cookbook

Edition: 0.19

Download PDF or Read on Scribd

Download Examples (ZIP)

5.12. Using a Case-Insensitive Map

5.12.1. Problem

You need to use a Map with String keys that will ignore the capitalization of a key when retrieving a value.

5.12.2. Solution

Use a CaseInsensitiveMap from the Commons Collections. This implementation of Map takes String keys and provides case-insensitive access. An entry with a key "Test" can be retrieved with the strings "TEST," "test," and "tEST." Here is a small example demonstrating the case insensitivity:

import java.util.*;
import org.apache.commons.collection.map.CaseInsensitiveMap;
Map grades = new CaseInsensitiveMap( );
grades.put( "Fortney", "B-" );
grades.put( "Puckett", "D+" );
grades.put( "Flatt", "A-" );
String grade1 = (String) grades.get( "puckett" );
String grade2 = (String) grades.get( "FLATT" );

In this example, the grades are stored with a capitalized last name, and the results are retrieved with irregularly capitalized last names. This example returns the grades for "Puckett" and "Flatt" even though they were retrieved with "puckett" and "FLATT."

5.12.3. Discussion

Example 5-12 demonstrates the use of CaseInsensitiveMap to access state names by state abbreviations regardless of capitalization. This is useful when an application is requesting a state from a user in a form to capture an address. If a user enters "il," "IL," or "Il," you need to be able to return "Illinois."

Example 5-12. Using a CaseInsensitiveMap for U.S. states

package com.discursive.jccook.collections.insensitive;
import java.util.Map;
import org.apache.commons.collections.map.CaseInsensitiveMap;
public class CaseInsensitiveExample {
    Map states = new CaseInsensitiveMap( );
    public static void main(String[] args) {
        CaseInsensitiveExample example = new CaseInsensitiveExample( );
        example.start( );
    }
    
    private void start( ) {
        states.put("IL", "Illinois");
        states.put("PA", "Pennsylvania");
        states.put("GA", "Georgia");
        states.put("AZ", "Arizona");
    
        String stateName = (String) states.get( "il" );
        System.out.println( "Value retrieved for 'il': " + stateName );
        stateName = (String) states.get( "IL" );
        System.out.println( "Value retrieved for 'IL': " + stateName );
        stateName = (String) states.get( "iL" );
        System.out.println( "Value retrieved for 'iL': " + stateName );
    }
}

Example 5-12 populates a CaseInsensitiveMap with state abbreviations and state names, and it retrieves the state name for three different capitalizations of "IL": "iL," "IL," and "il." For all three keys, the CaseInsensitiveMap returns the proper state name—"Illinois"—as illustrated by the output from the previous example:

Value retrieved for 'il': Illinois
Value retrieved for 'IL': Illinois
Value retrieved for 'iL': Illinois

5.12.4. See Also

If you are interested in how this class works, take a look at the source for CaseInsensitiveMap, and you will see that this implementation of Map extends the AbstractHashedMap class in the org.apache.commons.collections.map package. It would be just as easy to decorate a Map with a Transformer object to provide case insensitivity. Recipe 5.16 discusses the use of a Transformer to alter objects as they are stored in a Collection.


Creative Commons License
Common Java Cookbook by Tim O'Brien is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License.
Permissions beyond the scope of this license may be available at http://www.discursive.com/books/cjcook/reference/jakartackbk-PREFACE-1.html. Copyright 2009. Common Java Cookbook Chunked HTML Output. Some Rights Reserved.