Guest User

Untitled

a guest
Jul 15th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. public class FlattenedMapEntry {
  2. String key;
  3. String value;
  4. }
  5.  
  6.  
  7. public static List<FlattenedMapEntry> flattenMap(String parent, Map<?, ?> map) {
  8. List<FlattenedMapEntry> results = new ArrayList<>();
  9.  
  10. for (Object o : map.entrySet()) {
  11. Map.Entry pair = (Map.Entry) o;
  12. String key;
  13.  
  14. if (parent == null) {
  15. key = pair.getKey().toString();
  16. } else {
  17. key = parent + "." + pair.getKey().toString();
  18. }
  19.  
  20. if (pair.getValue() instanceof Map<?, ?>) {
  21. results.addAll(flattenMap(key, (Map<?, ?>) pair.getValue()));
  22. } else if (pair.getValue() instanceof Set<?> || pair.getValue() instanceof List<?>) {
  23. List<?> list = (List<?>) pair.getValue();
  24. for (Object aList : list) {
  25. if (aList instanceof Map<?, ?>) {
  26. results.addAll(flattenMap(key, (Map<?, ?>) aList));
  27. } else {
  28. results.add(FlattenedMapEntry.builder().key(key + "." + aList.toString()).value("").build());
  29. }
  30. }
  31. } else if (pair.getValue() != null) {
  32. String value = pair.getValue().toString();
  33. results.add(FlattenedMapEntry.builder().key(key).value(value).build());
  34. }
  35. }
  36. return results;
  37. }
Add Comment
Please, Sign In to add comment