1 / 95

Unit-IV Collections Framework

Unit-IV Collections Framework. Why Collection classes ? . int a; int b; 10000 variables Array :- class Employee; Employee Emp []=new Employee[1000];. Index 0. Index 1. Index 999. Array Limitations :. 1.Fixed in size 2. Homogeneous data

Download Presentation

Unit-IV Collections Framework

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. This work is created by N.Senthilmadasamy, Dr. A.Noble Mary Juliet, Dr. M. Senthilkumar and is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License Unit-IVCollections Framework

  2. Why Collection classes ? int a; intb; 10000 variables Array:- class Employee; Employee Emp[]=new Employee[1000]; Index 0 Index 1 Index 999

  3. Array Limitations: 1.Fixed in size 2. Homogeneous data 3. Data structure based methods not available

  4. Array vs Collections Array 1.Fixed in size 2. Homogeneous data 3. Data structure based methods not available Collections 1.Growable in nature 2. Heterogeneous data 3. DS based readymade methods

  5. What is Collection? Collection is representation of group of individual objects as a single entity Collection framework defines several classes and interfaces which can be used a group of objects as single entity. What is Collection framework ?

  6. Java Collection Collection Framework C++ Container STL (standard template library)

  7. Difference between Collection & Collections Collection is an interface which can be used to represent a group of individual objects as a single entity Collections is an utility class present in java.util.package to define several utility methods (like sorting, searching) for collection objects

  8. There are 9-Key Interfaces are available in Collection Framework ……

  9. Collection Framework1-Collection • Collection represent a group of individual objects as a single entity • Collection interface defines the most common methods which are applicable for any collection object • Collection interface is consider as root interface of collection frame work. • There is no concrete class which implements collection interface directly.

  10. Collection Framework2-List • List is a child interface of collection. • It represent a group of individual objects as a single entity where duplicates are allowed and insertion order preserved

  11. Collection(1.2) List (1.2) ArrayList (1.2) LinkedList (1.2) Vector (1.2) Stack (1.2) Legacy Classes

  12. Collection Framework3-Set • It is the child interface of Collection • It represent a group of individual objects as single entity where duplicates are not allowed and insertion order not preserved

  13. Collection(1.2) Set (1.2) HashSet (1.2) Linked HashSet (1.4)

  14. Collection Framework4-SortedSet • It is the child interface of Collection • It represent a group of individual objects as single entity where duplicates are not allowed but all the objects should be inserted according to some sorting order.

  15. Collection Framework5-Navigable set • It is the child interface of SortedSet • It defines several methods for navigation purposes • Accessing fist data, next data , previous data

  16. Collection(1.2) Set (1.2) SortedSet (1.2) NaviableSet (1.6) TreeSet (1.2)

  17. Collection Framework6-Queue • It is the child interface of Collection • It represent a group of individual objects Prior to processing. • Eg: Sending a mail • all mail id’s have to sore some where in some order

  18. Collection(1.2) Queue (1.5) PriorityQueue (1.5) BlockingQueue (1.5) LinkedBlockingQueue (1.5) Priority BlockingQueue (1.5)

  19. Collection Framework • All the above interfaces (Collection, List, Set, SortedSet , NavigableSet, Queue) meant for representing a group of individual objects. • To represents a group of objects with key value pairs => Map Interfaces.

  20. Collection Framework7-Map • Map is not the child interface of collection • It represents a group of objects with key value pairs • both key and value are objects • duplicated keys are not allowed but values can be duplicated

  21. Map (1.2) Dictionary (1.0) HashMap (1.2) WeakHashMap (1.2) IdentifyHashMap (1.4) HashTable (1.0) Properties (1.0) LinkedHashMap (1.4) Legacy Classes

  22. Collection Framework8-SortedMap • It is the child interface of Map • It represents a group of objects with key value pairs according to some sorting order of keys Map (1.2) SortedMap (1.2)

  23. Collection Framework9-Navigable Map • It is the child interface of SortedMap • It defines utility methods for navigation purpose Map (1.2) SortedMap (1.2) NaviableMap (1.6) TreeMap (1.2)

  24. Collections Framework

  25. Methods of Collection interface s:

  26. List(I) publicinterface List<E> extends Collection<E>

  27. Collection(1.2) List (1.2) ArrayList (1.2) LinkedList (1.2) Vector (1.2) Stack (1.2) Legacy Classes

  28. ArrayList • ResizeableArray • Duplicates are allowed • Insertion order is preserved • Heterogeneous object are allowed • expect TreeSet & TreeMapbecause of heterogeneous objects • Null insertion is possible

  29. Constructor • ArrayListobject=new ArrayList() • Create an empty Array list object with default initial capacity 10. • Once Array List reaches its capacity new ArrayList will be created with new Capacity • New capcity =(Current capacity * 3/2)+1 • ArrayList object =new ArrayList(intinitialCapacity); • ArrayList object=new ArrayList(Collection c);

  30. import java.util.*; public class ListExample{ public static void main(String args[]){ ArrayList al=new ArrayList(); al.add("A"); al.add(1); al.add("A"); al.add(null); System.out.println(al); al.remove(2); System.out.println(al); al.add(2,"B"); System.out.println(al); al.add("C"); System.out.println(al); } } [A,1,A,null] [A,1,null] [A,1,B,null] [A,1,B,null,C]

  31. Additional Interfaces • Serializable • Cloneable • Random Access • Any random element can be access with same speed. • Eg: First element access at 1 sec then 100th element also access at 1 sec 100000th element also. • Java.util.RandomAccess • It does not contain any methods, it is a Marker interface

  32. Additional Interfaces • Collection helps to transfer objects from one place to another place, to provide support for this requirement every collection implements Serializable and Cloneable interfaces. • ArrayList and vector classes implements RandomAccess interface

  33. ArrayList -Application • Worst choice ????? • Best choice ????? Pulse Question

  34. ArrayList -Application • Frequent operation is retrieval operation –best choice [RandomAccess interface] • Frequent operation is insertion or deletion in the middle –Worst choice [needs several shift operation ]

  35. Write a java program using menu to create a list for the following operations insert an element ,retrieve an element ,remove any given element search an element import java.util.*; public class ListExample{ public static void main(String args[]){ ArrayList l1=new ArrayList(); Scanner s=new Scanner(System.in); int choice; do{ System.out.println("List"); System.out.println("1.Insert Element into List"); System.out.println("2.Retrive Element from List"); System.out.println("3.Remove Element into List"); System.out.println("4.Search Element into List"); System.out.println("5.Exit"); choice =s.nextInt();

  36. switch(choice) { case 1: System.out.println("Enter Element into List"); l1.add(s.nextInt()); break; case 2: System.out.println("Enter Index to retrive from List"); System.out.println(l1.get(s.nextInt()));break; case 3: System.out.println("Enter Index remove from List"); l1.remove(s.nextInt());break; case 4: System.out.println("Enter Element to search from List"); int x=s.nextInt(); for(int k=0;k<l1.size();k++) if ((int)l1.get(k)==x) System.out.println(x+" is availabe at "+ k); break; } System.out.println("Now the List is "+l1); System.out.println(“ Do you want press 1 to continue press 5 to close"); choice =s.nextInt(); }while(choice!=5); } }

  37. Linked List

  38. Linked List • Double linked list • Insertion order is preserved • Duplicates are allowed • Heterogeneous data allowed • Null insertion possible • Implements Serializable,Clonalbe • Does not implement Randomaccess

  39. Link list is best choice – ????? • Link list is Worst choice – ?????

  40. Link list is best choice – when frequent operation is insertion or deletion in middle. • Link list is Worst choice – when frequent operation is retrieval .

  41. Constructor • LinkedList l1=new LinkedList(); • Creates an empty Linked Object • LinkedList l1=new LinkedList(Collection c); • Creates an equivalent LindkedList Object for given collection

  42. LinkedList used to implement stacks and queues to provide support for this requirement. • Stack=> LIFO Queue=>FIFO • LinkedList class defines the following methods: void addFirst(); void addLast(); Object getFirst(); Object getLast(); Object removeFirst(); Object removeLast();

  43. import java.util.*; public class ListExample1{ public static void main(String args[]){ LinkedList l1=new LinkedList(); l .add("Ram"); l .add(2); l .add("CSE"); l .set(0,"Ravi"); l .add(0,"Raj"); l .addFirst("MCET"); l .addLast(null); System.out.println(l ); } } O/P ?

  44. Vector • Methods present in vector are synchronized. Vector objects are always safe. • Vector is a best choice when our frequent operations is retrieval

  45. Names? Vector-1995 ArrayList

More Related