Vavr: implement diff for map

Created on 22 Mar 2020  路  4Comments  路  Source: vavr-io/vavr

Set::diff is really helpful for calculating set difference, unfortunately, unlike Guava which has Maps.difference, there is nothing in Map.

question

Most helpful comment

I am probably not the right person to make the decision so I will let core maintainers to expression their ideas. Meanwhile, I hope the following code snippet could unblock you for map usage. (I never used Guava so I hope I didn't make any mistake):

// Guava Maps#difference() equivalence in VAVR 1.0.0-alpha-3

Map<String, String> mapA = HashMap.of("a1", "1", "c1", "1", "c2", "2");
Map<String, String> mapB = HashMap.of("b1", "1", "c1", "1", "c2", "3");

// MapDifference#areEqual
boolean areEqual = mapA.equals(mapB);
assertThat(areEqual).isFalse();
assertThat(mapA.equals(HashMap.of("a1", "1", "c1", "1", "c2", "2"))).isTrue();

// MapDifference#entriesOnlyOnLeft
Map<String, String> entriesOnlyOnLeft = mapA.removeAll(mapB.keySet());
assertThat(entriesOnlyOnLeft).isEqualTo(HashMap.of("a1", "1"));

// MapDifference#entriesOnlyOnRight
Map<String, String> entriesOnlyOnRight = mapB.removeAll(mapA.keySet());
assertThat(entriesOnlyOnRight).isEqualTo(HashMap.of("b1", "1"));

// MapDifference#entriesInCommon
Map<String, String> entriesInCommon = mapA.retainAll(mapB);
assertThat(entriesInCommon).isEqualTo(HashMap.of("c1", "1"));

// MapDifference#entriesDiffering
// no, there is not... :/

All 4 comments

Hello @carnott-snap , do you know if there is Maps.difference or similar methods in Scala? I did a quick search on https://github.com/scala/scala but I didn't find any. I think the goal of VAVR is not to compare with Guava, but to align with Scala as much as possible.

I do not have experience with the scala stdlib, and was not aware vavr was tracking the scala stdlib. I just the need for this transform, and somebody commented that this exists in guava, so I started there. Do you think the feature has merit on its own, or is sticking to the scala api more important?

I am probably not the right person to make the decision so I will let core maintainers to expression their ideas. Meanwhile, I hope the following code snippet could unblock you for map usage. (I never used Guava so I hope I didn't make any mistake):

// Guava Maps#difference() equivalence in VAVR 1.0.0-alpha-3

Map<String, String> mapA = HashMap.of("a1", "1", "c1", "1", "c2", "2");
Map<String, String> mapB = HashMap.of("b1", "1", "c1", "1", "c2", "3");

// MapDifference#areEqual
boolean areEqual = mapA.equals(mapB);
assertThat(areEqual).isFalse();
assertThat(mapA.equals(HashMap.of("a1", "1", "c1", "1", "c2", "2"))).isTrue();

// MapDifference#entriesOnlyOnLeft
Map<String, String> entriesOnlyOnLeft = mapA.removeAll(mapB.keySet());
assertThat(entriesOnlyOnLeft).isEqualTo(HashMap.of("a1", "1"));

// MapDifference#entriesOnlyOnRight
Map<String, String> entriesOnlyOnRight = mapB.removeAll(mapA.keySet());
assertThat(entriesOnlyOnRight).isEqualTo(HashMap.of("b1", "1"));

// MapDifference#entriesInCommon
Map<String, String> entriesInCommon = mapA.retainAll(mapB);
assertThat(entriesInCommon).isEqualTo(HashMap.of("c1", "1"));

// MapDifference#entriesDiffering
// no, there is not... :/

slick, I was just going to use the guava logic and convert everything, but this is probably better. Plus I can just directly upstream the logic if that is the route you end up taking.

Assuming we do, you think the interface should be like guava, where a complex object that can be interrogated is returned, or more building block methods like left.difference(right), right.difference(left), left.union(right), mapping to your entriesOnlyOnLeft, entriesOnlyOnRight, and entriesInCommon respectively?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

paplorinc picture paplorinc  路  6Comments

rumatoest picture rumatoest  路  5Comments

Vivek-Patil picture Vivek-Patil  路  3Comments

HiDAl picture HiDAl  路  3Comments

liviamoroianu picture liviamoroianu  路  3Comments