May 10, 2021 Java
Java provided ad hoc classes long before Java 2. Classes such as Dictionary, Vector, Stack, and Properties are used to store and manipulate groups of objects.
While these classes are useful, they lack a core, unified theme. For this reason, there is a big difference between how you use vector classes and how you use Properties classes.
The collection framework is designed to meet the following goals.
To do this, the entire collection framework is designed around a set of standard interfaces. You can use standard implementations of these interfaces directly, such as LinkedList, HashSet, and TreeSet, and you can implement your own collections through these interfaces.
Simplified diagram:
Description: There are a few explanations for the framework diagram above
The Java collection framework provides a well-performing, easy-to-use interface and class, and the java collection framework is located in the java.util package, so a guide package is required when using the collection framework.
The collection framework defines some interfaces. This section provides an overview of each interface:
Serial number | The interface description |
---|---|
1 |
The Collection interface
Allows you to use a set of objects, which is the root interface of the College hierarchy. |
2 |
The List interface
Elements inherited from Collection and a List instance store an ordered collection. |
3 |
Set
Inherited from Collection, is a collection that does not contain duplicate elements. |
4 |
SortedSet
Inherited from Set's well-preserved 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, keeps Key in ascending order. |
8 |
Enumeration
This is a traditional interface and defined method through which elements in an object collection can be enumered (one at a time). This traditional interface has been replaced by an iterator. |
Java provides a set of standard collection classes that implement the Collection interface. Some of these are specific classes that can be used directly, while others are abstract classes that provide partial implementations of interfaces.
The standard collection classes are summarized in the following table:
Serial number | The class description |
---|---|
1 |
AbstractCollection
Most of the collection interfaces are implemented. |
2 |
AbstractList
Inherited from AbstractCollection and implemented most of the List interface. |
3 |
AbstractSequentialList
Inherited from AbstractList, provides chain access to data elements rather than random access. |
4 |
LinkedList
Inherited from AbstractSequentialList, a list is implemented. |
5 |
ArrayList
Dynamic arrays are implemented by inheriting AbstractList. |
6 |
AbstractSet
Inherited from AbstractCollection and implemented most of the Set interface. |
7 |
HashSet
AbstractSet is inherited and a hash table is used. |
8 |
LinkedHashSet
Hash table and link list implementations for Set interfaces with predictable iteration order. |
9 |
TreeSet
Inherited from AbstractSet, the elements are sorted using their natural order. |
10 |
AbstractMap
Most of the Map interfaces are implemented. |
11 |
HashMap
HashMap is inherited and a hash table is used. |
12 |
TreeMap
Inherited AbstractMap and used a tree. |
13 |
WeakHashMap
Inherit the AbstractMap class, using a hash table with a weak key. |
14 |
LinkedHashMap
Inherited from HashMap, elements are sorted in their natural order. |
15 |
IdentityHashMap
Inherit the AbstractMap class and use references equal when comparing documents. |
The classes defined in the java.util package were discussed in the previous tutorial, as follows:
Serial number | The class description |
---|---|
1 |
Vector
The Vector class implements a dynamic array. Similar to ArrayList, but different. |
2 |
Stack
A stack is a sub-class of Vector that implements a standard, last-in, first-out stack. |
3 |
Dictionary
A Dictionary class is an abstract class that stores key/value pairs that act like a Map class. |
4 |
Hashtable
Hashtable is part of the original java.util and is a Dictionary-specific implementation. |
5 |
Properties
Properties inherits hashtable. represents a persistent set of properties. Each key in the property list and its corresponding value is a string. |
6 |
BitSet
A Bitset class creates an array of special types to hold bit values. The size of the array in BitSet increases as needed. |
A Bitset class creates an array of special types to hold bit values. The size of the array in BitSet increases as needed.
The collection framework defines several algorithms that can be used for collections and mapping. These algorithms are defined as static methods of collection classes.
Some methods can throw
ClassCastException
exceptions when trying to compare incompatible types.
Throws a
UnsupportedOperationException
collection.
A collection defines three static variables: EMPTY_SET EMPTY_LIST, EMPTY_MAP. None of these variables can be changed.
Serial number | The description of the algorithm |
---|---|
1 |
Collection Algorithms
Here is a list of all the algorithm implementations. |
Typically, you want to traverse elements in a collection. For example, each element in the collection is displayed.
The easiest way to do this is to use an iterator, which is an object that implements between the Iterator interface and the ListIterator interface.
Iterators that enable you to get or remove elements of a collection through loops. ListIterator inherits Iterator to allow bidirectional traversal of lists and modified elements.
All the methods provided by the Iterator and listIterator interfaces are listed here by example.