0% found this document useful (0 votes)
56 views10 pages

03 Collections

The document discusses different types of data collections including sets, lists, directories, stacks, queues, bags, grids, and hierarchies. It describes how collections can differ in the types of values stored, their underlying structures, and access disciplines. The document introduces abstract data types (ADTs) and how they capture collection properties and operations without specifying implementation details. It provides examples of Java collection interfaces like Collection, Set, List, and Map and how classes implement these interfaces. Finally, it demonstrates defining a BoxADT interface and using it with a BoxImp class.

Uploaded by

api-3799621
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views10 pages

03 Collections

The document discusses different types of data collections including sets, lists, directories, stacks, queues, bags, grids, and hierarchies. It describes how collections can differ in the types of values stored, their underlying structures, and access disciplines. The document introduces abstract data types (ADTs) and how they capture collection properties and operations without specifying implementation details. It provides examples of Java collection interfaces like Collection, Set, List, and Map and how classes implement these interfaces. Finally, it demonstrates defining a BoxADT interface and using it with a BoxImp class.

Uploaded by

api-3799621
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 10

Collections

• What kinds of collections of data do you deal with?


• Phone book: set of (name – phone number) pairs
• Book: a sequence of words, or
a sequence of chapters of paragraphs of words of
letters

1
“Standard” Collections

Set List Directory

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!

• We can capture the structure and access discipline of a


collection in an Abstract Data Type:
• Specify the operations you can perform on the collection

• Specify the properties of these operations

• Don’t say anything about the actual data structure


underneath,
Don’t say how the operations are implemented

• A program can use an Abstract Data Type without caring


about how the ADT is implemented
4
ADT’s in Java
• An Interface specifies an Abstract Data Type (ADT)
• Specifies what methods can be called (but not what they do)
• properties of the methods can only be ‘suggested’ in
comments
× No constructors
× No fields
× No method bodies.
⇒ Your program can
• Create a variable of the type defined by the interface
• Call methods on that variable
× But, your program cannot create an instance of the interface
• A Class is needed to provide an implementation
• Specifies the fields in which data is stored
• Specifies the code for all the methods
⇒ Your program can create an instance of a class
5
Java collections library
• Interfaces:
• Collection
• Set

Interface
• List Class that
Class that
• Map implements
uses the
• the collection
Queue collection

• Classes:
• Map: EnumMap, HashMap, LinkedHashMap,TreeMap,
WeakHashMap, ConcurrentHashMap,UIDefaults

Each implementation has particular advantages


and disadvantages.
6
Using a collection type
• Declare a variable to be of the Interface type
• Specify the type of the collection
• Specify the type of the value that will be stored in the
collection
collection type

List <String> myJobs;


value type
• Create an instance of a class implementing the Interface
type:
• Specify the class
• Specify the type of the value

myJobs = new ArrayList<String>();

• Invoke methods on the instance (an object) to access or


7
modify.
Bigger Example
• TodoList – collection of jobs, in order they should be done.
• Collection type: List of jobs, (jobs represented by a
String)
public class TodoList implements ActionListener{
:
private List<String> myJobs;
:
public void readJobs(){
    try {
      myJobs = new ArrayList<String>();
Scanner sc = new Scanner(new File("myJobs.txt"));
      while ( sc.hasNext() ){       
        myJobs.add(sc.next());
      }
      sc.close();
      displayJobs();
    }
8
Making an ADT
Import java.util.Iterator;

public interface BoxADT<T>{


public boolean add(T item); // Add an item to the box
public T remove(); // Remove any item from the box
public Iterator<T> iterator(); // Return an iterator for the box

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?

public boolean equals (BoxADT<T> otherBox); // Does this box equal


otherBox
public String toString(); // Represent this box as a String.
}

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

You might also like