@Test public void should_map_element_to_hashMap() throws Exception { Content content = (Content) unmarshaller.unmarshal(getClass().getResourceAsStream( "/element_with_complex_data.xml")); Map<String, Object> map = elementMapper.toMap(content.elements); assertThat(map).hasSize(2); assertThat(map.get("metadata")).isNotNull(); assertThat(map.get("locator")).isNotNull(); assertThat((List<String>) map.get("locator")).containsExactlyInAnyOrder("tata", "toto"); List<Map> metadata = (List<Map>) map.get("metadata"); assertThat(metadata).hasSize(2); assertThat(metadata.get(0)).containsEntry("Addressee", newArrayList("a")) .containsEntry("Addressee2", newArrayList("a", "b")); assertThat(metadata.get(1)).containsEntry("Addressee", newArrayList("c")); }
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);  }