Docsity

OOPS - Collection classes and Iteration protocols, Study notes of Object Oriented Programming

Partial preview of the text

Download OOPS - Collection classes and Iteration protocols and more Study notes Object Oriented Programming in PDF only on Docsity!

Collection classes & Iteration

protocols

The Collections Framework

 A framework is a set of classes that form the basis

for building advanced functionality.

 A framework contains super-classes with useful

functionality, policies, and mechanisms.

 The user of a framework forms subclasses to extend

the functionality without having to reinvent the basic

mechanisms.

 The Java collections library forms a framework for

collection classes.

The Collection Classes

AbstractCollection

Implements most of the Collection interface.

AbstractList

Extends AbstractCollection and implements most of the List interface.

AbstractSequentialList

Extends AbstractList for use by a collection that uses sequential rather than random access of its elements.

AbstractSet

Extends AbstractCollection and implements most of the Set interface.

The Collection Classes

AbstractSortedSet

Extends AbstractSet and supports sorting functionality of elements in set

LinkedList

Implements a linked list by extending Abstract SequentialList.

ArrayList

Implements a dynamic array by extending AbstractList.

HashSet

Extends AbstractSet for use with a hash table

TreeSet

Implements a set stored in a tree. Extends AbstractSet.

ArrayList Class

 The ArrayList class extends AbstractList and

implements the List interface.

 ArrayList supports dynamic arrays that can

grow as needed.

 Constructors

– ArrayList()

– ArrayList(Collection c)

ArrayList Example

 The following program shows a simple use of

ArrayList. An array list is created, and then

objects of type String are added to it. The list

is then displayed. Some of the elements are

removed and the list is displayed again.

LinkedList Example

The HashSet Class

 HashSet extends AbstractSet and implements the

Set interface.

 It creates a collection that uses a hash table for

storage.

 Hash table stores information by using a mechanism

called hashing.

 Hash set does not guarantee the order of its

elements

 Constructors

  • (^) HashSet()
  • (^) HashSet(Collection c)

The TreeSet Class

 TreeSet provides an implementation of the Set

interface that uses a tree for storage.

 Objects are stored in sorted, ascending order.

 Access and retrieval times are quite fast, which

makes TreeSet an excellent choice when storing

large amounts of sorted information that must be

found quickly.

 Constructors

  • (^) TreeSet()
  • (^) TreeSet(Collection c)

TreeSet Example

import java.util.*;

class TreeSetDemo {

public static void main(String args[]) {

TreeSet ts = new TreeSet();

ts.add(“C”); ts.add(“A”); ts.add(“B”);

ts.add(“E”); ts.add(“F”); ts.add(“D”);

System.out.println(ts);

The output from the program is shown here:

[A, B, C, D, E, F]

Using an Iterator

 Each of the collection classes provides an Iterator()

method that returns an iterator to the start of the

collection.

 By using this iterator object, you can access each

element in the collection, one element at a time. In

general, to use an iterator to cycle through the

contents of a collection, follow these steps:

  • (^) Obtain an iterator to the start of the collection by calling the

collection’s Iterator() method.

  • (^) Set up a loop that makes a call to hasNext(). Have the loop

iterate as long as hasNext() returns true.

  • (^) Within the loop, obtain each element by calling next()