Skip to content

Commit

Permalink
fixes neo4j-contrib#151 coll.isEqualCollection compare 2 collections …
Browse files Browse the repository at this point in the history
…in any order - null checks
  • Loading branch information
vboulaye committed Oct 27, 2019
1 parent 749224f commit 9e96525
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/java/apoc/coll/Coll.java
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,8 @@ public boolean containsAllSorted(@Name("coll") List<Object> coll, @Name("values"
@UserFunction
@Description("apoc.coll.isEqualCollection(coll, values) return true if two collections contain the same elements with the same cardinality in any order (using a HashMap)")
public boolean isEqualCollection(@Name("coll") List<Object> first, @Name("values") List<Object> second) {
if (first == null || first.isEmpty() || first.size() != second.size()) return false;
if (first == second) return true;
if (first == null || second == null || first.size() != second.size()) return false;

Map<Object, Long> map1 = first.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/apoc/coll/CollTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ public void testIsEqualCollection() throws Exception {
testCall(db, "RETURN apoc.coll.isEqualCollection([1,2,3],[]) AS value", (res) -> assertEquals(false, res.get("value")));
testCall(db, "RETURN apoc.coll.isEqualCollection([1,2,3],[1]) AS value", (res) -> assertEquals(false, res.get("value")));
testCall(db, "RETURN apoc.coll.isEqualCollection([1,2,3],[1,2,3,4]) AS value", (res) -> assertEquals(false, res.get("value")));
testCall(db, "RETURN apoc.coll.isEqualCollection([1,2,3],null) AS value", (res) -> assertEquals(false, res.get("value")));
testCall(db, "RETURN apoc.coll.isEqualCollection([1,2,3],[]) AS value", (res) -> assertEquals(false, res.get("value")));
testCall(db, "RETURN apoc.coll.isEqualCollection([],null) AS value", (res) -> assertEquals(false, res.get("value")));
testCall(db, "RETURN apoc.coll.isEqualCollection([],[]) AS value", (res) -> assertEquals(true, res.get("value")));
testCall(db, "RETURN apoc.coll.isEqualCollection(null,null) AS value", (res) -> assertEquals(true, res.get("value")));
testCall(db, "RETURN apoc.coll.isEqualCollection(null,[]) AS value", (res) -> assertEquals(false, res.get("value")));
}

@Test
Expand Down

0 comments on commit 9e96525

Please sign in to comment.