Android String Case Convert lowercaseKeys(Map map)

Here you can find the source of lowercaseKeys(Map map)

Description

Given a map, creates and returns a new map in which all keys are the lower-cased version of each key.

License

Apache License

Parameter

Parameter Description
map A map containing String keys to be lowercased

Exception

Parameter Description
IllegalArgumentException if the map contains duplicate string keysafter lower casing

Declaration

public static <V> Map<String, V> lowercaseKeys(Map<String, V> map) 

Method Source Code

//package com.java2s;
/**//  www. jav a 2 s .  com
 * Copyright (c) 2000, Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.HashMap;

import java.util.Map;

public class Main {
    /**
     * Given a map, creates and returns a new map in which all keys are the
     * lower-cased version of each key.
     *
     * @param map A map containing String keys to be lowercased
     * @throws IllegalArgumentException if the map contains duplicate string keys
     *           after lower casing
     */
    public static <V> Map<String, V> lowercaseKeys(Map<String, V> map) {
        Map<String, V> result = new HashMap<String, V>(map.size());
        for (Map.Entry<String, V> entry : map.entrySet()) {
            String key = entry.getKey();
            if (result.containsKey(key.toLowerCase())) {
                throw new IllegalArgumentException(
                        "Duplicate string key in map when lower casing");
            }
            result.put(key.toLowerCase(), entry.getValue());
        }
        return result;
    }

    /**
     * Safely convert the string to lowercase.
     * @return lower case representation of the String; or null if
     * the input string is null.
     */
    public static String toLowerCase(String src) {
        if (src == null) {
            return null;
        } else {
            return src.toLowerCase();
        }
    }
}

Related

  1. toLowerCase(String str)
  2. toLowerCase(String str, int beginIndex, int endIndex)
  3. toUpperCase(String str)
  4. toUpperCase(String str, int beginIndex, int endIndex)
  5. endsWithIgnoreCase(String str, String suffix)
  6. startsWithIgnoreCase(String str, String prefix)
  7. toLowerCase(String src)
  8. toUpperCase(String src)
  9. flipCase(final String s)