Java Data Structures
Java Data Structures
Java provides rich data structures to process and organize data.
The java.util package of Java provides many implementations of these data structures; you can choose the appropriate class as needed.
The following are some common Java data structures:
Arrays
int[] array = new int[5];
- Characteristics: An array is a basic data structure that can store a fixed-size collection of elements of the same type.
- Fixed size, stores elements of the same type. Advantages:
- High efficiency in random access of elements. Disadvantages:
Fixed size; insertion and deletion of elements are relatively slow.
Lists
List arrayList = new ArrayList<>();
List linkedList = new LinkedList<>();
ArrayList:
- Characteristics: Java provides various list implementations, such as ArrayList and LinkedList.
- Fixed size, stores elements of the same type. Dynamic array, variable size.
- High efficiency in random access of elements. Efficient random access and fast tail insertion.
LinkedList:
- Characteristics: Insertion and deletion in the middle are relatively slow.
- Fixed size, stores elements of the same type. Doubly linked list, elements connected via pointers.
- High efficiency in random access of elements. Efficient insertion and deletion of elements, good iterator performance.
Random access is relatively slow.
Sets
Set hashSet = new HashSet<>();
Set treeSet = new TreeSet<>();
HashSet:
- Characteristics: Sets are used to store non-duplicate elements; common implementations are HashSet and TreeSet.
- Fixed size, stores elements of the same type. Unordered set, implemented based on HashMap.
- High efficiency in random access of elements. Efficient lookup and insertion operations.
TreeSet:
- Order not guaranteed.
- **Features:** TreeSet is an ordered set, implemented based on a red-black tree at the bottom, and does not allow duplicate elements.
- ** Advantages:** Provides automatic sorting, suitable for scenarios that need to store elements in order.
** Disadvantages:** Relatively poor performance, does not allow inserting null elements.
Maps
Map hashMap = new HashMap<>();
Map treeMap = new TreeMap<>();
HashMap:
- Characteristics: Maps are used to store key-value pairs; common implementations are HashMap and TreeMap.
- Fixed size, stores elements of the same type. A key-value storage structure implemented based on a hash table.
- High efficiency in random access of elements. Efficient lookup, insertion, and deletion operations.
TreeMap:
- Characteristics: Unordered, order not guaranteed.
- Fixed size, stores elements of the same type. An ordered key-value storage structure implemented based on a red-black tree.
- High efficiency in random access of elements. Ordered, supports traversal in key order.
Stack
Insertion and deletion are relatively slow.
Stack stack = new Stack<>();
A Stack is a linear data structure that manages elements according to the Last In, First Out (LIFO) principle. In a stack, new elements are added to the top of the stack, and elements can only be removed from the top of the stack. This means the last added element is the first to be removed.
- Characteristics: Stack class:
Queue
Represents a stack, usually operating elements in Last In, First Out (LIFO) order.
Queue queue = new LinkedList<>();
A Queue follows the First In, First Out (FIFO) principle; common implementations are LinkedList and PriorityQueue.
- Characteristics: Queue interface:
- Represents a queue, usually operating elements in First In, First Out (FIFO) order. LinkedList, PriorityQueue, ArrayDeque.
Implementation classes:
Heap
PriorityQueue minHeap = new PriorityQueue<>();
PriorityQueue maxHeap = new PriorityQueue<>(Collections.reverseOrder());
A Heap is the basis of a priority queue, and can implement max-heaps and min-heaps.
Trees
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
Java provides the TreeNode type, which can be used to build binary trees and other data structures.
Graphs
Representing a graph usually requires a custom data structure or a graph library; Java has no built-in graph class.
The above only introduces some common data structures in Java; in fact, there are many other data structures and algorithms that can be chosen based on specific problems.
Some Other Notes
The following classes are legacy; a new framework—the collection framework (Collection)—was introduced in Java 2, which we will discuss later.
Enumeration
Although the Enumeration interface itself does not belong to a data structure, it is widely used in the context of other data structures. The Enumeration interface defines a way to retrieve successive elements from a data structure.
For example, Enumeration defines a method called nextElement, which is used to get the next element of a data structure containing multiple elements.For more information about the Enumeration interface,.
please see Enumeration
BitSet
The BitSet class implements a set of bits or flags that can be set and cleared individually.
This class is very useful when handling a group of boolean values; you only need to assign one "bit" to each value, then set or clear the bits appropriately to operate on the booleans.For more information about this class,.
please see BitSet
Vector
The Vector class is very similar to traditional arrays, but the size of a Vector can change dynamically as needed.
Like arrays, the elements of a Vector object can also be accessed via an index.
This class is very useful when handling a group of boolean values; you only need to assign one "bit" to each value, then set or clear the bits appropriately to operate on the booleans.The main benefit of using the Vector class is that you do not need to specify a size when creating the object; its size changes dynamically as needed.
Stack
Please see Vector
Stack implements a Last In, First Out (LIFO) data structure.
You can think of a stack as a vertically distributed stack of objects; when you add a new element, you place it on top of the other elements.
This class is very useful when handling a group of boolean values; you only need to assign one "bit" to each value, then set or clear the bits appropriately to operate on the booleans.When you take an element from the stack, you take it from the top. In other words, the last element pushed onto the stack is the first to be taken out..
Please see Stack
Dictionary
The Dictionary class is an abstract class that defines a data structure mapping keys to values.
When you want to access data by a specific key rather than an integer index, you should use Dictionary.
This class is very useful when handling a group of boolean values; you only need to assign one "bit" to each value, then set or clear the bits appropriately to operate on the booleans.Because the Dictionary class is abstract, it only provides the key-to-value data structure and does not provide a specific implementation..
Please see Dictionary
The Dictionary class has been deprecated in newer Java versions; it is recommended to use the Map interface and its implementation classes, such as HashMap and TreeMap, to replace Dictionary.At this point, the trimToSize() method can remove the unallocated space and change the capacity of the ArrayList so that it equals the number of elements in the ArrayList..
For the Map interface and its implementation classes, refer to:
Hashtable
The Hashtable class provides a means to organize data based on a user-defined key structure.
For example, in a hash table of an address list, you can store and sort data using the postal code as the key, rather than by person name.
This class is very useful when handling a group of boolean values; you only need to assign one "bit" to each value, then set or clear the bits appropriately to operate on the booleans.The specific meaning of hash table keys depends entirely on the hash table's usage scenario and the data it contains..
Please see Hashtable
Properties
Properties inherits from Hashtable. The Properties class represents a persistent set of properties. Each key and its corresponding value in the property list is a string.
This class is very useful when handling a group of boolean values; you only need to assign one "bit" to each value, then set or clear the bits appropriately to operate on the booleans.The Properties class is used by many Java classes. For example, when getting environment variables, it serves as the return value of the System.getProperties() method..