1

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?

Sarah A
  • 1,185
  • 12
  • 27
  • what is `fj.data.Set`? Are you using libraries? – exception1 Jan 28 '16 at 14:07
  • @exception1 I think it's a library called functionaljava... haven't really had much contact but i'm always very suspicious about librarys that are reimplementing java basics like arrays and lists – ParkerHalo Jan 28 '16 at 14:08
  • @exception1 I am totally with you on that! unfortunately I am working on a codebase that's already using this "functional-java" heavily and I have to finish some code by tomorrow :/ once that's done I will have more time to go back and change everything! But for now I need to find the best way to compare two sets without writing a whole new method to do the job of ```containsAll``` – Sarah A Jan 28 '16 at 14:19
  • I can't really help because I haven't ever used this library. It seems to have nice features for functional approaches. But `fj.data.Set` and `java.util.Set` are incompatible and I can't find any conversion functions or something similar. At least, the fj data types implement the`Iterable`interface (the native one - there is also a fj interface) - so you can iterate and check the entries yourself. (Unfortunately, the don't implement `Collection` - would have made things a much easier) – exception1 Jan 28 '16 at 14:42
  • that was pretty much my conclusion, I was hoping some magic answer comes through! – Sarah A Jan 28 '16 at 14:43

2 Answers2

1

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.

Community
  • 1
  • 1
user1803551
  • 12,965
  • 5
  • 47
  • 74
0

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().

Mark Perry
  • 1,656
  • 1
  • 9
  • 6