/**
* debugPrint(PrintStream out, Object label, Map<?,?> map)
* Prints the given map with nice line breaks.
* {
* key1 = 1 java.lang.Byte
* key2 = 2 java.lang.Byte
* key3 = 3 java.lang.Byte
* } java.util.TreeMap
*/
@Test
public void test1() {
Map<String, Object> map = new TreeMap<>();
map.put("key1", (byte) 1);
map.put("key2", (byte) 2);
map.put("key3", (byte) 3);
MapUtils.debugPrint(System.out, null, map);
/**
* {
* key1 = 1 java.lang.Byte
* key2 = 2 java.lang.Byte
* key3 = 3 java.lang.Byte
* } java.util.TreeMap
*/
}
/**
* emptyIfNull(Map<K,V> map)
* Returns an immutable empty map if the argument is null, or the argument itself otherwise.
*/
@Test
public void test2() {
Map<String, String> map = new LinkedMap();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
Map<String, String> result = MapUtils.emptyIfNull(map);
System.out.println("result = " + result); // result = {key1=value1, key2=value2, key3=value3}
}
/**
* fixedSizeMap(Map<K,V> map)
* Returns a fixed-sized map backed by the given map.
*/
@Test
public void test3() {
Map<String, Object> map = new HashMap<>();
map.put("key1", 1);
map.put("key2", 2);
map.put("key3", 3);
IterableMap<String, Object> result = MapUtils.fixedSizeMap(map);
System.out.println("result = " + result); // result = {key1=1, key2=2, key3=3}
result.put("key3", 4);
System.out.println("result = " + result); // result = {key1=1, key2=2, key3=4}
result.put("key4", 5); // java.lang.IllegalArgumentException: Cannot put new key/value pair - Map is fixed size
}
/**
* fixedSizeSortedMap(SortedMap<K,V> map)
* Returns a fixed-sized sorted map backed by the given sorted map.
*/
@Test
public void test4() {
SortedMap<String, Object> sortedMap = new TreeMap<>();
sortedMap.put("name", "zhenghuixiang");
sortedMap.put("age", "1");
sortedMap.put("sex", "male");
System.out.println("sortedMap = " + sortedMap);
SortedMap<String, Object> result = MapUtils.fixedSizeSortedMap(sortedMap);
result.put("address", "Carton"); // java.lang.IllegalArgumentException: Cannot put new key/value pair - Map is fixed size
System.out.println("result = " + result);
}
/**
* getBoolean(Map<? super K,?> map, K key)
* If the value is a Boolean it is returned directly.
* If the value is a String and it equals 'true' ignoring case then true is returned, otherwise false.
* If the value is a Number an integer zero value returns false and non-zero returns true.
* Otherwise, null is returned.
*/
@Test
public void test5() {
Map<String, Object> map = new HashMap<>();
map.put("booleanValue1", true);
map.put("booleanValue2", false);
map.put("stringValue1", "true");
map.put("stringValue2", "www");
map.put("numberValue1", 1.1f);
map.put("numberValue2", 0);
boolean booleanValue1 = MapUtils.getBoolean(map, "booleanValue1");
boolean booleanValue2 = MapUtils.getBoolean(map, "booleanValue2");
boolean stringValue1 = MapUtils.getBoolean(map, "stringValue1");
boolean stringValue2 = MapUtils.getBoolean(map, "stringValue2");
boolean numberValue1 = MapUtils.getBoolean(map, "numberValue1");
boolean numberValue2 = MapUtils.getBoolean(map, "numberValue2");
String result = String.format("booleanValue1 = %b, " +
"booleanValue2 = %b, " +
"stringValue1 = %b, " +
"stringValue2 = %b, " +
"numberValue1 = %b, " +
"numberValue2 = %b",
booleanValue1,
booleanValue2,
stringValue1,
stringValue2,
numberValue1,
numberValue2);
System.out.println("result = " + result); // result = booleanValue1 = true, booleanValue2 = false, stringValue1 = true, stringValue2 = false, numberValue1 = true, numberValue2 = false
Boolean numberValue3 = MapUtils.getBoolean(map, "numberValue3");
System.out.println("numberValue3 = " + numberValue3); // numberValue3 = null
}
/**
* invertMap(Map<K,V> map)
* Inverts the supplied map returning a new HashMap such that the keys of the input are swapped with the values.
* 反转key - value 为 value - key
*/
@Test
public void test6() {
Map<String, Object> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
System.out.println("map = " + map); // map = {key1=value1, key2=value2, key3=value3}
Map<Object, String> result = MapUtils.invertMap(map);
System.out.println("result = " + result); // result = {value2=key2, value1=key1, value3=key3}
}
/**
* isEmpty(Map<?,?> map)
* Null-safe check if the specified map is empty. Returns: true if empty or null
* size为0或者null对象都返回true
*/
@Test
public void test7() {
Map<String, Object> map = new HashMap<>();
boolean isEmpty = MapUtils.isEmpty(map);
System.out.println("isEmpty = " + isEmpty); // isEmpty = true
isEmpty = MapUtils.isEmpty(null);
System.out.println("isEmpty = " + isEmpty); // isEmpty = true
}
/**
* iterableMap(Map<K,V> map)
* Get the specified Map as an IterableMap.
*/
@Test
public void test8() {
Map<String, Object> map = new TreeMap<>();
map.put("name", "value1");
map.put("age", "value2");
map.put("sex", "value3");
IterableMap<String, Object> result = MapUtils.iterableMap(map);
MapIterator<String, Object> iterator = result.mapIterator();
while (iterator.hasNext()) {
String key = iterator.next();
Object value = iterator.getValue();
System.out.println(String.format("key = %s, value = %s", key, value));
/**
* key = age, value = value2
* key = name, value = value1
* key = sex, value = value3
*/
}
}
/**
* lazyMap(Map<K,V> map, Factory<? extends V> factory)
* Returns a "lazy" map whose values will be created on demand.
*/
@Test
public void test9() {
IterableMap<String, Object> result = MapUtils.lazyMap(new HashMap<>(), (Factory<Object>) Date::new);
result.put("key1", "value1");
result.put("key2", "value2");
result.put("key3", "value3");
Object value = result.get("key4");
System.out.println("value = " + value); // value = Fri Sep 27 16:37:05 CST 2019
}
/**
* lazyMap(Map<K,V> map, Transformer<? super K,? extends V> transformerFactory)
* Returns a "lazy" map whose values will be created on demand.
*/
@Test
public void test10() {
Transformer<String, Integer> transformer = Integer::parseInt;
Map<String, Integer> map = MapUtils.lazyMap(new HashMap<>(), transformer);
Integer integer = map.get("1");
System.out.println("integer = " + integer); // integer = 1
System.out.println("map = " + map); // map = {1=1}
Integer i = transformer.transform("10");
System.out.println("i = " + i); // i = 10
}
/**
* orderedMap(Map<K,V> map)
* Returns a map that maintains the order of keys that are added backed by the given map.
* If a key is added twice, the order is determined by the first add. The order is observed through the keySet, values and entrySet.
* 如果一个键被加入了多次,则以第一次key的位置为准,值依旧会替换
*/
@Test
public void test11() {
OrderedMap<String, Object> result = MapUtils.orderedMap(new HashMap<>());
result.put("name", "value1");
result.put("age", "value2");
result.put("sex", "value3");
result.put("name", "ww");
result.put("name", "ww1");
System.out.println("result = " + result); // result = {name=ww1, age=value2, sex=value3}
}
/**
* populateMap(Map<K,V> map, Iterable<? extends E> elements, Transformer<E,K> keyTransformer, Transformer<E,V> valueTransformer)
* Populates a Map using the supplied Transformers to transform the elements into keys and values.
* 把一个集合按指定的转换规则设置到map里
*/
@Test
public void test12() {
List<String> names = new ArrayList<>();
String[] arr = {"1", "2", "3", "4", "5"};
CollectionUtils.addAll(names, arr);
Map<String, Object> map = new HashMap<>();
MapUtils.populateMap(map, names, e -> {
String key = null;
switch (e) {
case "1":
key = "a";
break;
case "2":
key = "b";
break;
case "3":
key = "c";
break;
case "4":
key = "d";
break;
case "5":
key = "e";
break;
}
return key;
}, Integer::parseInt);
System.out.println("map = " + map); // map = {a=1, b=2, c=3, d=4, e=5}
}
/**
* populateMap(Map<K,V> map, Iterable<? extends V> elements, Transformer<V,K> keyTransformer)
* Populates a Map using the supplied Transformer to transform the elements into keys, using the unaltered element as the value in the Map.
*/
@Test
public void test13() {
List<String> names = new ArrayList<>();
String[] arr = {"1", "2", "3", "4", "5"};
CollectionUtils.addAll(names, arr);
Map<String, Object> map = new HashMap<>();
MapUtils.populateMap(map, names, e -> "key" + e);
System.out.println("map = " + map); // map = {key1=1, key2=2, key5=5, key3=3, key4=4}
}
/**
* populateMap(MultiMap<K,V> map, Iterable<? extends E> elements, Transformer<E,K> keyTransformer, Transformer<E,V> valueTransformer)
* Populates a MultiMap using the supplied Transformers to transform the elements into keys and values.
*/
@Test
public void test14() {
List<String> names = new ArrayList<>();
String[] arr = {"a", "b", "c", "d", "e", "a"};
CollectionUtils.addAll(names, arr);
org.apache.commons.collections.MultiMap map = new MultiValueMap();
MapUtils.populateMap(map, names, e -> e, e -> e);
System.out.println("map = " + map); // map = {a=[a, a], b=[b], c=[c], d=[d], e=[e]}
}
/**
* populateMap(MultiMap<K,V> map, Iterable<? extends V> elements, Transformer<V,K> keyTransformer)
* Populates a MultiMap using the supplied Transformer to transform the elements into keys, using the unaltered element as the value in the MultiMap.
*/
@Test
public void test15() {
List<String> list = Arrays.asList("zheng", "hui", "xiang");
org.apache.commons.collections.MultiMap map = new MultiValueMap();
MapUtils.populateMap(map, list, e -> e);
System.out.println("map = " + map); // map = {xiang=[xiang], zheng=[zheng], hui=[hui]}
}
/**
* predicatedMap(Map<K,V> map, Predicate<? super K> keyPred, Predicate<? super V> valuePred)
* Returns a predicated (validating) map backed by the given map.
* 强校验,判断key和value是否分别符合给定的谓语
*/
@Test
public void test16() {
Map<String, Object> map = new HashMap<>();
map.put("name", "zheng");
map.put("age", "1");
map.put("sex", "male");
IterableMap<String, Object> result = MapUtils.predicatedMap(map, e -> e.length() <= 4, e -> e.toString().length() <= 5);
MapIterator<String, Object> it = result.mapIterator();
while (it.hasNext()) {
String key = it.next();
Object value = it.getValue();
System.out.println("key = " + key + "; value = " + value);
/**
* key = sex; value = male
* key = name; value = zheng
* key = age; value = 1
*/
}
}
/**
* predicatedSortedMap(SortedMap<K,V> map, Predicate<? super K> keyPred, Predicate<? super V> valuePred)
* Returns a predicated (validating) sorted map backed by the given map.
* 强校验
*/
@Test
public void test17() {
SortedMap<String, Object> sortedMap = new TreeMap<>();
sortedMap.put("age", "1");
sortedMap.put("name", "zheng");
sortedMap.put("sex", "male");
SortedMap<String, Object> result = MapUtils.predicatedSortedMap(sortedMap,
e -> e.length() <= 4, e -> e.toString().length() <= 5);
System.out.println("result = " + result); // result = {age=1, name=zheng, sex=male}
}
/**
* putAll(Map<K,V> map, Object[] array)
* Puts all the keys and values from the specified array into the map.
* If the first entry in the object array implements Map.Entry or KeyValue then the key and value are added from that object.
* If the first entry in the object array is an object array itself, then it is assumed that index 0 in the sub-array is the key and index 1 is the value.
* Otherwise, the array is treated as keys and values in alternate indices.
*/
@Test
public void test18() {
// case 1:
Map<String, Object> result1 = MapUtils.putAll(new HashMap<>(), new KeyValue[]{
new DefaultKeyValue<>("name", "zhenghuixiang"),
new DefaultKeyValue<>("age", "1"),
new DefaultKeyValue<>("sex", "male")
});
System.out.println("result1 = " + result1); // result1 = {sex=male, name=zhenghuixiang, age=1}
// case 2:
Map<String, Object> result2 = MapUtils.putAll(new HashMap<>(), new Map.Entry[]{
new DefaultMapEntry<>("name", "zhenghuixiang"),
new DefaultMapEntry<>("age", "1"),
new DefaultMapEntry<>("sex", "male")
});
System.out.println("result2 = " + result2); // result2 = {sex=male, name=zhenghuixiang, age=1}
// case 3: If the first entry in the object array is an object array itself, then it is assumed that index 0 in the sub-array is the key and index 1 is the value.
Map<String, Object> result3 = MapUtils.putAll(new HashMap<>(), new String[][]{
{"name", "zhenghuixiang"},
{"age", "1"},
{"sex", "male"}
});
System.out.println("result3 = " + result3); // result3 = {sex=male, name=zhenghuixiang, age=1}
// case 4: Otherwise, the array is treated as keys and values in alternate indices.
Map<String, Object> result4 = MapUtils.putAll(new HashMap<>(), new String[]{
"name", "zhenghuixiang", "age", "1", "sex", "male"
});
System.out.println("result4 = " + result4); // result4 = {sex=male, name=zhenghuixiang, age=1}
}
/**
* safeAddToMap(Map<? super K,Object> map, K key, Object value)
* Protects against adding null values to a map.
* 注意定义的map的key是? super K
*/
@Test
public void test19() {
Map<String, Object> map = new HashMap<>();
Integer i = null;
String key = "name";
MapUtils.safeAddToMap(map, key, i);
System.out.println("map = " + map); // map = {name=}
}
/**
* size(Map<?,?> map)
* Gets the given map size or 0 if the map is null
*/
@Test
public void test20() {
Map<String, String> map = new HashMap<>();
// case 1:
MapUtils.putAll(map, new String[]{"1", "张三", "2", "李四", "3", "王五"});
int size = MapUtils.size(map);
System.out.println("size = " + size); // size = 3
// case 2:
map.clear();
size = MapUtils.size(map);
System.out.println("size = " + size); // size = 0
// case 3:
map = null;
size = MapUtils.size(map);
System.out.println("size = " + size); // size = 0
}
/**
* toMap(ResourceBundle resourceBundle)
* Creates a new HashMap using data copied from a ResourceBundle.
*/
@Test
public void test21() {
ResourceBundle bundle = ResourceBundle.getBundle("map");
Map<String, Object> result = MapUtils.toMap(bundle);
System.out.println("result = " + result); // result = {sex=male, name=zhenghuixiang, age=1}
}
/**
* toProperties(Map<K,V> map)
* Gets a new Properties object initialised with the values from a Map.
*/
@Test
public void test22() {
Map<String, Object> map = MapUtils.putAll(new HashMap<>(), new Object[]{
"name", "zhenghuixiang", "age", 1, "sex", "male"
});
Properties prop = MapUtils.toProperties(map);
System.out.println("prop = " + prop); // prop = {age=1, name=zhenghuixiang, sex=male}
String name = prop.getProperty("name");
System.out.println("name = " + name); // name = zhenghuixiang
Object age = prop.getProperty("age");
System.out.println("age = " + age); // age = null
age = prop.get("age");
System.out.println("age = " + age); // age = 1
}
/**
* transformedMap(Map<K,V> map, Transformer<? super K,? extends K> keyTransformer, Transformer<? super V,? extends V> valueTransformer)
* Returns a transformed map backed by the given map.
* 已存在的不动。新加入的任何entry都会经过转换器
*/
@Test
public void test23() {
Map<String, Object> map = new HashMap<>();
map.put("name", "zhenghuixiang");
Transformer<String, String> keyT = e -> e + "zhx";
Transformer<Object, Integer> valueT = e -> (Integer) e;
IterableMap<String, Object> result = MapUtils.transformedMap(map, keyT, valueT);
System.out.println("result = " + result); // result = {name=zhenghuixiang}
result.put("age", 1);
System.out.println("result = " + result); // result = {name=zhenghuixiang, agezhx=1}
}
/**
* unmodifiableMap(Map<? extends K,? extends V> map)
* Returns an unmodifiable map backed by the given map.
*/
@Test
public void test24() {
Map<String, Object> map = new HashMap<>();
map.put("name", "zhenghuixiang");
map.put("age", "1");
System.out.println("map = " + map);
Map<String, Object> result = MapUtils.unmodifiableMap(new HashMap<>());
// result.put("name", "zhenghuixiang"); // java.lang.UnsupportedOperationException
result.remove("name"); // java.lang.UnsupportedOperationException
}
/**
* verbosePrint(PrintStream out, Object label, Map<?,?> map)
* Prints the given map with nice line breaks.
*/
@Test
public void test25() {
Map<String, Object> map = MapUtils.putAll(new HashMap<>(), new KeyValue[]{
new DefaultKeyValue<>("name", "zhenghuxiang"),
new DefaultKeyValue<>("age", 1),
new DefaultKeyValue<>("sex", "male")
});
MapUtils.verbosePrint(System.out, "myLabel", map);
/**
* myLabel =
* {
* sex = male
* name = zhenghuxiang
* age = 1
* }
*/
}
。
https://blog.csdn.net/littleluoli/article/details/101543753
MapUtils
最新推荐文章于 2025-05-30 15:42:52 发布