Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Java collection framework


May 10, 2021 Java


Table of contents


Java collection framework

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.

  • The framework must be high-performance. The implementation of basic collections (dynamic arrays, lists, trees, hash tables) must also be efficient.
  • The framework allows different types of collections to work in a similar way and have a high degree of interoperability.
  • The extension and adaptation of a collection must be simple.

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.

Java collection framework

Simplified diagram:

Java collection framework

Description: There are a few explanations for the framework diagram above

  1. All collection classes are located under the java.util package. Java's collection classes are derived from two main interfaces: Collection and Map, and Map are the root interfaces of the Java collection framework, which in turn contain sub-interfaces or implementation classes.
  2. Collection interfaces: 6 interfaces (short dashed), representing different collection types, are the basis of the collection framework.
  3. Abstract classes: 5 abstract classes (represented by long dashed lines), partial implementations of the collection interface. It can be extended to a custom collection class.
  4. Implementation class: 8 implementation classes (solid line), the specific implementation of the interface.
  5. The Collection interface is a set of objects that allow duplication.
  6. The Set interface inherits Collection, and collection elements are not duplicated.
  7. The List interface inherits Collection, allowing duplication and maintaining the order in which elements are inserted.
  8. The Map interface is a key-value object and has nothing to do with the Action interface.
  9. Set, List, and Map can be seen as three categories of collections:
    A List collection is an ordered collection in which elements in a collection can be duplicated, and elements in an access collection can be accessed based on the index of the elements.
    Set collections are out-of-order collections where elements in a collection cannot be duplicated, and elements in the access collection can only be accessed based on the elements themselves (which is why elements in the collection are not allowed to repeat).
    Elements in the Map collection that are in the form of Key-value pairs can only be accessed based on the key of each element.

The collection frame diagram looks like this:

Java collection framework

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 interface

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.

The collection class

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 algorithm

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.

How to use the iterator

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.