find key in a Map Ignoring Case - Java java.util

Java examples for java.util:Map Key

Description

find key in a Map Ignoring Case

Demo Code


//package com.java2s;

import java.util.Map;

public class Main {
    public static <V> V findIgnoringCase(final Map<?, V> map,
            final String key) {
        V value = map.get(key);//from   w  ww.j a  v a2  s. co  m

        if (value == null) {
            for (Map.Entry<?, V> entry : map.entrySet()) {
                final Object entryKey = entry.getKey();

                if (entryKey instanceof String) {
                    if (((String) entryKey).equalsIgnoreCase(key)) {
                        value = entry.getValue();
                        break;
                    }
                }
            }
        }

        return value;
    }
}

Related Tutorials