fj.data.Set comparison

150 Views Asked by At

In java.util, we can use the containsAll method to compare two java.util.Set. What's the best way to compare two fj.data.Set?

Is there really any "valuable" benefit of using fj over java.util?

2

There are 2 best solutions below

5
On

I have never used that library nor will I ever, but looking through the API I found the method

public final boolean subsetOf(Set<A> s)

Returns true if this set is a subset of the given set.

Parameters: s - A set which is a superset of this set if this method returns true.

Returns: true if this set is a subset of the given set.

I believe this should be used like a "reversed" containsAll:

a.containsAll(b) is true i.f.f. b.subsetOf(a) is true (not sure how equal sets are handled, my guess is that it's fine).

Afterthought: I just noticed how fishy the wording in the javadoc is. The parameter description is dependent on the output: a superset of this set if this method returns true. You're not supposed to assume on the parameter or use a conditional for it. A better wording would be along the lines of: a set to be checked for being a superset.

0
On

To compare two sets use Set.subsetOf. So s1.subsetOf(s2) if s1 is a subset of s2 (in contains terms, s2 contains s1).

To create sets use Set.set which takes an ordering of elements (that is, how to know whether an item is less than another so the set can be stored in a red-black tree).

FunctionalJava uses the functional programming (FP) approach - data is immutable. That makes the Collection interface inappropriate for implementing. Note all the mutation and updating of objects in the Collection interface in methods like add, addAll, remove, etc.

There should be better support for converting to and from java.util.Set, but to convert a fj.data.Set to a java.util class, call set.toStream().toCollection() or set.toList().toJavaList().