Collection

153 views

Published on

java collection

Published in: Education
  • Be the first to comment

  • Be the first to like this

Collection

  1. 1. OOP / Slide 1 Collection Framework
  2. 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?
  3. 3. OOP / Slide 3 The Collections Hierarchy
  4. 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. 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. 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. 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. 8. OOP / Slide 8 Methods of Collection interface public boolean equals(Object element) : matches two collection.
  9. 9. OOP / Slide 9 Example of addAll(Collection) method:
  10. 10. OOP / Slide 10 Example of removeAll() method:
  11. 11. OOP / Slide 11 Example of retainAll() method:
  12. 12. 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 Vijay Ravi
  13. 13. OOP / Slide 13 ArrayList: By for-each loop.
  14. 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. 15. OOP / Slide 15 Difference between List and Set: List can contain duplicate elements whereas Set contains unique elements only.
  16. 16. OOP / Slide 16 Linked List
  17. 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. 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.
  19. 19. OOP / Slide 19 Treeset Output: Ravi vijay
  20. 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. 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).
  22. 22. OOP / Slide 22 HashMap
  23. 23. OOP / Slide 23 Thank You Guna.dgv.88@gmail.com

×