コード例 #1
0
  /**
   * O(N) Determine if all of the elements of otherSet are in this set. <br>
   * pre: otherSet != null
   *
   * @param otherSet != null
   * @return true if this set contains all of the elements in otherSet, false otherwise.
   */
  public boolean containsAll(ISet<E> otherSet) {
    if (otherSet == null)
      throw new IllegalArgumentException("Illegal Parameter: otherSet cannot be null");
    // we know E is a Comparable, but we don't know if
    // otherSet is a SortedSet.

    // local var of correct type so we only have to cast once
    SortedSet<E> otherSortedSet;

    // do we need to create a new SortedSet based on otherSet or is otherSet
    // really a SortedSet?
    if (!(otherSet instanceof SortedSet<?>))
      otherSortedSet = new SortedSet<E>(otherSet); // should be O(NlogN)
    else otherSortedSet = (SortedSet<E>) otherSet;

    // The difference of the otherSet with this set must be 0 if the
    // otherSet is contained in this set. If there are any elements in
    // otherSet that are not in this set, the difference method will return
    // the values back and the size of the resulting array cannot be 0
    return otherSortedSet.difference(this).size() == 0;
  }