Java Collections Framework
Long before Java 2, Java provided ad hoc classes. For example: Dictionary, Vector, Stack, and Properties classes are used to store and manipulate groups of objects.
Although these classes are very useful, they lack a core, unifying theme. For this reason, the way you use the Vector class is very different from the way you use the Properties class.
The collection framework is designed to meet the following goals.
The framework must be performant. The implementation of basic collections (dynamic arrays, linked lists, trees, hash tables) must also be efficient.
The framework allows different types of collections to work in similar ways with a high degree of interoperability.
Extensions and adaptations to a collection must be simple.
To this end, the entire collection framework is designed around a set of standard interfaces. You can directly use the standard implementations of these interfaces, such as LinkedList, HashSet, and TreeSet, etc. In addition, you can also implement your own collections through these interfaces.
The collection framework is a unified architecture for representing and manipulating collections. All collection frameworks contain the following content:
Interface: is an abstract data type representing a collection. Interfaces allow a collection to independently manipulate the details of its representation. In object-oriented languages, interfaces usually form a hierarchy.
Implementation (class): is the specific implementation of the collection interface. Essentially, they are reusable data structures.
Algorithms: are methods in objects that implement the collection interface to perform some useful calculations, such as searching and sorting. These algorithms are called polymorphic because the same method can have different implementations on similar interfaces.
In addition to collections, the framework also defines several Map interfaces and classes. Map stores key/value pairs. Although Maps are not collections, they are fully integrated into collections.
Collection interface
The collection framework defines some interfaces. This section provides an overview of each interface:
Serial number | Interface description |
---|---|
1 | Collection interface allows you to use a set of Object is the root interface of the Collection hierarchy. |
2 | List interface Inherits from Collection and a List instance stores the elements of an ordered collection. |
3 | Set Inherited from Collection, it is a collection that does not contain duplicate elements. |
4 | SortedSet Inherits from Set to save an ordered collection. |
5 | Map Map unique keys to values. |
6 | Map.Entry Describes an element (key/value pair) in a Map. Is an internal class of Map. |
7 | SortedMap Inherited from Map, keeping Key in ascending order. |
8 | Enumeration This is a traditional interface and defined method through which you can enumerate (obtain one at a time) elements in a collection of objects. This traditional interface has been replaced by iterators. |
Collection Class
Java provides a set of standard collection classes that implement the Collection interface. Some of them are concrete classes that can be used directly, while others are abstract classes that provide partial implementation of the interface.
The standard collection classes are summarized in the following table:
Serial number | Class description |
---|---|
1 | AbstractCollection Implements most of the collection interfaces. |
2 | AbstractList Inherits from AbstractCollection and implements most of the List interface. |
3 | AbstractSequentialList Inherits from AbstractList and provides chained access to data elements instead of random access. |
4 | LinkedList Inherits from AbstractSequentialList and implements a linked list. |
5 | ArrayList By inheriting AbstractList, dynamic arrays are implemented. |
6 | AbstractSet Inherits from AbstractCollection and implements most of the Set interface. |
7 | HashSet Inherits AbstractSet and uses a hash table. |
8 | LinkedHashSet Hash table and linked list implementation of the Set interface with predictable iteration order. |
9 | TreeSet Inherits from AbstractSet and sorts the elements using their natural order. |
10 | AbstractMap Implements most of the Map interface. |
11 | HashMap HashMap is a hash table that stores key-value pairs. HashMap inherits from AbstractMap and implements the Map, Cloneable, and java.io.Serializable interfaces. |
12 | TreeMap Inherits AbstractMap and uses a tree. |
13 | WeakHashMap Inherits the AbstractMap class and uses a hash table with weak keys. |
14 | LinkedHashMap Inherits from HashMap and sorts elements using their natural order. |
IdentityHashMap | Inherit the AbstractMap class and use reference equality when comparing documents. |
Serial number | Class description |
---|---|
1 | Vector The Vector class implements a dynamic array. Similar to ArrayList, but different. |
2 | Stack Stack is a subclass of Vector, which implements a standard last-in-first-out stack. |
3 | Dictionary The Dictionary class is an abstract class used to store key/value pairs, and its function is similar to the Map class. |
4 | Hashtable Hashtable is part of the original java.util and is a specific implementation of Dictionary. |
5 | Properties Properties inherits from Hashtable. Represents a persistent attribute set. Each key and its corresponding value in the attribute list is a string . |
6 | BitSet A Bitset class creates a special type of array to hold bit values. The size of the array in BitSet will increase as needed. |
#A Bitset class creates a special type of array to hold bit values. The size of the array in BitSet will increase as needed.
Collection algorithms
The collection framework defines several algorithms that can be used for collections and mappings. These algorithms are defined as static methods of the collection class.
Some methods can throw ClassCastException when trying to compare incompatible types. When trying to modify an unmodifiable collection, an UnsupportedOperationException is thrown.
The set defines three static variables: EMPTY_SET EMPTY_LIST, EMPTY_MAP. None of these variables can be changed.
Serial number | Algorithm Description |
---|---|
1 |
Collection Algorithms Here is a list of all algorithm implementations. |
How to use iterators
Typically, you will want to iterate over the elements in a collection. For example, display each element in a collection.
The easiest way to do this is to use an iterator, which is an object that implements the Iterator interface or the ListIterator interface.
Iterator allows you to get or delete elements of a collection through looping. ListIterator extends Iterator to allow bidirectional traversal of lists and modification of elements.
All methods provided by the Iterator and listIterator interfaces are listed here through examples. <table class="reference>Serial number | Iterator method description |
---|---|
1 |
Using Java Iterator |
How to use comparators
TreeSet and TreeMap store elements in sorted order. However, it is through the comparator to precisely define what sorting order is followed.
This interface allows us to sort a collection in different ways.
Serial number | Comparator method description |
---|---|
1 |
Using Java Comparator Here are all the methods provided by the Comparator interface through examples |
Summary
The Java collection framework provides programmers with pre-packaged data structures and Algorithms to manipulate them.
A collection is an object that can hold references to other objects. The collection interface declares the operations that can be performed on each type of collection.
The classes and interfaces of the collection framework are in the java.util package.