Collections In Java

Any group of individual objects which are represented as a single unit is known as the collection of the objects. In Java, a separate framework named the “Collection Framework” has been defined in JDK 1.2 which holds all the collection classes and interface in it. 

The Collection interface (java.util.Collection) and Map interface (java.util.Map) are the two main “root” interfaces of Java collection classes.

What is a Framework?

A framework is a set of classes and interfaces which provide a ready-made architecture. In order to implement a new feature or a class, there is no need to define a framework. However, an optimal object-oriented design always includes a framework with a collection of classes such that all the classes perform the same kind of task. 

Java Collection Framework Hierarchy:

Java Collections Interface Methods:

Modifier and TypeMethod and Description
booleanadd(E e)Ensures that this collection contains the specified element (optional operation).
booleanaddAll(Collection<? extends E> c)Adds all of the elements in the specified collection to this collection (optional operation).
voidclear()Removes all of the elements from this collection (optional operation).
booleancontains(Object o)Returns true if this collection contains the specified element.
booleancontainsAll(Collection<?> c)Returns true if this collection contains all of the elements in the specified collection.
booleanequals(Object o)Compares the specified object with this collection for equality.
inthashCode()Returns the hash code value for this collection.
booleanisEmpty()Returns true if this collection contains no elements.
Iterator<E>iterator()Returns an iterator over the elements in this collection.
default Stream<E>parallelStream()Returns a possibly parallel Stream with this collection as its source.
booleanremove(Object o)Removes a single instance of the specified element from this collection, if it is present (optional operation).
booleanremoveAll(Collection<?> c)Removes all of this collection’s elements that are also contained in the specified collection (optional operation).
default booleanremoveIf(Predicate<? super E> filter)Removes all of the elements of this collection that satisfy the given predicate.
booleanretainAll(Collection<?> c)Retains only the elements in this collection that are contained in the specified collection (optional operation).
intsize()Returns the number of elements in this collection.
default Spliterator<E>spliterator()Creates a Spliterator over the elements in this collection.
default Stream<E>stream()Returns a sequential Stream with this collection as its source.
Object[]toArray()Returns an array containing all of the elements in this collection.
<T> T[]toArray(T[] a)Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.

Differences between the ArrayList and LinkedList classes that are given below:

ArrayListLinkedList
1) ArrayList internally uses a dynamic array to store the elements.LinkedList internally uses a doubly linked list to store the elements.
2) Manipulation with ArrayList is slow because it internally uses an array. If any element is removed from the array, all the other elements are shifted in memory.Manipulation with LinkedList is faster than ArrayList because it uses a doubly linked list, so no bit shifting is required in memory.
3) An ArrayList class can act as a list only because it implements List only.LinkedList class can act as a list and queue both because it implements List and Deque interfaces.
4) ArrayList is better for storing and accessing data.LinkedList is better for manipulating data.
5) The memory location for the elements of an ArrayList is contiguous.The location for the elements of a linked list is not contagious.
6) Generally, when an ArrayList is initialized, a default capacity of 10 is assigned to the ArrayList.There is no case of default capacity in a LinkedList. In LinkedList, an empty list is created when a LinkedList is initialized.
7) To be precise, an ArrayList is a resizable array.LinkedList implements the doubly linked list of the list interface.

Java Collections Interface:

Set Interface:

The set interface is inherited from the Java collections Interface A Set interface cannot store duplicate/redundant elements in it.

List Interface:

The List interface is derived from the java util package. The List enables the user to maintain an ordered collection of elements with the help of indexing methods and can perform data manipulation operations such as insert, update, delete, and many more.

Reference:

https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html

https://docs.oracle.com/javase/8/docs/api/java/util/Set.html

https://www.simplilearn.com/tutorials/java-tutorial/java-collection

https://www.geeksforgeeks.org/collections-in-java-2/

https://www.javatpoint.com/collections-in-java

https://www.scientecheasy.com/2020/09/collection-hierarchy-in-java.html/

Leave a comment

Design a site like this with WordPress.com
Get started