diff --git a/src/main/java/org/apache/commons/collections4/MapUtils.java b/src/main/java/org/apache/commons/collections4/MapUtils.java index 0476437132..a379c89e17 100644 --- a/src/main/java/org/apache/commons/collections4/MapUtils.java +++ b/src/main/java/org/apache/commons/collections4/MapUtils.java @@ -25,6 +25,8 @@ import java.util.Deque; import java.util.Enumeration; import java.util.HashMap; +import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Objects; @@ -856,6 +858,57 @@ public static long getLongValue(final Map map, final K key, fi return applyDefaultValue(map, key, MapUtils::getLong, defaultValue).longValue(); } + /** + * Gets a List from a Map in a null-safe manner. + *

+ * If the value returned from the specified list is not a List then {@code null} is returned. + *

+ * + * @param the key type + * @param map the map to use + * @param key the key to look up + * @return the value in the Map as a List, {@code null} if null map input + */ + public static List getList(final Map map, final K key) { + if (map != null) { + final Object answer = map.get(key); + if (answer instanceof List) { + return (List) answer; + } + } + return null; + } + + /** + * Looks up the given key in the given map, converting the result into a list, using the defaultFunction to produce + * the default value if the conversion fails. + * + * @param the key type + * @param map the map whose value to look up + * @param key the key of the value to look up in that map + * @param defaultFunction what to produce the default value if the value is null or if the conversion fails + * @return the value in the map as a List, or defaultValue produced by the defaultFunction if the original value + * is null, the map is null or the List conversion fails + */ + public static List getList(final Map map, final K key, final Function> defaultFunction) { + return applyDefaultFunction(map, key, MapUtils::getList, defaultFunction); + } + + /** + * Looks up the given key in the given map, converting the result into a List, using the default value if the + * conversion fails. + * + * @param the key type + * @param map the map whose value to look up + * @param key the key of the value to look up in that map + * @param defaultValue what to return if the value is null or if the conversion fails + * @return the value in the map as a List, or defaultValue if the original value is null, the map is null or the + * List conversion fails + */ + public static List getList(final Map map, final K key, final List defaultValue) { + return applyDefaultValue(map, key, MapUtils::getList, defaultValue); + } + /** * Gets a Map from a Map in a null-safe manner. *

@@ -885,7 +938,7 @@ public static long getLongValue(final Map map, final K key, fi * @param map the map whose value to look up * @param key the key of the value to look up in that map * @param defaultFunction what to produce the default value if the value is null or if the conversion fails - * @return the value in the map as a number, or defaultValue produced by the defaultFunction if the original value + * @return the value in the map as a Map, or defaultValue produced by the defaultFunction if the original value * is null, the map is null or the map conversion fails * @since 4.5 */ @@ -902,7 +955,7 @@ public static long getLongValue(final Map map, final K key, fi * @param map the map whose value to look up * @param key the key of the value to look up in that map * @param defaultValue what to return if the value is null or if the conversion fails - * @return the value in the map as a number, or defaultValue if the original value is null, the map is null or the + * @return the value in the map as a Map, or defaultValue if the original value is null, the map is null or the * map conversion fails */ public static Map getMap(final Map map, final K key, final Map defaultValue) { diff --git a/src/test/java/org/apache/commons/collections4/MapUtilsTest.java b/src/test/java/org/apache/commons/collections4/MapUtilsTest.java index 66b61c71d7..2fee53dae5 100644 --- a/src/test/java/org/apache/commons/collections4/MapUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/MapUtilsTest.java @@ -1223,6 +1223,19 @@ public void testGetBooleanValue() { assertTrue(MapUtils.getBoolean(inStr, "str1", true)); } + @Test + public void testGetList() { + final Map> in = new HashMap<>(); + final List valList = new ArrayList<>(); + valList.add("value1"); + in.put("key1", valList); + final List outValue = MapUtils.getList(in, "key1", (List) null); + + assertEquals("value1", outValue.get(0)); + assertNull(MapUtils.getList(in, "key2", (List) null)); + assertNull(MapUtils.getList(null, "key2", (List) null)); + } + @Test public void testGetMap() { final Map> in = new HashMap<>();