collection

23
OOP / Slide 1 Collection Framework

Upload: guna-sekaran

Post on 01-Nov-2014

76 views

Category:

Education


0 download

DESCRIPTION

java collection

TRANSCRIPT

Page 1: Collection

OOP / Slide 1

Collection Framework

Page 2: Collection

OOP / Slide 2

Reduces programming effort >Increases program speed and quality

>Reduce effort to learn and use new APIs

Is simply an object that groups multiple elements

into a single unit. Sometimes called a container Collections are used to store, retrieve, manipulate,

and communicate aggregate data

Benefits of Collection Framework

What is a Collection?

Page 3: Collection

OOP / Slide 3

The Collections Hierarchy

Page 4: Collection

OOP / Slide 4

Collection Framework ArrayList Class

LinkedList Class

HashSet Class

LinkedHaseSet Class

TreeSet Class

Map Interface

LinkedHashMap Class

TreeMap Class

Hashtable Class

Comparable Interface

Comparator Interface

HashMap Class

Page 5: Collection

OOP / Slide 5

Commonly thrown Exceptions in Collection Framework

UnSupportedOperationException occurs if a Collection cannot be modified.

ClassCastException occurs when one object is incompatible with another.

NullPointerException occurs when you try to store null object in Collection.

IllegalArgumentException thrown if an invalid argument is used.

IllegalStateException thrown if you try to add an element to an already full Collection.

Page 6: Collection

OOP / Slide 6

Hierarchy of ArrayList class:

ArrayList<Integer> list = new ArrayList<Integer>(0); list.add(1001);

list.add(1002);

System.out.println(list.get(5));

Exception:

java.lang.IndexOutOfBoundsException: Index: 4, Size: 2

IndexOutOfBoundsException

Page 7: Collection

OOP / Slide 7

ArrayList class: Uses a dynamic array for storing the elements.It extends

AbstractList class and implements List interface.

Can contain duplicate elements.

It Maintains insertion order.

Not synchronized.

Random access because array works at the index basis. Manipulation slow because a lot of shifting needs to be

occurred.

Page 8: Collection

OOP / Slide 8

Methods of Collection interface

public boolean equals(Object element) : matches two collection.

Page 9: Collection

OOP / Slide 9

Example of addAll(Collection) method:

Page 10: Collection

OOP / Slide 10

Example of removeAll() method:

Page 11: Collection

OOP / Slide 11

Example of retainAll() method:

Page 12: Collection

OOP / Slide 12

By Iterator interface.package Collection;import java.util.ArrayList;import java.util.Iterator;

public class Array1 { public static void main(String[] args) {

ArrayList alist=new ArrayList(); alist.add("Ravi"); alist.add("Vijay"); alist.add("Ravi"); Iterator itr=alist.iterator(); while(itr.hasNext()){ System.out.println(itr.next());

} }}

Output:Ravi VijayRavi

Page 13: Collection

OOP / Slide 13

ArrayList: By for-each loop.

Page 14: Collection

OOP / Slide 14

LinkedList class: Uses doubly linked list to store the elements. It extends the

AbstractList class and implements List and Deque interfaces.

Can contain duplicate elements.

Maintains insertion order.

Not synchronized.

No random access.

Manipulation fast because no shifting needs to be occurred.

Page 15: Collection

OOP / Slide 15

Difference between List and Set:

List can contain duplicate elements whereas

Set contains unique elements only.

Page 16: Collection

OOP / Slide 16

Linked List

Page 17: Collection

OOP / Slide 17

HashSet class:

Uses hashtable to store the elements.It extends AbstractSet class and implements Set interface.

contains unique elements only.

   HashSet hs=new HashSet(); hs.add("Ravi"); hs.add("Vijay"); Iterator itr=hs.iterator(); while(itr.hasNext()){ System.out.println(itr.next());

Output:RaviVijay

Page 18: Collection

OOP / Slide 18

TreeSet class:

1. Contains unique elements only like HashSet.

2. The TreeSet class implements NavigableSet interface that extends the SortedSet interface.

3. Maintains ascending order.

Page 19: Collection

OOP / Slide 19

Treeset

Output:

Ravi

vijay

Page 20: Collection

OOP / Slide 20

HashMap A HashMap contains values based on the key. It implements

the Map interface and extends AbstractMap class.

It contains only unique elements.

It may have one null key and multiple null values.

It maintains no order.

Page 21: Collection

OOP / Slide 21

HashMap class:

Hierarchy of HashMap What is difference

between HashSet and HashMap?

HashSet contains only values whereas HashMap contains entry(key and value).

Page 22: Collection

OOP / Slide 22

HashMap

Page 23: Collection

OOP / Slide 23

Thank You

[email protected]