diff --git a/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/ComparisonMatrix.java b/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/ComparisonMatrix.java index 6f5c0c7f..1c0a9cc0 100644 --- a/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/ComparisonMatrix.java +++ b/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/ComparisonMatrix.java @@ -86,8 +86,9 @@ ComparisonMatrix compare() { List matches = getEqualValues(i); if (matches.size() == 1) { recordMatch(i, matches.get(0)); - } else if (matches.size() > 0) { - // we have more matches, since comparison does not have to be transitive ([1, 2] == [2] == [2, 3]), we have to check all the possibilities + } else if (!matches.isEmpty()) { + // we have more matches, since comparison does not have to be transitive ([1, 2] == [2] == [2, 3]), + // we have to check all the possibilities for (int match : matches) { ComparisonMatrix copy = copy(i + 1); copy.recordMatch(i, match); @@ -114,7 +115,7 @@ private void doSimpleMatching() { for (int i = 0; i < equalElements.size(); i++) { if (!alreadyMatched.get(i)) { List equalTo = equalElements.get(i); - if (equalTo.size() > 0) { + if (!equalTo.isEmpty()) { List equivalentElements = getEquivalentElements(equalTo); // We have the same set matching as is equivalent, we can remove them all diff --git a/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/Node.java b/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/Node.java index a400b9b5..95a64179 100644 --- a/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/Node.java +++ b/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/Node.java @@ -15,11 +15,19 @@ */ package net.javacrumbs.jsonunit.core.internal; + +import org.jetbrains.annotations.NotNull; import java.math.BigDecimal; +import java.util.AbstractMap; import java.util.Collections; import java.util.Iterator; -import java.util.LinkedHashMap; import java.util.LinkedList; +import java.util.Set; +import java.util.Spliterators; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +import static net.javacrumbs.jsonunit.core.internal.JsonUtils.prettyPrint; import static net.javacrumbs.jsonunit.core.internal.JsonUtils.prettyPrint; @@ -33,7 +41,7 @@ enum NodeType implements ValueExtractor { OBJECT("object") { @Override public Object getValue(Node node) { - // custom conversion to map. We want be consistent and native mapping may have different rules for + // custom conversion to map. We want to be consistent and native mapping may have different rules for // serializing numbers, dates etc. return new JsonMap(node); } @@ -214,16 +222,20 @@ interface ValueExtractor { Object getValue(Node node); } - class JsonMap extends LinkedHashMap implements NodeWrapper { + class JsonMap extends AbstractMap implements NodeWrapper { private final Node wrappedNode; JsonMap(Node node) { wrappedNode = node; - Iterator fields = node.fields(); - while (fields.hasNext()) { - KeyValue keyValue = fields.next(); - put(keyValue.getKey(), keyValue.getValue().getValue()); - } + } + + @NotNull + @Override + public Set> entrySet() { + Iterator fields = wrappedNode.fields(); + return StreamSupport.stream(Spliterators.spliteratorUnknownSize(fields, 0), false) + .map(keyValue -> new SimpleEntry<>(keyValue.getKey(), keyValue.getValue().getValue())) + .collect(Collectors.toSet()); } @Override