Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.

Similar presentations


Presentation on theme: "Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different."— Presentation transcript:

1 Introduction to Collections

2 Collections Collections provide a way of organizing related data in a model Different types of collections have different patterns of organization and operations

3 Collections Formal properties –What are they? –How are they organized and classified?

4 Collections Formal properties –What are they? –How are they organized and classified? Implementations –How are they represented? –What are the performance trade-offs?

5 Collections Formal properties –What are they? –How are they organized and classified? Implementations –How are they represented? –What are the performance trade-offs? Applications –What are they good for?

6 Categories of Collections Linear Hierarchical Graph Unordered

7 Linear Collections Vector List Stack Queue D1D2D3 0 1 2 Elements are ordered by position Each element except the first has a unique predecessor Each element except the last has a unique successor

8 Linear Collections Vector List Stack Queue D1D2D3 0 1 2 Vectors and lists allow insertions and removals at any position

9 Linear Collections Vector List Stack Queue D1D2D3 0 1 2 Stacks allow insertions and removals at one end only

10 Linear Collections Vector List Stack Queue D1D2D3 0 1 2 Queues allow insertions at one end and removals at the the other end

11 Hierarchical Collections Binary tree General tree Binary search tree Heap D1 D2 D3 Each element except the root has a unique predecessor Each can have zero or more successors

12 Graph Collections Undirected graph Directed graph D4 D3 D5 D1D2 Each element can have zero or more predecessors Each can have zero or more successors

13 Unordered Collections Set Bag Map (table) D4 D3 D5 D1D2

14 A First Example: Tiny The Tiny collection is quite simple –It’s linear –We add items to the end –We remove them from the end –We can check the size –We can see if it’s full –We can obtain a copy (clone)

15 constructor public void add(Object obj) public Object removeLast() public int size() public boolean isFull() public Object clone() public Iterator iterator() The Tiny Interface With the exception of strings and bitsets, collections contain objects

16 // Create a Tiny collection and assign it to tcol // Add some objects tcol.add("a string"); tcol.add(new Employee()); // Be more careful! if (! tcol.isFull()) tcol.add(new Circle()); // Remove and display all objects while (tcol.size() > 0){ Object obj = tcol.removeLast(); System.out.println(obj.toString()); } Using a Tiny Collection

17 // Precondition: the collection is not full // Postondition: obj is added to the end of the collection public void add(Object obj) // Precondition: the collection is not empty // Postondition: the last object is removed from the end of // the collection and returned to the client public Object removeLast() // Returns the number of objects currently in the collection public int size() // Returns true if the collection is full or false otherwise public boolean isFull() // Returns a shallow copy of the collection public Object clone() The Tiny Interface Use prefatory comments to specify pre and postconditions

18 // Precondition: the collection is not full // Postondition: obj is added to the end of the collection // Throws an IllegalStateException if the collection is full public void add(Object obj) // Precondition: the collection is not empty // Postondition: the last object is removed from the end of // the collection and returned to the client // Throws an IllegalStateException if the collection is empty public Object removeLast() Exceptional Conditions

19 Import java.util.Iterator; public interface Tiny{ public void add(Object obj); public Object removeLast(); public int size(); public boolean isFull(); public Object clone(); public Iterator iterator(); } Define a Java Interface

20 The Tiny Implementation Two alternatives –Array –Linked structure Each implementation is a class that implements the Tiny interface

21 Multiple Implementations ArrayTinyLinkedTiny Tiny Classes Interfaces

22 Multiple Implementations ArrayTinyLinkedTiny Tiny Concrete Classes Interfaces AbstractTiny Abstract Classes Exploit inheritance if possible

23 Multiple Implementations ArrayTinyLinkedTiny Tiny Concrete Classes Interfaces AbstractTiny Abstract Classes Add standard functionality SerializableCloneable

24 ArrayTiny t1 = new ArrayTiny(); LinkedTiny t2 = new LinkedTiny(); // Or, better Tiny t3 = new ArrayTiny(); Tiny t4 = new LinkedTiny(); Creating Instances In general, use interface names to specify the types of collections when declaring variables.

25 The Array Implementation Two kinds –Static array (limits the size) –Dynamic array (no limit, in principle) Static array is simpler

26 public class ArrayTiny implements Tiny{ // Data declarations // Constructors // Other methods } Array Implementation

27 public class ArrayTiny implements Tiny{ private static final int DEFAULT_SIZE = 10; private Object[] items; private int size; public ArrayTiny(){ items = new Object[DEFAULT_SIZE]; size = 0; } // Other methods } Array Implementation

28 public void add(Object obj){ items[size] = obj; size++; } Adding an Item Quick and dirty: does not enforce precondition

29 public void add(Object obj){ if (isFull()) throw new IllegalStateException( "Attempt to add to a full collection"); items[size] = obj; size++; } Adding an Item

30 if (tcol.isFull()) System.out.println("Attempt to add to a full collection"); else tcol.add( ); Client’s Error Handling try{ tcol.add( ); } catch(IllegalStateException e){ System.out.println("Attempt to add to a full collection"); }

31 // tcol is a Tiny collection // Open a file output stream fos ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(tcol); oos.flush; oos.close(); Serialization // tcol is a Tiny collection // Open a file input stream fis ObjectInputStream ois = new ObjectInputStream(fis); tcol = (Tiny) ois.readObject(); ois.close();

32 import java.io.*; public class ArrayTiny implements Tiny, Serializable{ // Data declarations // Constructors // Other methods } Serialization

33 public class ArrayTiny implements Tiny, Serializable, Cloneable{ public Object clone(){ // Create a new ArrayTiny // Add all items in my array to it // Return it } Cloning A clone contains the same objects as the original collection (shallow copy).


Download ppt "Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different."

Similar presentations


Ads by Google