A framework is a set of interfaces and classes which contain predefined architecture. A framework is a collection of classes such that all the classes perform the same kind of task.
The Java collection framework provides a predefined architecture to manipulate and store a group of objects.
Java Collections provide a predefined operation that you can perform on data such as insertion, deletion, manipulation, and deletion.
Why the need for Java Collection over the Arrays?
- An array cannot grow and shrink dynamically.
- An array doesn’t have type safety.
- Implementing data structure from scratch is difficult.
Benefits of java Collections Framework
- Reduce Development Efforts – Collection provides predefined methods used to manipulate and iterate the data. So we concentrate on business logic rather than providing implementation, manipulation and integration of the data.
- Reusability and Interoperability – It is easy to learn as it works in a similar manner and interoperability.
- Increase Quality – Using a collection of predefined classes is well tested so the number of loops holes is negligible as compared to the user-developed data structure.
- Abstraction – Interface provides abstraction as we just to know the name of methods to implement the data structure rather than caring about the implementation of methods.
Hierarchy of Collection Framework
Java.util contains a collection framework as given below architecture of implementation.
Methods in collection Framework
S.No | Methods | Description |
1 | public boolean add(E e) | Insert element return true |
2 | public boolean addAll(Collection<? extends E> c) | Insert an object into another object |
3 | public boolean remove(Object element) | Removes element |
4 | public boolean removeAll(Collection<?> c) | thus, Remove a set of the element present in one object from another object |
5 | default boolean removeIf(Predicate<? super E> filter) | Remove the element with a filter, for example remove element if age above age 60 |
6 | public boolean retainAll(Collection<?> c) | Delete all different elements that are not present specified collection |
7 | public int size() | Return number of elements in the collection |
8 | public void clear() | Remove all element from the collection |
9 | public boolean contains(Object element) | Return true if element found in collection else false. It is used to search element the collection |
10 | public boolean containsAll(Collection<?> c) | Return true if all elements of one collection are present in a specified collection. It is used to search elements of the collection. |
11 | public Iterator iterator() | It is to manipulate the data structure |
12 | public Object[] toArray() | Convert collection into an array |
13 | public <T> T[] toArray(T[] a) | Convert collection into an array but at the runtime |
14 | public boolean isEmpty() | Returns true if there is no element in data struture else return false. |
15 | default Stream<E> parallelStream() | returns a parallel Stream with the data structure as its source. |
16 | default Stream<E> stream() | returns a sequential Stream with the data-structure as its source. |
17 | default Spliterator<E> spliterator() | a Spliterator over the specified elements in the collection. |
18 | public boolean equals(Object element) | return if the object is the same |
19 | public int hashCode() | returns the hash code number |
Iterable Interface
The iterable interface is the root interface in the collection.
Iterator<T> iterator()
List Interface
List interface is the child interface of the Collection interface. The list is implemented by classes of vector, stack, Arraylist, and stack.
ArrayList
This implements the list interface. It is a kind of dynamic array. In this duplicate data is stored. In this insertion order and is non-synchronized. We can access the element in ArrayList randomly.
LinkedList
This is a kind of dynamic array. In this duplicate data is stored. The insertion order is maintained and is non-synchronized. We cannot access the element in ArrayList. This is used in which manipulation of elements is required to be fast because no shifting is required.
Vector
This uses a dynamic array to store the data elements. It is synchronized.
Stack
It implements the last first-out data structure. It uses methods like push, pop, peek.
Queue Interface
It maintains a first-in-first-out order.
Set Interface
This data structure does not allow to store the duplicate elements. Only one null element can be stored.
Conclusion on Java Collection
Collection in java allows you to use almost all commonly used data structure like list, vector, ArrayList, LinkedList, Set, Map, queue, stack, etc and also provide with the methods to use in implementing the data structure like manipulation, searching, sorting, insertion, deletion, etc. This helps the user to code fast, efficiently, and increase code reusability and readability.
written by: Charchil Gupta
Reviewed by: Soutik Maity
If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs