Collections In Java
Collection Framework
A collection of objects is any collection of distinct objects that together function as a single entity. JDK 1.2 defined the “Collection Framework,” a separate framework for Java that contains all collection classes and interfaces.
The Collection interface (java.util.Collection) and Map interface (java.util.Map) are the two main “root” interfaces of Java collection classes.
Collection Overview
Java has a framework called collections that is used to store and manage groups of objects. It is a hierarchy of classes and interfaces that makes managing a collection of objects simple. The Java Collection Framework offers numerous classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet) and interfaces (List, Queue, Deque, Set).
What is a Framework?
A framework is a collection of classes and interfaces that offer an already constructed architecture. There is no requirement to define a framework before implementing a new feature or class. However, the best object-oriented design incorporates a framework with a set of classes that all carry out the same kind of function.
The Collection Framework has several benefits.
The following are the benefits of the collection framework because the lack of one led to the aforementioned set of drawbacks.
- API consistency: The API has a fundamental set of interfaces like Collection, Set, List, or Map, and all classes that implement these interfaces (such as ArrayList, LinkedList, Vector, etc.) share a common set of methods.
- Makes programming easier: Instead of worrying about the Collection’s design, a programmer can concentrate on how to use it most effectively in his program. As a result, the fundamental idea of object-oriented programming, or abstraction, has been successfully applied.
- Program speed and quality are increased: provides high-performance implementations of practical data structures and algorithms, which improves performance because it frees the programmer from having to consider the best way to implement a given data structure. To significantly improve the performance of his algorithm or program, he can simply use the best implementation.
Example: ArrayList Class of Collections
Run
// The Collections framework is defined in the java.util package import java.util.ArrayList; class Main { public static void main(String[] args){ ArrayList animals = new ArrayList<>(); // Add elements animals.add("Dog"); animals.add("Cat"); animals.add("Horse"); System.out.println("ArrayList: " + animals); } }
Output:
ArrayList: [Dog, Cat, Horse]
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Methods of Collection interface
There are many methods declared in the Collection interface. They are as follows:
No. | Method | Description |
---|---|---|
1 | public boolean add(E e) | This element is inserted using it into the collection. |
2 | public boolean addAll(Collection<? extends E> c) | The invoking collection is used to contain the specified collection elements. |
3 | public boolean remove(Object element) | An element can be removed from the collection using it. |
4 | public boolean removeAll(Collection<?> c) | It is used to remove all of the specified collection’s elements from the collection that is being invoked. |
5 | default boolean removeIf(Predicate<? super E> filter) | It is used to remove all collection elements that match the given criteria. |
6 | public boolean retainAll(Collection<?> c) | Except for the specified collection, it is used to delete every element of the invoked collection. |
7 | public int size() | It provides the collection’s overall element count. |
8 | public void clear() | The total number of elements is taken out of the collection. |
9 | public boolean contains(Object element) | It is employed to look up an element. |
10 | public boolean containsAll(Collection<?> c) | It is utilised to search the collection’s designated collection. |
11 | public Iterator iterator() | It returns an iterator. |
12 | public Object[] toArray() | It converts collection into array. |
13 | public T[] toArray(T[] a) | Collections are transformed into arrays. The returned array in this case has the same runtime type as the specified array. |
14 | public boolean isEmpty() | It checks if collection is empty. |
15 | default Stream parallelStream() | The collection serves as the source of the potentially parallel Stream that is returned. |
16 | default Stream stream() | It returns a sequential Stream with the collection as its source. |
17 | default Spliterator spliterator() | It generates a Spliterator over the specified elements in the collection. |
18 | public boolean equals(Object element) | It matches two collections. |
19 | public int hashCode() | It returns the hash code number of the collection. |
Login/Signup to comment