Data Structures
Stack
Stack
class from Collections Framework class can be used.
Creating
Stack<Integer> stack = new Stack<>();
Pushing
stack.push(i)
Popping
Integer i = stack.pop()
Peeking
Returns last element without removing.
Integer i = stack.peek()
Searching
Returns position of the element.
- Position starts from 1(not 0) and is counted from the topmost(last) element.
- Returns -1 if element is not found.
Integer pos = stack.search(element);
Heap
Heap is implemented using PriorityQueue in Java.
Creating
PriorityQueue<T> minHeap = new PriorityQueue<T>();
PriorityQueue<T> maxHeap = new PriorityQueue<T>(Collections.reverseOrder());
Inserting
heap.add(element);
Heapify
Adding elements from any collection or data structure to Priority Queue heapifies them.
heap.addAll(Arrays.stream(stones).boxed().toList());
Retrieval
For getting the top-most element without deleting.
heap.peek();
Delete
Deletes and returns the top-most element.
heap.poll();