Class SubsetIterator<E>

  • Type Parameters:
    E - the type of element iterated over
    All Implemented Interfaces:
    Iterator<Set<E>>


    public class SubsetIterator<E>
    extends Object
    implements Iterator<Set<E>>
    This iterates over all the possible subsets of an initial set of elements, starting with the smallest and building up to the largest. That is: this identifies all the possible combinations of elements of a set (but it does not address possible permutations or repetition of elements).
    • Constructor Detail

      • SubsetIterator

        public SubsetIterator(Set<E> elements,
                              boolean includeEmptySet)
        Create an iterator to iterate over all subsets of the argument provided. If the elements provided are a SortedSet, then the Comparator used for that set is used to sort results of this class.
        Parameters:
        includeEmptySet - if true then the first value this returns will be an empty set. If false then then first value this returns will have 1 element. (If false and elements is empty: an exception will be thrown.)
        elements - the elements to create subsets of. Note this class continues to reference this argument, so you should not modify this set once this iterator is constructed.
      • SubsetIterator

        public SubsetIterator(Set<E> elements,
                              Comparator<E> comparator,
                              int minSetSize,
                              int maxSetSize)
        Parameters:
        elements - the elements to create subsets of. Note this class continues to reference this argument, so you should not modify this set once this iterator is constructed.
        comparator - an optional comparator to sort the returned results.
        minSetSize - the smallest subset size to return. This may be [zero, max].
        maxSetSize - the largest subset size to return. This must be larger than the minimum, and not larger than elements.size().
    • Method Detail

      • hasNext

        public boolean hasNext()
      • next

        public Set<E> next()
      • remove

        public void remove()
        Throws an UnsupportedOperationException.