@Test public void testIterator () { Bag<Integer> bag = Bag.create(); Set<Integer> values = new HashSet<Integer>(); values.add(5); values.add(10); values.add(25); for (Integer elem : values) bag.add(elem); assertEquals(values.size(), bag.size()); for (Integer elem : bag) assertTrue(values.remove(elem)); assertTrue(values.isEmpty()); }
@Override public Iterator<E> iterator () { return new Iterator<E>() { @Override public boolean hasNext() { return _pos < _size; } @Override public E next () { return get(_pos++); } @Override public void remove () { Bag.this.removeAt(--_pos); } protected int _pos; }; }
Bag implements Iterable<E> { @Override public Iterator<E> iterator () { return new Iterator<E>() { @Override public boolean hasNext() { return _pos < _size; } @Override public E next () { return get(_pos++); } @Override public void remove () { Bag.this.removeAt(--_pos); } protected int _pos; }; } }
Bag implements Iterable<E> { @Override public Iterator<E> iterator () { return new Iterator<E>() { @Override public boolean hasNext() { return _pos < _size; } @Override public E next () { return get(_pos++); } @Override public void remove () { Bag.this.removeAt(--_pos); } protected int _pos; }; } Bag();  Bag(int initialCapacity); }
Bag implements Iterable<E> { @Override public Iterator<E> iterator () { return new Iterator<E>() { @Override public boolean hasNext() { return _pos < _size; } @Override public E next () { return get(_pos++); } @Override public void remove () { Bag.this.removeAt(--_pos); } protected int _pos; }; } Bag();  Bag(int initialCapacity); static Bag<E> create(); static Bag<E> create(int initialCapacity); int size(); boolean isEmpty(); final E get(int index); boolean contains(E elem); boolean contains(Function<E,Boolean> pred); int add(E elem); E removeAt(int index); boolean remove(E elem); boolean removeWhere(Function<E,Boolean> pred); E removeLast(); void removeAll(); @Override Iterator<E> iterator(); @Override String toString(); }
Bag implements Iterable<E> { @Override public Iterator<E> iterator () { return new Iterator<E>() { @Override public boolean hasNext() { return _pos < _size; } @Override public E next () { return get(_pos++); } @Override public void remove () { Bag.this.removeAt(--_pos); } protected int _pos; }; } Bag();  Bag(int initialCapacity); static Bag<E> create(); static Bag<E> create(int initialCapacity); int size(); boolean isEmpty(); final E get(int index); boolean contains(E elem); boolean contains(Function<E,Boolean> pred); int add(E elem); E removeAt(int index); boolean remove(E elem); boolean removeWhere(Function<E,Boolean> pred); E removeLast(); void removeAll(); @Override Iterator<E> iterator(); @Override String toString();  }