Docsity

Java Collection Framework: Understanding Lists, Sets, and Queues, Lecture notes of Software Engineering

Partial preview of the text

Download Java Collection Framework: Understanding Lists, Sets, and Queues and more Lecture notes Software Engineering in PDF only on Docsity!

At the end of this lecture students will be able to :

 Describe Java Collection Framework

 Identify Hierarchy of Collection Framework

 Identify Collection interfaces and Classes

 Use Iterator Interface and Generic Classes

 Use commonly used collection algorithms

 (^) It is a Pre packaged Implementation  (^) A Java collection framework provides an architecture to store and manipulate a group of objects.  (^) The JCF provides developers to access pre-packaged data structures as well as algorithms to manipulate data  Java collections framework includes the following:  (^) Interfaces:  (^) are abstract data types that represent collections.  (^) allow collections to be manipulated independently of the details of their representation.  (^) Implementations, i.e., Classes:  (^) are the implementation of the collection interfaces.  (^) are reusable data structures.  (^) Algorithm:  (^) the methods which are used to perform operations such as searching and sorting, on objects that implement collection interfaces  (^) are said to be polymorphic : that is, the same method can be used on many different implementations of the appropriate collection interface. 4

 Collections are used to store, retrieve, manipulate, and communicate aggregate data.

 SortedSet: SortedSet is the alternate of Set interface that

provides a total ordering on its elements. The elements of the

SortedSet are arranged in the increasing (ascending) order. The

SortedSet provides the additional methods that inhibit the

natural ordering of the elements.

 Map: It represents key-value pairs. The keys will be unique and

each key can map to at most one value. Although, it does not

ensure element ordering, some implementations guarantee it.

To interoperate with other collection classes/interfaces, it

provides three collection views, which allow a map's contents to

be viewed as a set of keys, collection of values, or set of key-

value mappings.

8

A collection is a container that encapsulates

multiple objects into a single unit, such as a list

of employees, a set of numbers, a set of,

processes, a queue of requests etc..

All the collection classes are available in the

package called 'java.util’

Group of collection classes is called a

Collection Framework.

Some examples of collections are

the cards you hold in a card game,

your favorite songs stored in your computer,

the members of a sports team and

the real-estate records in your local registry of

deeds (which map book numbers and page

 Iterator is an interface that iterates the elements. It is used

to traverse the list and modify the elements.Traverse

forward direction only.

 Defines three fundamental methods

 public boolean hasNext() - This method returns true if

the iterator has more elements.

 public Object next() - It returns the element and

moves the cursor pointer to the next element.

 public void remove() - This method removes the last

elements returned by the iterator.

 These three methods provide access to the contents of the

collection

 An Iterator knows position within collection

 Each call to next() “reads” an element from the collection

 Then you can use it or remove it

public class SimpleCollection {

public static void main(String[] args) {

Collection c;

c = new ArrayList();

System.out.println(c.getClass().getName());

for (int i=1; i <= 10; i++) {

c.add(i + " * " + i + " = "+i*i);

Iterator iter = c.iterator();

while (iter.hasNext())

System.out.println(iter.next());

Output: java.util.ArrayList 1 * 1 = 1 2 * 2 = 4 3 * 3 = 9 4 * 4 = 16 5 * 5 = 25 6 * 6 = 36 7 * 7 = 49 8 * 8 = 64 9 * 9 = 81 10 * 10 = 100

 (^) Extends the Iterator interface  (^) Defines three fundamental methods  void add(Object o)boolean hasPrevious()Object previous()  (^) The addition of these three methods defines the basic behavior of an ordered list  (^) A ListIterator knows position within list.

 (^) The indexed get and set methods of the List interface are appropriate to use since ArrayLists are backed by an array  Object get(int index)Object set(int index, Object element)  (^) Indexed add and remove are provided, but can be costly if used frequently  void add(int index, Object element)Object remove(int index)  (^) May want to resize in one shot if adding many elements  void ensureCapacity(int minCapacity)

import java.util.*;

class ArrayListDemo

{ public static void main(String args[])

{ ArrayList al = new ArrayList();

al.add (“Sheger"); al.add (“Adama");

al.add (“Hawassa"); al.add (“BahirDar");

al.add (“Mekele");

al.add (1,“DireDawa");

al.add (2,“Asosa");

System.out.print("Size of the Array List is: " + al.size ());

System.out.print("\nRetrieving elements in ArrayList using Iter

Iterator it = al.iterator ();

while (it.hasNext () )

System.out.print (it.next () + "\t");

import java.util.*;

public class LinkedList1{

public static void main(String args[]){

LinkedList pl= new LinkedList();

pl.add(“Java");

pl.add(“Python");

pl.add(“C++");

pl.add(“C#");

Iterator itr=al.iterator();

while(itr.hasNext()){

System.out.println(itr.next());