code: private static int compareSets(Object[] snapshot, Set<?> set) {
          // Uses O(n^2) algorithm, that is only appropriate for small
          // sets, which CopyOnWriteArraySets should be.
          //
          // Optimize up to O(n) if the two sets share a long common prefix,
          // as might happen if one set was created as a copy of the other set.
  
          final int len = snapshot.length;
          // Mark matched elements to avoid re-checking
          final boolean[] matched = new boolean[len];
  
          // j is the largest int with matched[i] true for { i | 0 <= i < j }
          int j = 0;
          outer: for (Object x : set) {
              for (int i = j; i < len; i++) {
                  if (!matched[i] && Objects.equals(x, snapshot[i])) {
                      matched[i] = true;
                      if (i == j)
                          do { j++; } while (j < len && matched[j]);
                      continue outer;
                  }
              }
              return -1;
          }
          return (j == len) ? 0 : 1;
      }
docs: Tells whether the objects in snapshot (regarded as a set) are a
  superset of the given set.
  
  @return -1 if snapshot is not a superset, 0 if the two sets
  contain precisely the same elements, and 1 if snapshot is a
  proper superset of the given set