In Java, commonly used data structures can be used throughCollections FrameworkImplementation, or manually. The following are common data structures and their characteristics, as well as corresponding Java implementation examples:
1. Array
- Features: Fixed size, continuous memory, and efficient random access.
- use: Stores a fixed number of elements.
Java implementation:
int[] array = new int[5]; // Static arrayarray[0] = 10;
2. Dynamic array
(ArrayList)
- Features: Based on array implementation, supports dynamic expansion, efficient random access (O(1)), and inefficient insertion/deletion (O(n)).
- use: Scenarios that require frequent random access.
Java implementation:
import ; ArrayList<Integer> list = new ArrayList<>(); (10); // Add elementsint value = (0); // Access elements
3. LinkedList
- Features: Based on bidirectional linked list implementation, insertion/deletion efficient (O(1)), random access inefficient (O(n)).
- use: Frequently inserted/deleted scenes.
- Java implementation:
import ; LinkedList<Integer> linkedList = new LinkedList<>(); (10); // Add elements(5); // Head insertionint first = (); // Access head elements
4. Stack
-
Features:Last in first out (LIFO), supported
push
andpop
operate. - use: Function call stack, expression evaluation.
-
Java implementation(Recommended use
Deque
):
import ; ArrayDeque<Integer> stack = new ArrayDeque<>(); (10); // Press the stackint top = (); // Bullet stack
5. Queue
-
Features: First-in-first-out (FIFO), supported
offer
andpoll
operate. - use: Task scheduling, Broad-first search (BFS).
- Java implementation:
import ; import ; Queue<Integer> queue = new LinkedList<>(); (10); // Join the teamint head = (); // Departure
6. Hash table (HashMap)
- Features: Based on hash table implementation, key-value pair storage, efficient search (average O(1)), disordered.
- use: Quickly search and remove heavy loads.
- Java implementation:
import ; HashMap<String, Integer> map = new HashMap<>(); ("Alice", 25); // Add key-value pairsint age = ("Alice"); // Find
7. Tree (TreeSet/TreeMap)
- Features: Based on the red and black tree implementation, the elements are automatically sorted (in natural order or custom comparator), and the time complexity of the search/insert/delete is O(log n).
- use: Scenarios that require orderly storage.
- Java implementation:
import ; TreeSet<Integer> treeSet = new TreeSet<>(); (10); (5); // Automatically sort as [5, 10]
8. Heap (PriorityQueue)
- Features: Based on the heap (default minimum heap), elements are sorted by priority.
- use: Task scheduling and Top K problem.
- Java implementation:
import ; PriorityQueue<Integer> heap = new PriorityQueue<>(); (10); (5); // The top of the pile is 5int min = (); // Pop up the minimum value 5
9. Graph
- Features: A collection of nodes and edges, usually implemented through an adjacency table or an adjacency matrix.
- use: Social network, path planning.
- Java implementation(Adjacent table):
import .*; class Graph { private Map<Integer, List<Integer>> adjacencyList = new HashMap<>(); public void addEdge(int src, int dest) { (src, k -> new ArrayList<>()).add(dest); (dest, k -> new ArrayList<>()).add(src); // Undirected graph } }
10. Set
- Features: Repeat elements are not allowed.
-
Java implementation(
HashSet
andTreeSet
):
import ; HashSet<String> set = new HashSet<>(); ("Apple"); ("Apple"); // Repeated elements will be ignored
11. Deque
- Features: Supports insertion and deletion at both ends.
- use: Sliding window, dual-end operation.
- Java implementation:
import ; ArrayDeque<Integer> deque = new ArrayDeque<>(); (10); (20); int first = ();
12. Custom linked list (manual implementation)
class Node { int data; Node next; Node(int data) { = data; = null; } } class LinkedList { Node head; public void add(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; } else { Node current = head; while ( != null) { current = ; } = newNode; } } }
Summarize
Java provides rich built-in data structures (throughPackage), developers can choose the appropriate structure according to their needs:
-
Quick search:
HashMap
、HashSet
-
Orderly storage:
TreeMap
、TreeSet
- Efficient insertion/deletion:
LinkedList
、ArrayDeque
- Dynamic expansion:
ArrayList
- Priority processing:
PriorityQueue
Mastering the characteristics and usage scenarios of these data structures can significantly improve code efficiency and maintainability!
This is the end of this article about Java's manual implementation of common data structures. For more relevant Java common data structure content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!