Skip to main content
Skip table of contents

extractMapValue

Versionshinweise

25.6

Code

GROOVY

Object extractMapValue(Map<?, ?> dataMap, Object... key);

Beschreibung

Die Methode liest einen Wert aus der gegebenen Map, indem anhand der Keys tiefer in die Map navigiert wird.
Jeder Key muss auf eine Map verweisen, außer dem letzen. Verweist einer der Keys (bis auf den letzten) nicht auf eine Map, so wird null zurückgegeben.
Der letzte Key kann auf ein beliebiges Objekt verweisen, dieses wird dann zurückgegeben.
Wird ein Key nicht an der entsprechenden Stelle gefunden, wird null zurückgegeben.

Parameter

  • dataMap - Die Map, aus der der Wert gelesen werden soll.

  • key - Die Keys, die den Weg zum gesuchten Wert in der Map beschreiben.

Rückgabe

Der über die Keys referenzierte Wert der dataMap.

Beispiele

GROOVY

GROOVY
Map<String, Object> m1 = new HashMap();
m1.put("k1Test1IntVal", 10);

Map<String, Object> m2 = new HashMap();
m2.put("k2Test1StringVal", "TestStringVal1");
m2.put("k2Test2StringVal", "TestStringVal2");
m2.put("k2Test3MapVal", m1);

Map<String, Object> m3 = new HashMap();
m3.put("k3Test1DoubleVal", 3.5d);
m3.put("k3Test2StringVal", "TestStringVal3");
m3.put("k3Test3MapVal", m2);
m3.put("k3Test3NullVal", null);

Assertions.assertEquals(10, LogicScriptUtils.extractMapValue(m1, "k1Test1IntVal"), "Test 1 read key in simple Map");
Assertions.assertEquals(10, LogicScriptUtils.extractMapValue(m2, "k2Test3MapVal", "k1Test1IntVal"), "Test 2 read key in Map contained in Map");
Assertions.assertEquals(10, LogicScriptUtils.extractMapValue(m3, "k3Test3MapVal", "k2Test3MapVal", "k1Test1IntVal"), "Test 3 read key in Map contained in Map contained in Map");
Assertions.assertEquals("TestStringVal1", LogicScriptUtils.extractMapValue(m3, "k3Test3MapVal", "k2Test1StringVal"), "Test 4 read String value of key in Map contained in Map");
Assertions.assertEquals(m1, LogicScriptUtils.extractMapValue(m3, "k3Test3MapVal", "k2Test3MapVal"), "Test 5 read Map value of key in Map in Map");
Assertions.assertEquals("TestStringVal3", LogicScriptUtils.extractMapValue(m3, "k3Test2StringVal"), "Test 6 read String value of key in Map");
Assertions.assertNull(LogicScriptUtils.extractMapValue(m3, "k3Test2StringVal", "I do not exist"), "Test 7 try to read value of a key containing no Map");
Assertions.assertNull(LogicScriptUtils.extractMapValue(m3, "k3Test3NullVal"), "Test 8 read null value of a key in Map");
Assertions.assertNull(LogicScriptUtils.extractMapValue(m3, "k3Test3NullVal", "I do not exist"), "Test 9 try to read value of a non-existing key in Map");

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.