fork download
  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.Map.Entry;
  4. import java.util.Objects;
  5. import java.util.Set;
  6. import java.util.stream.Collectors;
  7.  
  8. class Ideone {
  9. public static void main(String[] args) {
  10. Map<String, Object> johnDoe = new HashMap<>();
  11. johnDoe.put("name", "John Doe");
  12. Map<String, Object> johnDoesAddress = new HashMap<>();
  13. johnDoesAddress.put("street", "samplestress");
  14. johnDoesAddress.put("city", null);
  15. johnDoesAddress.put("country", "samplecountry");
  16. johnDoe.put("address", johnDoesAddress);
  17.  
  18. Map<String, Object> janeDoe = new HashMap<>();
  19. janeDoe.put("name", "Jane");
  20. Map<String, Object> janeDoesAddress = new HashMap<>();
  21. janeDoesAddress.put("street", "examplestreet");
  22. janeDoesAddress.put("country", "examplecountry");
  23. janeDoe.put("address", janeDoesAddress);
  24.  
  25. System.out.println(areStructuralEqual(johnDoe, janeDoe));
  26. System.out.println(areStructuralEqual(johnDoesAddress, janeDoe));
  27. System.out.println(areStructuralEqual(johnDoe, janeDoesAddress));
  28. System.out.println(areStructuralEqual(johnDoesAddress, janeDoesAddress));
  29. }
  30.  
  31. @SuppressWarnings("unhecked")
  32. public static boolean areStructuralEqual(Map<String, ?> left, Map<String, ?> right) {
  33. Set<Entry<String, ?>> leftEntriesWithNonNullValues =
  34. extractEntriesWithNonNullValues(left);
  35. Set<Entry<String, ?>> rightEntriesWithNonNullValues =
  36. extractEntriesWithNonNullValues(right);
  37. Set<String> leftKeysWithNonNullValues = leftEntriesWithNonNullValues.stream()
  38. .map(Entry::getKey)
  39. .collect(Collectors.toUnmodifiableSet());
  40. Set<String> rightKeysWithNonNullValues = rightEntriesWithNonNullValues.stream()
  41. .map(Entry::getKey)
  42. .collect(Collectors.toUnmodifiableSet());
  43. if (!Objects.equals(leftKeysWithNonNullValues, rightKeysWithNonNullValues)) {
  44. return false;
  45. }
  46.  
  47. Set<String> leftKeysWithMapValues =
  48. extractAllKeysThatMapToMaps(leftEntriesWithNonNullValues);
  49. Set<String> rightKeysWithMapValues =
  50. extractAllKeysThatMapToMaps(rightEntriesWithNonNullValues);
  51. if (!Objects.equals(leftKeysWithMapValues, rightKeysWithMapValues)) {
  52. return false;
  53. }
  54.  
  55. for (String key : leftKeysWithMapValues) {
  56. if (!areStructuralEqual(
  57. (Map<String, ?>) left.get(key),
  58. (Map<String, ?>) right.get(key))) {
  59. return false;
  60. }
  61. }
  62. return true;
  63. }
  64.  
  65. private static Set<String> extractAllKeysThatMapToMaps(Set<Entry<String, ?>> entrySet) {
  66. return entrySet.stream()
  67. .filter(e -> e.getValue() instanceof Map)
  68. .map(Entry::getKey)
  69. .collect(Collectors.toUnmodifiableSet());
  70. }
  71.  
  72. private static Set<Entry<String, ?>> extractEntriesWithNonNullValues(Map<String, ?> map) {
  73. return map.entrySet().stream()
  74. .filter(e -> Objects.nonNull(e.getValue()))
  75. .collect(Collectors.toUnmodifiableSet());
  76. }
  77. }
Success #stdin #stdout 0.08s 34360KB
stdin
Standard input is empty
stdout
true
false
false
true