mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-18 13:10:44 +00:00
Add swapMultiLevelMapKeys
This commit is contained in:
@ -627,4 +627,40 @@ public class CollectionUtils
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
** Take a multi-level map, e.g., Map{String, Map{Integer, BigDecimal}}
|
||||||
|
** and invert it - e.g., to Map{Integer, Map{String, BigDecimal}}
|
||||||
|
*******************************************************************************/
|
||||||
|
public static <K1, K2, V> Map<K2, Map<K1, V>> swapMultiLevelMapKeys(Map<K1, Map<K2, V>> input)
|
||||||
|
{
|
||||||
|
if(input == null)
|
||||||
|
{
|
||||||
|
return (null);
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<K2, Map<K1, V>> output = new HashMap<>();
|
||||||
|
|
||||||
|
for(Map.Entry<K1, Map<K2, V>> entry : input.entrySet())
|
||||||
|
{
|
||||||
|
K1 key1 = entry.getKey();
|
||||||
|
Map<K2, V> map1 = entry.getValue();
|
||||||
|
|
||||||
|
if(map1 != null)
|
||||||
|
{
|
||||||
|
for(Map.Entry<K2, V> entry2 : map1.entrySet())
|
||||||
|
{
|
||||||
|
K2 key2 = entry2.getKey();
|
||||||
|
V value = entry2.getValue();
|
||||||
|
|
||||||
|
output.computeIfAbsent(key2, (k) -> new HashMap<>());
|
||||||
|
output.get(key2).put(key1, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (output);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -34,6 +34,7 @@ import java.util.TreeMap;
|
|||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
import com.kingsrook.qqq.backend.core.BaseTest;
|
import com.kingsrook.qqq.backend.core.BaseTest;
|
||||||
|
import com.kingsrook.qqq.backend.core.utils.collections.MapBuilder;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
@ -594,4 +595,26 @@ class CollectionUtilsTest extends BaseTest
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
@Test
|
||||||
|
void testSwapMultiLevelMapKeys()
|
||||||
|
{
|
||||||
|
Map<String, Map<Integer, String>> input = MapBuilder.of(
|
||||||
|
"A", Map.of(1, "A1", 2, "A2", 3, "A3"),
|
||||||
|
"B", Map.of(1, "B1", 4, "B4"),
|
||||||
|
"C", null);
|
||||||
|
|
||||||
|
Map<Integer, Map<String, String>> output = CollectionUtils.swapMultiLevelMapKeys(input);
|
||||||
|
|
||||||
|
assertEquals(MapBuilder.of(
|
||||||
|
1, Map.of("A", "A1", "B", "B1"),
|
||||||
|
2, Map.of("A", "A2"),
|
||||||
|
3, Map.of("A", "A3"),
|
||||||
|
4, Map.of("B", "B4")), output);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user