Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FlattenedMapEntry {
- String key;
- String value;
- }
- public static List<FlattenedMapEntry> flattenMap(String parent, Map<?, ?> map) {
- List<FlattenedMapEntry> results = new ArrayList<>();
- for (Object o : map.entrySet()) {
- Map.Entry pair = (Map.Entry) o;
- String key;
- if (parent == null) {
- key = pair.getKey().toString();
- } else {
- key = parent + "." + pair.getKey().toString();
- }
- if (pair.getValue() instanceof Map<?, ?>) {
- results.addAll(flattenMap(key, (Map<?, ?>) pair.getValue()));
- } else if (pair.getValue() instanceof Set<?> || pair.getValue() instanceof List<?>) {
- List<?> list = (List<?>) pair.getValue();
- for (Object aList : list) {
- if (aList instanceof Map<?, ?>) {
- results.addAll(flattenMap(key, (Map<?, ?>) aList));
- } else {
- results.add(FlattenedMapEntry.builder().key(key + "." + aList.toString()).value("").build());
- }
- }
- } else if (pair.getValue() != null) {
- String value = pair.getValue().toString();
- results.add(FlattenedMapEntry.builder().key(key).value(value).build());
- }
- }
- return results;
- }
Add Comment
Please, Sign In to add comment