A Bag data structure stores an unordered collection of values and allows multiple copies of the same value. Bags know how many occurrences (i.e., count) of each distinct value are in its collection. Adding a member increases the number of instances in the bag by one and removing a member decreases the number of instances in the bag by one. The member is deleted from the bag when it has no instances.
In this implementation of a Bag ADT, a bag is of a Comparable
type and is homogenous. Its operations include:
add()
addWithOccurences()
contains()
remove()
size()
isEmpty()
iterator()
toString()
removeAllCopies()
subtract()
The Bag has been implemented using a dictionary(HashMap), a Linked list and an array.
The final build is a Maven project. Clone this repo and use your IDE to process the Maven .pom file. This may take a little while as the IDE will download all the dependencies needed. Experiment with the ADT and run main
to understand its current functionality.