Methods for Bag Interface

 

 

Bag Interface

int

add (Object item)

Adds the specified item to this bag and returns the number of instances of the item now in the bag.  Throws an exception if the item is null.

void

clear ()

Removes all items from this bag.

Object

clone ()

Returns a shallow copy of this bag.

Collection

collectionView ()

Returns a collection view of the items contained in this bag.  Each item appears only once, even if the bag contains multiple instances of it.

boolean

contains (Object item)

Returns true if the item is in this bag.   Throws an exception if the item is null.

boolean

equals (Object other)

Returns true if the other object is a bag that equals this bag.  To be equal two bags must contain equal items with equal frequencies.  Order does not matter.

int

getCount (Object item)

Returns the number of instances of the specified item in this bag.  Throws an exception if the item is null.

int

hashCode ()

Returns the hash code value for this bag.

boolean

isEmpty ()

Returns true if the set  is empty.

Iterator

iterator ()

Returns an iterator over the items contained in this bag.  Each item appears only once, even if the bag contains multiple instances of it.  The items appear in no particular order.  If the iterator supports removal, it should remove all instances of an item at once.

int

remove (Object item)

Tries to removes one instance of the specified item from this bag.  If the item is present, returns the number of instance remaining, else returns -1.  Throws an exception if the item is null.

int

size ()

Returns the number of items in this bag.  Each instance of an item counts toward the total.

String

toString ()

Returns a string that lists the items in the bag plus the count for each.  The list is not sorted.

 

martin@cc.wwu.edu
Disclaimer

Copyright Martin Osborne 1998-2001
  All rights reserved