top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

COLLECTION IN JAVA


Collection

 Collection is an interface which can be used to represent a group of individual objects as a single entity.

Collection is available in the java.util package.


Collection Framework

It defines the several interfaces and classes by which we can represent group of objects in single entity.

Collection Framework in Java is an architecture with interfaces and classes that can be used anytime and anywhere.

In simple they are reusable data structures.

It is used in storing, maintaining, and handling data effectively.


Collections

 Collections is a utility class present in java.util.package to define several utility methods(like Sorting,Seaching,Shuffling) for collection objects.


Need of collection:

To overcome the limitations in array we use collection in java.

1.Collection are growable in nature i.e., based on the requirement we can increase or decrease the size.

2.Collection can hold both homogeneous and heterogeneous elements(all datatypes).

3.Collection class is implemented based on some data structure standard , so we can use predefined methods available in collection for our requirement.

4.Memory wastage is less.

 


COLLECTION FRAMEWORK Hierarchy: 





CLASS

 Class is a user-defined prototype required to create the object.

A class is a collection of properties or methods shared by all objects of the same type.


INTERFACE

An interface is a class blueprint that includes static methods and variables.

In short, an interface is simply a collection of similar methods with empty bodies.


An interface is made up of a list of actions that an object can perform. For example, when you turn on a fan, all you have to do is flip the switch, and the fan starts; you don’t have to worry about how it works. The interface’s syntax is similar to that of the class. In the Java programming language, the interface is used to inherit more than one class at a time.




9 key Interfaces of Collection Framework:


1.Collection(I):


1.It is the parent/root interface of the collection framework.

2. Whenever we want to represent a group of individual objects as a single entity, we use collection.

3.It defines the most common methods which are applicable for any collection object/classes.

4.  It contains all the declarations of the methods that the Collections will have but there is no concrete class which implements collection interface directly.

5. It contains various abstract methods which are further implemented by several classes of the collection framework. These methods are:

add(Object) – used to add an object to the collection.

addAll(Collection c) – adds all the elements from the given collection(given in argument) to a collection.

clear() – deletes/remove all the elements from a collection.

contains(Object o) – returns true/false if an element is present in            the collection.

containsAll(Collection c) – returns true/false if all the elements (present in the argument collection) are present in the collection.

equals(Object o) – returns true/false after comparing the values of the given object and the collection.

hashCode() – returns the hashcode value of the collection.

isEmpty() – returns true/false if the collection is empty.

iterator() – returns an iterator pointing to the first element in the collection.

max() – returns the maximum value present in the collection.

parallelStream() – returns a parallel stream with this collection as its source.

remove(Object o) – removes the first occurrence of a given object from the collection.

removeAll(Collection c) – removes all the occurrences of a given object from the collection.

removeIf(Predicate filter) – removes all the elements of this collection that satisfy the given predicate.

retainAll(Collection c) – retains only the elements in the collection that are in the argument collection ‘c’.

size() – returns the number of elements present in the collection.

spliterator() – used to create a spliterator over the elements in this collection.

stream() – returns a sequential Stream with this collection as its source.

toArray() – returns an array containing all the elements present in this collection.

 

2.List(I):


1.It helps us to store ordered collection (in sequential / insertion order).

2.  The List interface allows duplicate elements to be present in the collection, and stores objects on an indexing basis.

3.Child interface of collection(I).

4.Implementation classes are ArrayList, LinkedList and Vector(Stack).

 

3.Set(I):


1.Duplicate elements are not allowed.

2.Sequential/Insertion order is not maintained/preserved.

3.Child interface of collection(I).

4. Implementation classes are HashSet and LinkedHashSet.

 

4.SortedSet(I):

1. Duplicate elements are not allowed.

2. Sequential/Insertion order is maintained/preserved(Objects should be inserted according to some sorting order we choose SortedSet).

3.Child interface of set.

 

5.NavigableSet(I):

1.Its is Child interface of SortedSet.

2.It defines several methods for navigation purpose.

3.Implementation class is Treeset.

 

6.Queue(I):

1. Child interface of collection(I).

2.If we want to represent a group of objects prior to processing we use Queue.

3. This interface uses the FIFO (First in First out) principle of storing and retrieving objects, that is, the objects that are inserted first are the ones to be removed/retrieved first.

4.Implementation class of Queue is Priority Queue and Blocking Queue.

 

7.Map(I):

1. Map is not a child interface of collection(I).

2. Map interface is used to store key value pairs, so if we want to represent a group of individual elements as key value pairs then we choose Map.

4.Duplicate keys are not allowed in map, but values can be duplicated.

3. Implementation class of map is HashMap, LinkedHashMap and HashTable.

 

8.Sorted Map(I):

1.It is child interface of map(I).

2. It extends the Map interface and implements the key-value pairs just like a Map object and it orders all the keys by their natural ordering or as specified.

3.If we want some sorting order of keys to represent the group of element we choose SortedMap.

 

9.Navigable Map(I):

1.It is child interface of SortedMap(I).

2.Implementation class is TreeMap.

3.It defines several utility methods for navigation purpose.



Keep learning and growing..


20 views0 comments

Recent Posts

See All

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page