/**
   * Removes given value from the set and returns the instance stored in the set or {@code null} if
   * value was not found.
   *
   * @param val Value to remove.
   * @return The instance that was stored in the set or {@code null}.
   */
  @Nullable
  public V removex(V val) {
    A.notNull(val, "val");

    if (comp == null || !strict) {
      for (Iterator<V> it = vals.iterator(); it.hasNext(); ) {
        V v = it.next();

        if (v.equals(val)) {
          it.remove();

          return v;
        }
      }

      return null;
    }

    assert comp != null && strict;

    for (Iterator<V> it = vals.iterator(); it.hasNext(); ) {
      V v = it.next();

      // Prefer equals to comparator.
      if (v.equals(val)) {
        it.remove();

        return v;
      }

      if (comp.compare(v, val) > 0) break;
    }

    return null;
  }
  /** {@inheritDoc} */
  @SuppressWarnings({"unchecked"})
  @Override
  public boolean contains(Object val) {
    A.notNull(val, "val");

    if (comp != null && strict) {
      for (V v : vals) {
        // Prefer equals to comparator.
        if (v.equals(val)) return true;

        if (comp.compare(v, (V) val) > 0) break;
      }

      return false;
    }

    return vals.contains(val);
  }
  /**
   * Gets instance {@code e} stored in this set for which {@code e.equals(val)} returns {@code
   * true}.
   *
   * @param val Value to check for equality.
   * @return Instance stored in this set for which {@code e.equals(val)} returns {@code true}.
   */
  @Nullable
  public V get(V val) {
    A.notNull(val, "val");

    if (comp == null || !strict) {
      for (V v : vals) if (v.equals(val)) return v;

      return null;
    }

    assert comp != null && strict;

    for (V v : vals) {
      // Prefer equals to comparator.
      if (v.equals(val)) return v;

      if (comp.compare(v, val) > 0) break;
    }

    return null;
  }
  /**
   * Either adds a value to set or does nothing if value is already present.
   *
   * @param val Value to add.
   * @return The instance of value from this set or {@code null} if value was added.
   */
  @Nullable
  public V addx(V val) {
    A.notNull(val, "val");

    if (comp == null) {
      for (V v : vals) if (v.equals(val)) return v;

      vals.add(val);

      return null;
    }

    if (strict) {
      for (ListIterator<V> it = vals.listIterator(); it.hasNext(); ) {
        V v = it.next();

        // Prefer equals to comparator.
        if (v.equals(val)) return v;

        int c = comp.compare(v, val);

        if (c == 0) throw new IllegalStateException("Inconsistent equals and compare methods.");

        if (c > 0) {
          // Back up.
          it.previous();

          it.add(val);

          return null;
        }
      }

      vals.add(val);

      return null;
    }

    // Full scan first.
    for (V v : vals) if (v.equals(val)) return v;

    for (ListIterator<V> it = vals.listIterator(); it.hasNext(); ) {
      V v = it.next();

      if (comp.compare(v, val) > 0) {
        do {
          // Back up.
          v = it.previous();
        } while (comp.compare(v, val) == 0);

        it.add(val);

        return null;
      }
    }

    vals.add(val);

    return null;
  }