Collection Framework-1.pptx

SarthakSrivastava70 4 views 45 slides Oct 07, 2023
Slide 1
Slide 1 of 45
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45

About This Presentation

No


Slide Content

Collection

Collection A group of individual object as a single entity is called collection.

Collection Framework It defines several classes and interfaces , which can be used to represent a group of objects as a single entity.

Hierarchy of Collection Framework The  java.util  package contains all the classes and interfaces for the Collection framework .

Collection Interface If we want to represent a group of individual object as a single entity then should go for collection.

Methods of Collection Interface b oolean add(Object o): It is used to insert an element (object) in the collection. boolean addAll (Collection c ): It is used to insert the specified collection elements (objects) in the collection. boolean remove(Object o): It is used to delete an element (object) from the collection.

Cont … b oolean removeAll (Collection c): It is used to delete all the elements (objects) of the specified collection from the collection. boolean retainAll (Collection c ): It is used to delete all the elements (objects) of collection except those present in collection c. v oid clear(): It removes all elements (objects) from the collection.

Cont … i nt size(): It returns the total number of elements (objects) in the collection. Iterator iterator(): It returns reference of an Iterator interface.

List Interface List is the child interface of Collection interface. If we want to represent a group of individual object where duplicate objects are allowed and insertion order is preserved then we should go for List. Insertion order will be preserved by mean of index.

Cont … We can differentiate duplicate object by using index. Hence index place a very important role in List. We can also store the null elements in the list.

Methods of List Interface void add( int index, Object o): It is used to insert the specified element at the specified position (index) in a list. boolean addAll ( int index, Collection c ): It is used to append all the elements in the specified collection, starting at the specified position (index) of the list. Object remove( int index): It is used to remove the element present at the specified position (index) in the list.

Cont … Object get( int index): It is used to get the element from the particular index of the list. boolean set( int index, Object new): It is used to replace the specified element in the list, present at the specified position and return the element previously at the specified position i nt indexOf (Object o): It is used to return the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element.

Cont … int lastIndexOf (Object o): It is used to return the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element.

Iterator Interface (Universal cursor) Iterator interface can be used to get object one by one from the collection. We can use I terator interface for any collection so it is called universal cursor.

Methods of Iterator Interface p ublic boolean hasNext (): It returns true if the iterator has more elements otherwise it returns false. p ublic Object next(): It returns the next element in the iteration and moves the cursor pointer to the next element. p ublic void remove(): It removes the element.

ArrayList class ArrayList implements the List interface so we can use all the methods of the List interface here . It uses a dynamic array for storing the elements. It is like an array, but there is no size limit. The underlying data structure for a ArrayList is resizable array (dynamic array) or growable array.

Cont … Insertion order is preserved. Duplicate objects are allowed. Heterogeneous objects are allowed. n ull insertion is possible.

Cont … ArrayList allows random access because the array works on an index basis. In ArrayList , manipulation is a little bit slower than the LinkedList in Java because a lot of shifting needs to occur if any element is removed from the array list.

Constructor of ArrayList class ArrayList (): Create an empty ArrayList with default initial capacity 10 and once arraylist reaches its maximum capacity then a new arraylist will be created with new capacity. new capacity = ( currentcapacity * 3/2) + 1 ArrayList ( int initialcapacity ): create an empty ArrayList with the specified initial capacity.

Example of ArrayList (Storing heterogeneous elements) Example-1 : Write a program to store heterogeneous elements (objects) in an ArrayList then print arrayList .

import java.util .*; class ArrayList1 { public static void main(String a[]) { ArrayList al = new ArrayList (); al.add (1); al.add (2.2); al.add ("Ram"); al.add ('D'); System.out.println (al); } } Output: [1, 2.2, Ram, D]

Example of ArrayList (Storing homogeneous elements) Example-2 : Write a program to store only homogeneous elements (objects) in an ArrayList then print arrayList .

import java.util .*; class ArrayList2 { public static void main(String a[]) { ArrayList <Integer> al = new ArrayList <Integer>(); al.add (1); al.add (2); al.add (3); al.add (4); System.out.println (al); } } Output: [1, 2, 3, 4]

Example-3 Write a program to store elements (objects) in an ArrayList then print the element of arrayList by using I terator interface.

import java.util .*; class ArrayList3 { public static void main(String a[]) { ArrayList al = new ArrayList (); al.add (1); al.add (2.2); al.add ("Ram"); al.add ('D'); Iterator it = al.iterator (); while( it.hasNext ()) { System.out.println ( it.next ()); } } } Output: 1 2.2 Ram D

LinkedList class The underlying data structure for a LinkedList is doubly l inked list. Insertion order is preserved. Duplicate objects are allowed. Heterogeneous objects are allowed . null insertion is possible.

Cont … LinkedList is best choice if our frequent operation is insertion or deletion in the middle. LinkedList is worst choice if our frequent operation is retrival .

Constructor of LinkedList class LinkedList (): Create an empty LinkedList .

Methods of LinkedList class void addFirst (Object o): It is used to insert the specified element (object) at the beginning of list . void addLast (Object o): It is used to add the specified element (object) to the end of list. Object removeFirst (): It is used to remove and return the first element (object) from list .

Cont … Object removeLast (): It is used to remove and return the last element (object) from list . Object getFirst (): It is used to return the first element (object) from list . Object getLast (): It is used to returns the last element (object) from list.

Example Example-1 : Write a program to store elements (objects) in an LinkedList then print LinkedList .

import java.util .*; class LinkedListExample { public static void main(String a[]) { LinkedList l = new LinkedList (); l.add (1); l.add (2.2); l.add ("Ram"); l.add ('D'); System.out.println (l); } } Output: [1, 2.2, Ram, D]

Set Interface Set is the child interface of Collection interface. If we want to represent a group of individual object where duplicate objects are not allowed and insertion order is not preserved then we should go for Set. Set interface does not contain any method so we have to use only methods of collection interface.

HashSet class The underlying data structure for HashSet is Hash table. Duplicate objects are not allowed . Insertion order is not preserved and all objects are inserted according to their hash code. Heterogeneous objects are allowed.

Cont … null insertion is possible only once because duplicates are not allowed. HashSet is the best approach for search operations.

Constructor of HashSet class HashSet (): Create an empty HashSet with default initial capacity 16 and fill ration .75. HashSet ( int initialcapacity ): create an empty HashSet with the specified initial capacity and fill ration .75. .

Example Example-1 : Write a program to store elements (objects) in an HashSet then print HashSet .

import java.util .*; class HashSetExample { public static void main(String a[]) { HashSet hs = new HashSet (); hs.add (1); hs.add (2.2); hs.add ("Ram"); hs.add ('D'); System.out.println ( hs ); } }

LinkedHashSet class LinkedHashSet is the child class of HashSet . It is exactly same as HashSet except the following difference. The underlying data structure for LinkedHashSet is a combination of Hash table and LinkedList . Insertion order is preserved.

Example Example-1 : Write a program to store elements (objects) in an LinkedHashSet then print LinkedHashSet .

import java.util .*; class LinkedHashSetExample { public static void main(String a[]) { LinkedHashSet lhs = new LinkedHashSet (); lhs.add (1); lhs.add (2.2); lhs.add ("Ram"); lhs.add ('D'); System.out.println (lhs); } } Output: [1, 2.2, Ram, D ]

SortedSet Interface It is the child interface of Set interface. If we want to represent a group of individual objects according to some sorting order then we should go for Set. The default natural sorting order for the number’s is ascending order. The default natural sorting order for the character and string are alphabetical order (dictionary based order) .

Methods of SortedSet Interface public Object first(): Returns the first element currently in the sorted set. public Object last(): Returns the last element currently in sorted the set. SortedSet headSet (Object obj ): Return the sorted set whose elements are less than obj in sorted set.

Cont … SortedSet tailSet (Object obj ): Return the sorted set whose elements are grater than or equal to obj in sorted set. SortedSet subSet (Object obj1, Object obj2): Return the sorted set whose elements are grater than or equal to obj1 but less than obj2 in sorted set .

Example