collection
DESCRIPTION
java collectionTRANSCRIPT
OOP / Slide 1
Collection Framework
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?
OOP / Slide 3
The Collections Hierarchy
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
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.
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
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.
OOP / Slide 8
Methods of Collection interface
public boolean equals(Object element) : matches two collection.
OOP / Slide 9
Example of addAll(Collection) method:
OOP / Slide 10
Example of removeAll() method:
OOP / Slide 11
Example of retainAll() method:
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
OOP / Slide 13
ArrayList: By for-each loop.
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.
OOP / Slide 15
Difference between List and Set:
List can contain duplicate elements whereas
Set contains unique elements only.
OOP / Slide 16
Linked List
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
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.
OOP / Slide 19
Treeset
Output:
Ravi
vijay
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.
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).
OOP / Slide 22
HashMap