Class Bag<E>

java.lang.Object
tripleplay.util.Bag<E>
All Implemented Interfaces:
Iterable<E>

public class Bag<E> extends Object implements Iterable<E>
An unordered collection of elements which may contain duplicates. Elements must not be null. The elements will be reordered during normal operation of the bag. This is optimized for fast additions, removals and iteration. It is not optimized for programmer ass coverage; see the warnings below.

Note: extra bounds checking is not performed which means that some invalid operations will succeeed and return null rather than throwing IndexOutOfBoundsException. Be careful.

Note: the iterator returned by iterator() does not make concurrent modification checks, so concurrent modifications will cause unspecified behavior. Don't do that.

  • Constructor Summary

    Constructors
    Constructor
    Description
    Bag()
    Creates a bag with a default initial capacity of 16.
    Bag(int initialCapacity)
    Creates a bag with the specified initial capacity.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    add(E elem)
    Adds elem to this bag.
    boolean
    contains(E elem)
    Returns whether this bag contains elem.
    boolean
    contains(react.Function<E,Boolean> pred)
    Returns whether this bag contains at least one element matching pred.
    static <E> Bag<E>
    Creates an empty bag.
    static <E> Bag<E>
    create(int initialCapacity)
    Creates an empty bag with the specified initial capacity.
    final E
    get(int index)
    Returns the element at index.
    boolean
    Returns whether this bag is empty.
     
    boolean
    remove(E elem)
    Removes the first occurrance of elem from the bag.
    void
    Removes all elements from this bag.
    removeAt(int index)
    Removes the element at the specified index.
    Removes and returns the last element of the bag.
    boolean
    removeWhere(react.Function<E,Boolean> pred)
    Removes all elements that match pred.
    int
    Returns the number of elements in this bag.
     

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Constructor Details

    • Bag

      public Bag()
      Creates a bag with a default initial capacity of 16.
    • Bag

      public Bag(int initialCapacity)
      Creates a bag with the specified initial capacity.
  • Method Details

    • create

      public static <E> Bag<E> create()
      Creates an empty bag. This allows one to avoid repeating the parameter type: Bag<Foo> bag = Bag.create();
    • create

      public static <E> Bag<E> create(int initialCapacity)
      Creates an empty bag with the specified initial capacity. This allows one to avoid repeating the parameter type: Bag<Foo> bag = Bag.create();
    • size

      public int size()
      Returns the number of elements in this bag.
    • isEmpty

      public boolean isEmpty()
      Returns whether this bag is empty.
    • get

      public final E get(int index)
      Returns the element at index.
    • contains

      public boolean contains(E elem)
      Returns whether this bag contains elem. Equality is by reference.
    • contains

      public boolean contains(react.Function<E,Boolean> pred)
      Returns whether this bag contains at least one element matching pred.
    • add

      public int add(E elem)
      Adds elem to this bag. The element will always be added to the end of the bag.
      Returns:
      the index at which the element was added.
    • removeAt

      public E removeAt(int index)
      Removes the element at the specified index.
      Returns:
      the removed element.
    • remove

      public boolean remove(E elem)
      Removes the first occurrance of elem from the bag. Equality is by reference.
      Returns:
      true if elem was found and removed, false if not.
    • removeWhere

      public boolean removeWhere(react.Function<E,Boolean> pred)
      Removes all elements that match pred.
      Returns:
      true if at least one element was found and removed, false otherwise.
    • removeLast

      public E removeLast()
      Removes and returns the last element of the bag.
      Throws:
      ArrayIndexOutOfBoundsException - if the bag is empty.
    • removeAll

      public void removeAll()
      Removes all elements from this bag.
    • iterator

      public Iterator<E> iterator()
      Specified by:
      iterator in interface Iterable<E>
    • toString

      public String toString()
      Overrides:
      toString in class Object