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
- toLowerCase(String str)
- toLowerCase(String str, int beginIndex, int endIndex)
- toUpperCase(String str)
- toUpperCase(String str, int beginIndex, int endIndex)
- endsWithIgnoreCase(String str, String suffix)
- startsWithIgnoreCase(String str, String prefix)
- toLowerCase(String src)
- toUpperCase(String src)
- flipCase(final String s)