03 Collections
03 Collections
1
“Standard” Collections
Stack Queue
Bag
It is useful to have
libraries of them, so
Grid we don’t have to
write them over and
Hierarchy over again.
Network
2
Collections Differ
• Different types of values
• Different structures
• BAG: No structure – just a mess of values
• LIST, QUEUE, etc: Linear structure of values – the order
matters
• MAP: Set of key-value pairs
• TREE: Hierarchical structures
• ….
• Different access disciplines
• get, put, remove anywhere
• get, put, remove only at the ends, or only at the top, or
…
• get, put, remove by position, or by value, or by key, or
…
3
• ….
Using Collections
• The structure and access discipline of a collection is the
same, regardless of the type of value in it
⇒ The kind of value is not important!
Interface
• List Class that
Class that
• Map implements
uses the
• the collection
Queue collection
• Classes:
• Map: EnumMap, HashMap, LinkedHashMap,TreeMap,
WeakHashMap, ConcurrentHashMap,UIDefaults
public boolean contains(T item); // Does the box contain this item?
public boolean isEmpty(); // Is this box empty?
public int size(); // How many items in the box?
9
Using our BoxADT
…
BoxADT <String> toyBox;
toyBox = new BoxImp<String>();
toyBox.add(“Car”);
toyBox.add(“Plane”);
toyBox.add(“Fred”);
System.out.println(toyBox.contains(“Barbie”));
System.out.println(toyBox.contains(“Fred”));
while(toyBox.size())
System.out.println(toyBox.remove());
System.out.println(toyBox.contains(“Barbie”));
…
10