The underlying principle of Java collections


First, the architecture of the collection framework

All collection classes in the Java Collections Framework are defined by the following interface:

  • Collection interface: Collection is the root interface in all collection frameworks. It defines the basic operations of the collection, such as adding, deleting, traversing, etc.

  • List interface: The List interface extends the Collection interface, which allows the repetition of ordered elements.

  • Set interface: The Set interface extends the Collection interface, which does not allow duplication of ordered elements.

  • Map interface: The Map interface is a collection of key-value pairs, and each key can only be mapped to one value. Both keys and values ​​can be arbitrary objects.

Every class in the Java Collections Framework implements one or more of these interfaces.

Second, the classes in the collection framework

  • ArrayList Class
    The ArrayList class implements the List interface, which uses an array as an internal storage structure. It automatically increases the size of the array when the number of elements increases. Therefore, the ArrayList class has the ability of random access.

  • LinkedList class
    The LinkedList class implements the List interface, which uses a linked list as an internal storage structure. Therefore, the LinkedList class has the ability to insert and delete elements.

  • HashSet class
    The HashSet class implements the Set interface, which uses a hash table as an internal storage structure. A hash table is a hash table that stores elements in key-value pairs. The HashSet class has the ability to quickly find elements.

  • TreeSet class
    The TreeSet class implements the Set interface, which uses a tree as an internal storage structure. Since trees are ordered, the TreeSet class has the ability to sort elements.

  • HashMap Class The
    HashMap class implements the Map interface, which uses a hash table as its internal storage structure. The HashMap class uses key-value pairs to store elements, and can quickly find the corresponding value through the key.

  • The TreeMap class
    The TreeMap class implements the Map interface, which uses a tree as an internal storage structure. Since trees are ordered, the TreeMap class has the ability to sort elements.

3. Algorithms in the collection framework

Algorithms in the Java collections framework are iterator-based. An iterator is an object that can be used to iterate over the elements in a collection. The following are some commonly used algorithms:

  • for-each loop: The for-each loop is a simple way to iterate over the elements in a collection, it traverses each element in the collection without explicitly using an iterator.

  • Iterator method: Each collection class in the Java collection framework implements the Iterator interface, which provides some methods, such as hasNext and next, that can be used to traverse the elements in the collection.

  • Sorting methods: The Collections class in the Java collection framework provides sorting methods, such as sort and reverse, which can sort and reverse the elements in the collection.

  • Search methods: The Collections class in the Java collection framework provides search methods, such as binarySearch and indexOf, which can be used to find elements in a collection.

Fourth, the underlying implementation principle of the collection framework

Various data structures in the Java collection framework, such as arrays, linked lists, hash tables, and trees, are implemented at the bottom. The following are some common underlying implementation principles:

  • Array: An array in Java is a fixed-length data structure that stores elements in contiguous blocks in memory. When the number of elements in the array exceeds the size of the array, a larger array needs to be recreated, and the elements in the original array are copied to the new array.

  • Linked List: A linked list in Java is a data structure composed of nodes, each node contains an element and a reference to the next node. When inserting or deleting elements, you only need to modify the references between adjacent nodes.

  • Hash table: A hash table in Java is a data structure composed of buckets, each bucket contains a set of key-value pairs. When adding an element, the hash table will calculate the position of the bucket it is in based on the key value of the element, and add the element to the bucket. When an element needs to be looked up, the hash table will calculate the position of the bucket it is in based on the key value of the element, and look up the element in the bucket.

  • Tree: A tree in Java is a data structure composed of nodes, each node contains an element and one or more child nodes. Trees can be classified into binary trees and multi-fork trees. In a binary tree, each node has at most two child nodes, while in a multi-tree, each node can have any number of child nodes. When elements are inserted or removed, the structure of the tree needs to be rebalanced to keep the height of the tree balanced.

V. Summary

The Java collection framework provides many commonly used data structures and algorithms, all of which are implemented based on iterators. Different data structures use different implementation principles at the bottom, such as arrays, linked lists, hash tables, and trees. Understanding the underlying implementation principles of the Java collection framework can help us better use and understand the collection framework.

Guess you like

Origin blog.csdn.net/qq_54351538/article/details/129337434