π‘
Collection (no interface)
represents a group ofobjects
,elements
, ordata
.
π‘
Java Collections Framework
is aunified architecture
that provides optimaldata structures
and essentialalgorithms
for efficientdata storage
andmanipulation
.
Java Collections Framework
enables developers to concentrate on managing and processing data
without having to implement the underlying logic themselves, by offering standardised and optimal data structures
and related algorithms
as the framework
simplifies tasks like searching
, sorting
, and organising
data.
Collection Framework
, specifically, implements the Collection interface
in the java.util package
where the interface
is further implemented to other critical interfaces
including:
Set
List
Queue
List
is further extended to a Stack
, and the Map interface
is a component of a Java Collections Framework
that does not implement the Collection interface
, however, implements the Map interface
.
Overall, this can be graphically represented as below:
π‘
Interable Interface
is implemented to theCollection Interface.
Implementing the iterable interface
implies that the data will be sequentially managed followed by iterator()
and other iterating methods
.
π‘
Collection Interface
is virtually a rootinterface
in theCollection
hierarchy in theCollection Framework
.
Many methods
strictly related to data management
are defined where these methods can be implemented individually in the collection classes
.
π‘
Set
is acollection
that containsno duplicate
andnon-sequential
elements
ordata
.
Set
places stronger emphasis on some methods from the Collection interface
including add
, equals
, and hashcode
methods
. The beyond is the list of methods
that has to be implemented in the Set classes
.
The most popular classes
implementing the set interface
are:
HashSet
O(1)
.non-sequential iteration
order. HashMap class
.LinkedHashSet
O(1)
(except for iteration).sequential iteration
order.HashSet
and, ultimately the Linked HashMap class
.TreeSet
log(n)
time operation Comparator
or by default by the NATURAL_ORDERING
.TreeMap Class
and hence the Red-Black Tree structure
.LinkedHashSet
specifically uses a constructor only designed for the LinkedHashSet class
in the HashSet class
where a constructor initialises the map to an instance of the LinkedHashMap class
.
HashSet & LinkedHashSet
public class HashSet<E>
extends AbstractSet<E>
implements Set<E>, Cloneable, java.io.Serializable
{
HashSet(int initialCapacity, float loadFactor, boolean dummy) {
map = new LinkedHashMap<>(initialCapacity, loadFactor);
}
}
public class LinkedHashSet<E>
extends HashSet<E>
implements Set<E>, Cloneable, java.io.Serializable {
...
public LinkedHashSet(int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor, true);
}
}
π‘
List
is adata structure
appropriate for theordered collection
where a developer has full control of insertion and thedata
can be accessed via index.
List
allows duplicate data as a part of a collection
and the operation cost varies from O(1)
to O(n)
depending on the operations.
List Interface
includes the following methods
that are left for individual implementation from implementing classes
:
Popular classes
implementing the List interface
are namely:
ArrayList
Queue
Vector
Stack
π‘
ArrayList
is an implementation of theList interface
, which dynamically resizes itself when thedata
exceeds its capacity.
ArrayList
maintains the order of data storage
and allows duplicate values.
ArrayList
offers strengths in fast retrieval
(O(1)
for accessing elements by index), but operations like insertion
and deletion
can be slower (O(n)
), especially when modifying elements in the middle of the list.
In-depth, it is backed by an Object array
, Object[]
, in which if the size of the data
, n
, reaches a cetain threashold resizes followed by its resizing method
combined with Arrays.copyOf()
.
π‘
Queue
is aFIFO
(First In, First Out
)data structure
, commonly used to handle tasks insequential order
.
Queue
ensures that the first element added is the first one to be removed.
Common operations include offer
(adding an element) and poll
(removing an element), typically having a time complexity of O(1)
for both.
In Java
, queue
is often implemented via LinkedList Class
where it implements List
and Queue
interfaces together.
LinkedList Class
internally contains the Node Class
where the Node object
contains a data
and previous and next Node information
, implying it is a Doubly-LinkedList
.
π‘
Vector
is a legacyCollection Class
where it is essentially similar to theArrayList Class
, however, isthread-safe
.
Vector
is implemented via resisable Object array
, Object[]
like the ArrayList Class
.
Vector
is thread-safe
and it is followed by all data operational methods
being synchronized methods
.
π‘
Stack
is aLIFO
(Last In, First Out
)data structure
, where the most recently added element is the first one to be removed.
Stack
is widely used in tasks like function call management, expression evaluation, and undo features.
Common operations include push
(adding an element) and pop
(removing the top element).
Extends the Vector Class
, where it implies that the Stack
is thred-safe
.
π‘
Map
is akey-value
data structure
where there are no duplicatekeys
.
Map
almost always ensures a constant time for basic operations (O(1)
), including search
, add
, remove
, and etc
, and hence if often used by the developers for performance's sake.
Some methods that are left for individual implementation from the Map classes
are:
Most popular classes that implements the map interface
are:
HashMap
O(1)
).HashTable
.null
key.LinkedHashMap
O(1)
).HashMap
& a doubly-linked list
.doubly-linked list
running through all of its entries
(preserves key insertion order).TreeMap
O(log n)
).Comparator
or by default by the NATURAL_ORDERING
.Red-Black tree
based NavigableMap
implementation.Hashtable
O(1)
).Java
βs legacy classes for storing key-value
pairs.synchronised
(thread safety
) where specifically all data operational methods
are synchronized methods
.μλ°μ μ
TechBum
Oracle (1)
Oracle (2)
Oracle (3)
Oracle (4)
Oracle (5)