Docsity

Java Collections: Stacks and Queues, Slides of Java Programming

Partial preview of the text

Download Java Collections: Stacks and Queues and more Slides Java Programming in PDF only on Docsity!

316

PriorityQueue< E > Class

 Works with Comparable objects (or takes a

comparator as a parameter).

 The smallest item has the highest priority.

 Implements a priority queue as a min-heap

PriorityQueue

«interface» Queue

317

push

pop

Stack Queue

LIFO (Last-In-First-Out) access method

add

remove

FIFO (First-In-First-Out) access method

Stacks and queues are used for temporary

storage, but in different situations

319

Stack: Array Implementation

Stack pointer sp

myElements[5] myElements[4] myElements[3] value myElements[2] value myElements[1] value myElements[0] value

public void push (Object x) { myElements [sp] = x; sp++; }

public Object pop ( ) { sp--; return myElements [sp]; }

320

ArrayList Implementation

import java.util.ArrayList;

public class ArrayStack { private ArrayList items ;

public ArrayStack () { items = new ArrayList(); }

public boolean isEmpty () { return items. isEmpty (); } public void push (Object x) { items. add (x); } public Object pop () { return items. remove (items. size () - 1); } public Object peek ( ) { return items. get (items. size () - 1); } }

322

Properties of Stacks

 In an efficient implementation, push, pop, and peek

methods run in O (1) time.

 A stack of objects holds references to objects.

 If necessary, a stack can hold multiple references to

the same object.

 If you are not careful, an object can change while

stored on the stack (unless that object is immutable).

323

java.util.Stack class

 Part of the Java Collections Framework.

 A ―generic‖ class (works with objects of

specified type).

 Based on the legacy Vector class, similar to

ArrayList.

 Methods: isEmpty, push, pop, peek.

 Has other methods  do not use them!

325

Stack Example:Traversing a Tree Stack stk = new Stack() ; TreeNode node = root; while (node != null) { System. out. println (node. getValue ()) ; if (node. getLeft () != null ) { if (node. getRight () != null ) stk.push (node.getRight ()) ; node = node. getLeft (); } else if (node. getRight () != null ) node = node. getRight (); else if (! stk.isEmpty () ) node = stk.pop () ; else node = null; }

Save for future processing

if no children, take the next node from the stack

326

Queues are used for:

 Processing events or messages in order of their

arrival

 System tasks

 Queueing print jobs

 Entering keystrokes

 Processing mouse clicks

328

Properties of Queues

 In an efficient implementation, add, remove, and

peek methods run in O (1) time.

 A queue of objects holds references to objects.

 If necessary, a queue can hold multiple references

to the same object.

 If you are not careful, an object can change while

stored in the queue (unless that object is immutable).

329

The java.util.Queue Interface

 A ―generic‖ interface, part of the Java

Collections Framework

 Implemented by java.util.LinkedList

boolean isEmpty () boolean add (E obj) E remove () E peek ()

331

Sets

 A set is a collection without duplicate values

 What is a ―duplicate‖ depends on the

implementation

 Designed for finding a value quickly

 java.util:

Set interface

 TreeSet

 HashSet

332

Sets (cont‘d)

«interface» Collection

«interface» Iterator

TreeSet HashSet

«interface» Set Methods of Set < E > are the same as methods of Collection < E >

Set ‟s semantics are different from Collection (no duplicates), but Set does not add any new methods.

334

HashSet< E >

 Works with objects for which reasonable

hashCode and equals methods are defined

 Implements a set as a hash table

 contains, add, and remove methods run in O (1)

time

 Iterator returns elements in no particular order

TreeSet HashSet

«interface» Set

Example

 import java.util.*;  public class SetExample {  public static void main(String args[]) {  Set set = new HashSet();  set.add("Bernadine");  set.add("Elizabeth");  set.add("Gene");  set.add("Elizabeth");  set.add("Clara");  System.out.println(set);  Set sortedSet = new TreeSet(set);  System.out.println(sortedSet);  }  } adrish.b@ardentcollaboratio

335