) Collections
7
7.1What is Collection in Java
● A"Collection"inJavareferstoaconceptorframeworkthatholdstogethera
set of objects, or elements, as a single entity.
● Groupsofobjectscanbemanaged,stored,retrieved,andmanipulatedusing
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 elementsand 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 anddoes 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 andremoving
D
elements from both ends. Common implementations include
ArrayDeque.
Collection:The root interface of the Java CollectionsFramework,
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 themselvesto
accommodate the number of elements they hold.
2.Type Safety:Generics in Java collections ensure typesafety,
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 ofthewholecollectionframeworkandit
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 classesforthecollectionhierarchyframeworkwhere
placed in [Link].
4. The Java Collectionhierarchyinjavaisaframeworkprovidesthestructuraldesign
which is used to store up and control the group of objects.
5. TheJavacollectionhierarchyaccomplishedwiththeentireoperationsperformedon
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,VectorandTreeSet.Additionally,therearetwomorefixedinterfaces
namely the SortedSet and SortedMap for sorting purpose.
8. The entire interfaces and the classesforthecollectionhierarchyframeworkwhere
placed in [Link].
7.3 Methods of Collection interface
Interface Java Collection Hierarchy Methods
In Java Collection Hierarchy, there are several Collection Interface Methods whichisusedfor
controlling elements in the collection. Let’s see the following Java Collection Interface Methods.
1. add():
● T hismethodismainlyusedforinsertingoraddinganelementinthecollection.In
the name itself it depicts that add, to add an element to the collection.
● WhenaddinganewelementtothecollectionitreturnsTRUEotherwiseitreturns
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.
● WhenaddinganewelementtothecollectionitreturnsTRUEotherwiseitreturns
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 otherwiseit
returns FALSE.
● The common syntax used for contains() method is as follows
Syntax: contains (Object element) : Boolean
5. ContainsAll():
● This methodisusedtocheckwhethertheentireelementispresentinthegiven
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 withanotherobject,itcomparesthe
particular object in the collection for equality.
● The common syntax used for equal() method is as follows
Syntax: equal(Object element) : Boolean
7. isEmpty():
● Thismethodisusedtoreturntrueonlywhenthecollectionisempty,otherwiseit
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.
● Itremovesthegivenparticularelementfromthecollection.Ifthereisaduplicate
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, ifitremovesalltheelementit
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.
● TheSize()methodreturntypeisintegeritreturnsthetotalnumberofelementsin
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 elementsinthecollection.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.