2. 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?
4. 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
5. 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.
6. 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
7. 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.
8. OOP / Slide 8
Methods of Collection interface
public boolean equals(Object element) : matches two collection.
9. OOP / Slide 9
Example of addAll(Collection) method:
14. 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.
15. OOP / Slide 15
Difference between List and Set:
List can contain duplicate elements
whereas
Set contains unique elements only.
17. 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:
Ravi
Vijay
18. 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.
20. 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.
21. 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).