Package algs13

Class Bag<T>

All Implemented Interfaces:

public class Bag<T> extends Object implements Iterable<T>
The Bag class represents a bag (or multiset) of generic items. It supports insertion and iterating over the items in arbitrary order.

The add, isEmpty, and size operation take constant time. Iteration takes time proportional to the number of items.

For additional documentation, see Section 1.3 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.

  • Constructor Details

    • Bag

      public Bag()
      Create an empty stack.
  • Method Details

    • isEmpty

      public boolean isEmpty()
      Is the BAG empty?
    • size

      public int size()
      Return the number of items in the bag.
    • add

      public void add(T item)
      Add the item to the bag.
    • iterator

      public Iterator<T> iterator()
      Return an iterator that iterates over the items in the bag.
      Specified by:
      iterator in interface Iterable<T>
    • main

      public static void main(String[] args)
      A test client.