data-structures
Array
Fixed sized list for storing elements of the same data type
int[] sizeThreeArray = new int[3];
myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 3;
int[] sizeThreeArrayWithElements = {1, 2, 3};ArrayList
Dynamic sized array for storing elements of the same data type
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
// Access elements
int thirdCar = cars.get(2);
// Changing element from index 1
cars.set(1, "Toyota");
// Get size
int size = myList.size();
// Check contains element
boolean found = myList.contains("Mazda");
// Remove element
myList.remove("Ford");
// Sorting elements (alphabetically/numerically)
Collections.sort(myList);
// Iterating over elements using a for-each loop
for (int element : myList) {
System.out.print(element + " ");
}
// Clearing the ArrayList
myList.clear();Linked List
Consists of a sequence of nodes, where each node stores a data element and a reference (or link) to the next node in the sequence
Node
Contains data & pointer to next node
Head
Reference to first node of the linked list
Tail
Reference to last node of the linked list
Singly Linked List
Only allows traversal in one direction , head to tail
Doubly Linked List
Allows bidirectional traversal, each node has references to both the next and the previous nodes
Implement using java.util.LinkedList
Stack
Last In First Out (LIFO) Data Structure
Implement using an java.util.ArrayList :
Implement using java.util.Stack
Queue
First In First Out (FIFO) Data Structure
Implement using java.util.LinkedList:
Implement using java.util.ArrayDeque:
Hash Map
Store data in key:value pair format
Hash Set
Store unique values in no particular order
Binary Tree
Hierarchical data structure consisting of nodes, where each node has at most two children: a left child and a right child
Heap
A specialized tree-based data structure that satisfies the heap property
Heap property:
min heap , root is min value
max heap , root is max value
Last updated