@Test public void should_map_element_with_complex_empty_tree() throws Exception { String xml = "<Content>" + "<Titles>" + "<EmptyValueA></EmptyValueA>" + "<EmptyValueB></EmptyValueB>" + "<ValueC>" + "<Argument></Argument>" + "<MyTitles>" + "<Argument></Argument>" + "<RealTiTle>Batman</RealTiTle>" + "</MyTitles>" + "</ValueC>" + "</Titles>" + "</Content>"; Content content = getXMLFromString(xml, Content.class); Map<String, Object> elements = elementMapper.toMap(content.elements); assertThat(elements.toString()).isEqualTo("{Titles=[{EmptyValueB=[], EmptyValueA=[], ValueC=[{Argument=[], MyTitles=[{RealTiTle=[Batman], Argument=[]}]}]}]}"); }
public Map<String, Object> toMap(List<Object> elements) { Map<String, List<Object>> collect = elements.stream() .filter(item -> item instanceof Element) .map(item -> (Element) item) .map(item -> new SimpleImmutableEntry<>(item.getLocalName(), elementToMap(item))) .collect(Collectors.toMap( SimpleImmutableEntry::getKey, SimpleImmutableEntry::getValue, (o, o2) -> Stream.concat(o.stream(), o2.stream()).collect(Collectors.toList()))); return (Map) collect; }
ElementMapper { public Map<String, Object> toMap(List<Object> elements) { Map<String, List<Object>> collect = elements.stream() .filter(item -> item instanceof Element) .map(item -> (Element) item) .map(item -> new SimpleImmutableEntry<>(item.getLocalName(), elementToMap(item))) .collect(Collectors.toMap( SimpleImmutableEntry::getKey, SimpleImmutableEntry::getValue, (o, o2) -> Stream.concat(o.stream(), o2.stream()).collect(Collectors.toList()))); return (Map) collect; } }
ElementMapper { public Map<String, Object> toMap(List<Object> elements) { Map<String, List<Object>> collect = elements.stream() .filter(item -> item instanceof Element) .map(item -> (Element) item) .map(item -> new SimpleImmutableEntry<>(item.getLocalName(), elementToMap(item))) .collect(Collectors.toMap( SimpleImmutableEntry::getKey, SimpleImmutableEntry::getValue, (o, o2) -> Stream.concat(o.stream(), o2.stream()).collect(Collectors.toList()))); return (Map) collect; }  }
ElementMapper { public Map<String, Object> toMap(List<Object> elements) { Map<String, List<Object>> collect = elements.stream() .filter(item -> item instanceof Element) .map(item -> (Element) item) .map(item -> new SimpleImmutableEntry<>(item.getLocalName(), elementToMap(item))) .collect(Collectors.toMap( SimpleImmutableEntry::getKey, SimpleImmutableEntry::getValue, (o, o2) -> Stream.concat(o.stream(), o2.stream()).collect(Collectors.toList()))); return (Map) collect; }  Map<String, Object> toMap(List<Object> elements); }
ElementMapper { public Map<String, Object> toMap(List<Object> elements) { Map<String, List<Object>> collect = elements.stream() .filter(item -> item instanceof Element) .map(item -> (Element) item) .map(item -> new SimpleImmutableEntry<>(item.getLocalName(), elementToMap(item))) .collect(Collectors.toMap( SimpleImmutableEntry::getKey, SimpleImmutableEntry::getValue, (o, o2) -> Stream.concat(o.stream(), o2.stream()).collect(Collectors.toList()))); return (Map) collect; }  Map<String, Object> toMap(List<Object> elements);  }