950 likes | 965 Views
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
E N D
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
Why Collection classes ? int a; intb; 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 3. Data structure based methods not available
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
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 ?
Java Collection Collection Framework C++ Container STL (standard template library)
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
There are 9-Key Interfaces are available in Collection Framework ……
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.
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
Collection(1.2) List (1.2) ArrayList (1.2) LinkedList (1.2) Vector (1.2) Stack (1.2) Legacy Classes
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
Collection(1.2) Set (1.2) HashSet (1.2) Linked HashSet (1.4)
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.
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
Collection(1.2) Set (1.2) SortedSet (1.2) NaviableSet (1.6) TreeSet (1.2)
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
Collection(1.2) Queue (1.5) PriorityQueue (1.5) BlockingQueue (1.5) LinkedBlockingQueue (1.5) Priority BlockingQueue (1.5)
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.
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
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
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)
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)
List(I) publicinterface List<E> extends Collection<E>
Collection(1.2) List (1.2) ArrayList (1.2) LinkedList (1.2) Vector (1.2) Stack (1.2) Legacy Classes
ArrayList • ResizeableArray • Duplicates are allowed • Insertion order is preserved • Heterogeneous object are allowed • expect TreeSet & TreeMapbecause of heterogeneous objects • Null insertion is possible
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);
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]
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
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
ArrayList -Application • Worst choice ????? • Best choice ????? Pulse Question
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 ]
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();
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); } }
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
Link list is best choice – ????? • Link list is Worst choice – ?????
Link list is best choice – when frequent operation is insertion or deletion in middle. • Link list is Worst choice – when frequent operation is retrieval .
Constructor • LinkedList l1=new LinkedList(); • Creates an empty Linked Object • LinkedList l1=new LinkedList(Collection c); • Creates an equivalent LindkedList Object for given collection
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();
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 ?
Vector • Methods present in vector are synchronized. Vector objects are always safe. • Vector is a best choice when our frequent operations is retrieval
Names? Vector-1995 ArrayList