PropertiesExt { public static Map<String, Map<String, String>> groupByRootName(Map<String, String> properties, int rootParts) { if (CollectionsExt.isNullOrEmpty(properties)) { return Collections.emptyMap(); } Map<String, Map<String, String>> result = new HashMap<>(); for (Map.Entry<String, String> entry : properties.entrySet()) { Pair<String, String> parts = split(entry.getKey(), rootParts); if (parts == null) { continue; } String rootPart = parts.getLeft(); String internalName = parts.getRight(); Map<String, String> internalProps = result.get(rootPart); if (internalProps == null) { result.put(rootPart, internalProps = new HashMap<>()); } internalProps.put(internalName, entry.getValue()); } return result; } private  PropertiesExt(); static Optional<Map<String, String>> loadFromClassPath(String resourceName); static Map<String, Map<String, String>> groupByRootName(Map<String, String> properties, int rootParts); static Map<String, String> getPropertiesOf(String namePrefix, Map<String, String> properties); static List<String> verifyRootNames(Map<String, String> properties, Set<String> expectedRootNames); static Set<String> getRootNames(Collection<String> names, int rootSegments); static Map<String, Set<String>> splitNames(Collection<String> names, int rootSegments); static Pair<String, String> split(String name, int rootParts); static PropertyNode<Boolean> fullSplit(Collection<String> names);  }
@Test public void testSplitByRootName() throws Exception { Map<String, String> properties = asMap( "my.a", "valueA", "my.b", "valueB", "your.x", "valueX", "your.y", "valueY" ); Map<String, Map<String, String>> groups = PropertiesExt.groupByRootName(properties, 1); assertThat(groups).hasSize(2); assertThat(groups.get("my")).containsAllEntriesOf(asMap("a", "valueA", "b", "valueB")); assertThat(groups.get("your")).containsAllEntriesOf(asMap("y", "valueX", "y", "valueY")); }