Docsity

OOPS - The Collection Classes, Study notes of Object Oriented Programming

Toggle sidebar
bg1
COLLECTION CLASSES AND ITERATION PROTOCOLS
The Collections Framework
• A framework is a set of classes that form the basis for building advanced
functionality.
• A framework contains superclasses 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
The standard collection classes are summarized in the following table.
Class Description
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.
LinkedList Implements a linked list by extending
AbstractSequentialList.
ArrayList Implements a dynamic array by extending
AbstractList.
AbstractSet Extends AbstractCollection and implements most of
the Set interface.
AbstractSortedSet Extends AbstractSet and supports sorting
functionality of elements in set
HashSet Extends AbstractSet for use with a hash table,
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download OOPS - The Collection Classes and more Study notes Object Oriented Programming in PDF only on Docsity!

COLLECTION CLASSES AND ITERATION PROTOCOLS

The Collections Framework

  • A framework is a set of classes that form the basis for building advanced functionality.
  • A framework contains superclasses 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

The standard collection classes are summarized in the following table.

Class Description

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. LinkedList Implements a linked list by extending AbstractSequentialList. ArrayList Implements a dynamic array by extending AbstractList. AbstractSet Extends AbstractCollection and implements most of the Set interface. AbstractSortedSet Extends AbstractSet and supports sorting functionality of elements in set 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.

ArrayList() ArrayList(Collection c)

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.

//Demonstrate ArrayList.

import java.util.*;

class ArrayListDemo { public static void main(String args[]) { // create an array list ArrayList al = new ArrayList(); System.out.println(“Initial size of al : “ + al.size()); // add elements to the array list al.add(“C”); al.add(“A”); al.add(“E”); al.add(“B”); al.add(“B”); al.add(“D”);

class ArrayListToArray { public static void main(String args[]) { // Create an array list ArrayList al = new ArrayList{};

// Add elements to the array list al.add(new Integer(1)); al.add(new Integer(2)); al.add(new Integer(3)); al.add(new Integer(4));

System.out.println(“Contents of al: “ + al); // get array Object ia[] = al.toArray(); int sum = 0;

//sum the array for (int i=0; i<ia.length; i++) sum += ((Integer) ia[i]).intValue(); System.out.println(“Sum is : 4 sum); } }

The output from the program is shown here:

Contents of al: [1, 2, 3, 4] Sum is: 10

The LinkedList Class The LinkedList class extends AbstractSequentialList and implements the List interface. It provides a linked-list data structure. It has the two constructors, shown here:

LinkedList() LinkedList(Collection c)

void addFirst(Object obj) void addLast(Object obj)

Object getFirst() Object getLast()

Object removeFirst() Object removeLast()

The following program illustrates several of the methods supported by LinkedList:

// Demonstrate LinkedList. import java.util.*

class LinkedListDemo { public static void main(String args[]) { // create a linked list LinkedList l1 = new LinkedList() ; // add elements to the linked list l1.add(“F”); l1.add(“B”); l1.add(“D”); l1.add(“E”); l1.add(“C”); l1.addLast(“Z”); l1.addFirst(“A”);

l1.add(1, “A2”);

index at which the data associated with the key is stored. The transformation of the key into its hash code is performed automatically-you never see of hashing is that is allows the execution time of basic operations, such as add(), contains(), remove() and size() to remain constant even for large sets.

The following constructors are defined:

HashSet() HashSet(Collection c)

Importantly, note that a hash set does not guarantee the order of its elements.

//Demonstrate HashSet import java.util.*;

class HashSetDemo { public static void main(String args[]) { //create a hast set HashSet ha = new HashSet();

//add elements to the hash set ha.add(“B”); ha.add(“A”); ha.add(“D”); ha.add(“E”); ha.add(“C”); ha.add(“F”);

System.out.println(ha); } }

The following is the output from this program [F, E, D, C, B, A]

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.

The following constructors are defined:

TreeSet() TreeSet(Collection c)

//Demonstrate TreeSet import java.util.*;

class TreeSetDemo { public static void main(String args[]) { // create a tree set TreeSet ts = new TreeSet();

//Add elements to the tree set ts.add(“C”); ts.add(“A”); ts.add(“B”); ts.add(“E”); ts.add(“F”); ts.add(“D”);

System.out.println(ts);

public static void main(String args[]) { //create an array list ArrayList al = new ArrayList();

// add elements to the array list al.add(“C”); al.add(“A”); al.add(“E”); al.add(“B”); al.add(“D”); al.add(“F”);

//use iterator to display contents of al System.out.print(“Original contents of al: “); while(itr.hasNext()) { Object element = itr.next(); System.out.print(element + “”); System.out.pirntln(); // modify objects being iterated ListIterator litr = al.listIterator[]; while(litr.hasNext()) { Object element = litr.next(); litr.set(element + “+”); } System.out.print(“Modified contents of al: “); itr = al.iterator(); while(itr.hasNext()) { Object element = itr.next(); System.out.print(element + “ “); } System.out.println(); // now, display the list backwards

System.out.print(“Modified list backwards: “); While(litr.hasPrevious()) { Object element = litr.previous(); System.out.print(element + “ “0; } System.out.println(); } }

The output is shown here: Original contents of al: C A E B D F Modified contents of al: C+ A+ E+ B+ D+ F+ Modified list backwards: F+ D+ B+ E+ A+ C+