0% found this document useful (0 votes)
3 views4 pages

Chapter 7 Java Programming

The document provides an overview of the Java Collections Framework, which is a framework that manages groups of objects as a single entity, allowing for storage, retrieval, and manipulation of data. It describes various types of collections such as List, Set, Map, Queue, and Deque, along with their implementations and advantages like dynamic sizing and type safety. Additionally, it outlines key methods available in the Collection interface for managing elements within these collections.

Uploaded by

gauravsniper503
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Chapter 7 Java Programming

The document provides an overview of the Java Collections Framework, which is a framework that manages groups of objects as a single entity, allowing for storage, retrieval, and manipulation of data. It describes various types of collections such as List, Set, Map, Queue, and Deque, along with their implementations and advantages like dynamic sizing and type safety. Additionally, it outlines key methods available in the Collection interface for managing elements within these collections.

Uploaded by

gauravsniper503
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

‭ ) Collections‬

7
‭7.1What is Collection in Java‬
‭●‬ ‭A‬‭"Collection"‬‭in‬‭Java‬‭refers‬‭to‬‭a‬‭concept‬‭or‬‭framework‬‭that‬‭holds‬‭together‬‭a‬
‭set of objects, or elements, as a single entity.‬
‭●‬ ‭Groups‬‭of‬‭objects‬‭can‬‭be‬‭managed,‬‭stored,‬‭retrieved,‬‭and‬‭manipulated‬‭using‬
‭collections.‬
‭●‬ ‭Working‬ ‭with‬ ‭data‬ ‭collections‬ ‭is‬ ‭made‬ ‭easier‬ ‭by‬ ‭Java's‬ ‭extensive‬ ‭array‬ ‭of‬
‭collection classes and interfaces, which are included in the [Link] package.‬
‭●‬ ‭The‬ ‭Java‬ ‭Collections‬ ‭Framework‬ ‭comprises‬ ‭multiple‬ ‭fundamental‬ ‭interfaces‬
‭and their corresponding implementations, which comprise:‬

‭ ist:‬‭A collection that allows duplicate elements‬‭and maintains their‬


L
‭order. Common implementations include ArrayList, LinkedList, and‬
‭Vector.‬

‭ et:‬‭A collection that does not allow duplicate elements.‬‭Common‬


S
‭implementations include HashSet, LinkedHashSet, and TreeSet.‬

‭ ap:‬‭A collection that stores key-value pairs and‬‭does not allow‬


M
‭duplicate keys. Common implementations include HashMap,‬
‭LinkedHashMap, and TreeMap.‬

‭ ueue:‬‭A collection for holding elements before processing,‬‭often‬


Q
‭used for managing tasks in a first-in, first-out (FIFO) order. Common‬
‭implementations include LinkedList and PriorityQueue.‬

‭ eque:‬‭A double-ended queue that supports adding and‬‭removing‬


D
‭elements from both ends. Common implementations include‬
‭ArrayDeque.‬
‭Collection:‬‭The root interface of the Java Collections‬‭Framework,‬
‭which extends Iterable. It represents a group of objects and provides‬
‭common methods for working with collections.‬

‭●‬ ‭Collections in Java provide numerous advantages, including:‬

‭1.‬‭Dynamic Sizing:‬‭Collections automatically resize themselves‬‭to‬


‭accommodate the number of elements they hold.‬
‭2.‬‭Type Safety:‬‭Generics in Java collections ensure type‬‭safety,‬
‭reducing the chances of runtime errors.‬
‭3.‬‭Efficient Algorithms:‬‭Many Java collections use efficient‬
‭algorithms and data structures to provide fast access and‬
‭manipulation.‬
‭4.‬‭Standardized API:‬‭Collections adhere to a common interface,‬
‭making it easier to work with different types of collections using‬
‭a consistent set of methods.‬
‭7.2 Hierarchy of Collection Framework‬

‭1.‬ ‭Java‬ ‭Collection‬ ‭Hierarchy‬ ‭is‬ ‭the‬ ‭hierarchy‬ ‭of‬‭the‬‭whole‬‭collection‬‭framework‬‭and‬‭it‬


‭contained within it with four core interfaces they are Collection, Set, Map and List.‬
‭2.‬ ‭Additionally,‬ ‭there‬ ‭are‬ ‭two‬ ‭more‬ ‭focused‬ ‭interfaces‬ ‭namely‬ ‭the‬ ‭SortedSet‬ ‭and‬
‭SortedMap which is used for sorting purpose.‬
‭3.‬ ‭The‬ ‭entire‬ ‭interfaces‬ ‭and‬ ‭the‬ ‭classes‬‭for‬‭the‬‭collection‬‭hierarchy‬‭framework‬‭where‬
‭placed in [Link].‬
‭4.‬ ‭The‬ ‭Java‬ ‭Collection‬‭hierarchy‬‭in‬‭java‬‭is‬‭a‬‭framework‬‭provides‬‭the‬‭structural‬‭design‬
‭which is used to store up and control the group of objects.‬
‭5.‬ ‭The‬‭Java‬‭collection‬‭hierarchy‬‭accomplished‬‭with‬‭the‬‭entire‬‭operations‬‭performed‬‭on‬
‭data like insertion, deletion, searching, sorting and manipulation.‬
‭6.‬ ‭The‬ ‭Java‬ ‭Collection‬ ‭depicts‬ ‭the‬ ‭single‬ ‭unit‬ ‭of‬ ‭objects.‬ ‭It‬ ‭makes‬ ‭available‬ ‭with‬ ‭a‬
‭number‬ ‭of‬ ‭interfaces‬ ‭like‬ ‭Set,‬ ‭Queue,‬ ‭Deque,‬ ‭List‬ ‭and‬ ‭also‬ ‭it‬ ‭available‬ ‭with‬ ‭the‬
‭classes like ArrayList, LinkedList, LinkedHashSet, HashSet,‬
‭7.‬ ‭PriorityQueue,‬‭Vector‬‭and‬‭TreeSet.‬‭Additionally,‬‭there‬‭are‬‭two‬‭more‬‭fixed‬‭interfaces‬
‭namely the SortedSet and SortedMap for sorting purpose.‬
‭8.‬ ‭The‬ ‭entire‬ ‭interfaces‬ ‭and‬ ‭the‬ ‭classes‬‭for‬‭the‬‭collection‬‭hierarchy‬‭framework‬‭where‬
‭placed in [Link].‬

‭7.3 Methods of Collection interface‬


‭Interface Java Collection Hierarchy Methods‬
I‭n‬ ‭Java‬ ‭Collection‬ ‭Hierarchy,‬ ‭there‬ ‭are‬ ‭several‬ ‭Collection‬ ‭Interface‬ ‭Methods‬ ‭which‬‭is‬‭used‬‭for‬
‭controlling elements in the collection. Let’s see the following Java Collection Interface Methods.‬

‭1.‬ ‭add():‬
‭●‬ T ‭ his‬‭method‬‭is‬‭mainly‬‭used‬‭for‬‭inserting‬‭or‬‭adding‬‭an‬‭element‬‭in‬‭the‬‭collection.‬‭In‬
‭the name itself it depicts that add, to add an element to the collection.‬
‭●‬ ‭When‬‭adding‬‭a‬‭new‬‭element‬‭to‬‭the‬‭collection‬‭it‬‭returns‬‭TRUE‬‭otherwise‬‭it‬‭returns‬
‭FALSE.‬
‭●‬ ‭It‬ ‭avoids‬ ‭duplications‬ ‭so‬ ‭if‬ ‭the‬ ‭particular‬ ‭added‬ ‭element‬ ‭present‬ ‭already‬ ‭the‬
‭collection does not accept it.‬
‭●‬ ‭The common syntax used for add() method is as follows-‬
‭Syntax: add (Object Element): Boolean‬
‭2.‬ ‭addAll():‬
‭●‬ ‭This‬ ‭addAll()‬ ‭is‬ ‭used‬ ‭to‬ ‭add‬ ‭all‬ ‭the‬ ‭elements‬ ‭in‬ ‭the‬ ‭specified‬ ‭collection‬ ‭to‬ ‭this‬
‭collection.‬
‭●‬ ‭When‬‭adding‬‭a‬‭new‬‭element‬‭to‬‭the‬‭collection‬‭it‬‭returns‬‭TRUE‬‭otherwise‬‭it‬‭returns‬
‭FALSE.‬
‭●‬ ‭The common syntax used for addAll() method is as follows-‬
‭Syntax: addAll(Collection c) : Boolean‬
‭3.‬ ‭Clear():‬
‭●‬ ‭this clear() method is used to remove or clear an element in the collection.‬
‭●‬ ‭It does not return anything.‬
‭●‬ ‭The common syntax used for Clear() method is as follows,‬
‭Syntax: clear() : void‬
‭4.‬ ‭Contains():‬
‭●‬ ‭This‬ ‭method‬ ‭is‬ ‭used‬ ‭to‬ ‭check‬ ‭whether‬ ‭the‬ ‭specific‬ ‭element‬ ‭is‬ ‭present‬ ‭in‬ ‭the‬
‭collection or not, mainly it is used for searching/ finding an element.‬
‭●‬ ‭It‬ ‭returns‬ ‭TRUE‬ ‭if‬ ‭it‬ ‭contains‬ ‭the‬ ‭specific‬ ‭element‬ ‭in‬ ‭the‬ ‭collection‬ ‭otherwise‬‭it‬
‭returns FALSE.‬
‭●‬ ‭The common syntax used for contains() method is as follows‬
‭Syntax: contains (Object element) : Boolean‬
‭5.‬ ‭ContainsAll():‬
‭●‬ ‭This‬ ‭method‬‭is‬‭used‬‭to‬‭check‬‭whether‬‭the‬‭entire‬‭element‬‭is‬‭present‬‭in‬‭the‬‭given‬
‭collection or not.‬
‭●‬ ‭It‬ ‭returns‬ ‭TRUE‬ ‭if‬ ‭it‬ ‭contains‬ ‭all‬ ‭element‬ ‭in‬ ‭the‬ ‭collection‬ ‭otherwise‬ ‭it‬ ‭returns‬
‭FALSE.‬
‭●‬ ‭The common syntax used for containsAll() method is as follows,‬
‭Syntax: containsAll(Collection c) : Boolean‬
‭6.‬ ‭equals():‬
‭●‬ ‭This‬ ‭method‬ ‭is‬ ‭used‬ ‭to‬ ‭check‬ ‭the‬ ‭equality‬ ‭with‬‭another‬‭object,‬‭it‬‭compares‬‭the‬
‭particular object in the collection for equality.‬
‭●‬ ‭The common syntax used for equal() method is as follows‬
‭Syntax: equal(Object element) : Boolean‬
‭7.‬ ‭isEmpty():‬
‭●‬ ‭This‬‭method‬‭is‬‭used‬‭to‬‭return‬‭true‬‭only‬‭when‬‭the‬‭collection‬‭is‬‭empty,‬‭otherwise‬‭it‬
‭returns false.‬
‭●‬ ‭The common syntax used for isEmpty() method is as follows,‬
‭Syntax: isEmpty() : Boolean‬
‭8.‬ ‭iterator():‬
‭●‬ ‭This method is used to return an iterator over the elements in the collection.‬
‭●‬ ‭The common syntax used for iterator() method is as follows,‬
‭Syntax: iterator() : iterator‬
‭9.‬ ‭remove():‬
‭●‬ ‭This‬ ‭method‬ ‭is‬ ‭used‬ ‭to‬ ‭return‬ ‭an‬ ‭Boolean‬ ‭value,‬ ‭if‬ ‭it‬ ‭removes‬ ‭the‬ ‭element‬ ‭it‬
‭returns true otherwise it returns false.‬
‭●‬ ‭It‬‭removes‬‭the‬‭given‬‭particular‬‭element‬‭from‬‭the‬‭collection.‬‭If‬‭there‬‭is‬‭a‬‭duplicate‬
‭value, then it removes the first occurrence of the object.‬
‭●‬ ‭The common syntax used for remove() method is as follows,‬
‭Syntax: remove(Object element): Boolean‬
‭10.‬‭removeAll():‬
‭●‬ ‭This‬ ‭method‬ ‭is‬ ‭used‬ ‭to‬ ‭return‬ ‭an‬ ‭Boolean‬ ‭value,‬ ‭if‬‭it‬‭removes‬‭all‬‭the‬‭element‬‭it‬
‭returns‬ ‭true‬ ‭otherwise‬ ‭it‬ ‭returns‬ ‭false.‬ ‭It‬ ‭removes‬ ‭the‬ ‭entire‬ ‭element‬ ‭from‬ ‭the‬
‭collection.‬
‭●‬ ‭The common syntax used for removeAll() method is as follows,‬
‭Syntax: removeAll (Collection c): Boolean‬
‭11.‬‭retainAll():‬
‭●‬ ‭This‬ ‭method‬ ‭is‬ ‭used‬ ‭to‬ ‭retains‬ ‭the‬ ‭elements‬ ‭which‬ ‭are‬ ‭only‬ ‭in‬ ‭the‬ ‭collection‬
‭contained in particular collection. It returns a Boolean value.‬
‭●‬ ‭The common syntax used for retainAll() method is as follows,‬
‭Syntax: retainAll(Collection c): Boolean‬
‭12.‬‭size():‬
‭●‬ ‭This method is used to return the number of elements appears in the collection.‬
‭●‬ ‭The‬‭Size()‬‭method‬‭return‬‭type‬‭is‬‭integer‬‭it‬‭returns‬‭the‬‭total‬‭number‬‭of‬‭elements‬‭in‬
‭the collection.‬
‭●‬ ‭The common syntax used for size() method is as follows,‬
‭Syntax: size(): int‬
‭13.‬‭toArray():‬
‭●‬ ‭This‬ ‭method‬ ‭is‬ ‭used‬ ‭to‬ ‭return‬ ‭the‬ ‭array‬ ‭contained‬ ‭elements‬‭in‬‭the‬‭collection.‬‭It‬
‭returns in the form of an array elements in the collection.‬
‭●‬ ‭The common syntax used for toArray() method is as follows,‬
‭Syntax: toArray(): Object [ ]‬
‭14.‬‭Object[ ] toArray():‬
‭●‬ ‭This‬ ‭method‬ ‭is‬ ‭used‬ ‭to‬ ‭return‬ ‭the‬ ‭array‬ ‭contained‬ ‭in‬ ‭the‬ ‭elements‬ ‭stored‬ ‭in‬
‭invoked collection.‬
‭●‬ ‭The common syntax used for Object toArray() method is as follows,‬
‭Syntax: toArray (Object array[ ]): Object [ ]‬

‭ .4 Collections‬
7
‭❖ Iterator interface‬
‭❖ Collection Interface‬
‭❖ List Interface‬
‭❖ ArrayList‬
‭❖ LinkedList‬
‭❖ Vector‬
‭❖ Stack‬
‭❖ Queue Interface‬
‭❖ Set Interface‬

‭Notice:‬
‭ )‬‭Use link for 7.4 “‬‭[Link]
1
‭2)‬‭Write answer in points with syntax , program & output.‬

You might also like