-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
337 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,337 @@ | ||
--- | ||
title: "DSA - Data Structures" | ||
description: "Data Structures in Java" | ||
date: 2024-09-26T12:00:00+01:00 | ||
draft: false | ||
--- | ||
|
||
Overview of the most useful data structures from the Java Collections API | ||
|
||
# Arrays | ||
|
||
**Array** | ||
|
||
{{< highlight java >}} | ||
E[] array = new E[10]; | ||
int n = array.length; | ||
|
||
E element = array[0]; | ||
array[0] = element; | ||
|
||
array.add(element); | ||
array.add(index, element); | ||
array.addAll(collection); | ||
array.addAll(index, collection); | ||
|
||
E element = array.get(index); | ||
|
||
E removed = array.remove(index); | ||
array.clear(); | ||
|
||
boolean isEmpty = array.isEmpty(); | ||
boolean contains = array.contains(element); | ||
int index = array.indexOf(element); | ||
|
||
Arrays.sort(array); | ||
Arrays.sort(array, Collections.reverseOrder()); | ||
Arrays.sort(array, (E e1, E e2) -> e1.compareTo(e2)); | ||
|
||
int index = Arrays.binarySearch(array, element); | ||
{{< / highlight >}} | ||
|
||
**Running times**: | ||
|
||
| Operation | Time Complexity | | ||
| --------- | --------------- | | ||
| Get | O(1) | | ||
| IndexOf | O(n) | | ||
| Add | O(n) | | ||
| Remove | O(n) | | ||
|
||
--- | ||
|
||
# Dynamic Array | ||
|
||
**ArrayList** | ||
|
||
{{< highlight java >}} | ||
ArrayList<E> list = new ArrayList<>(); | ||
int n = list.size(); | ||
|
||
list.add(element); | ||
list.add(index, element); | ||
list.addAll(collection); | ||
list.addAll(index, collection); | ||
|
||
E element = list.get(index); | ||
|
||
E removed = list.remove(index); | ||
list.clear(); | ||
|
||
boolean isEmpty = list.isEmpty(); | ||
boolean contains = list.contains(element); | ||
int index = list.indexOf(element); | ||
|
||
Collections.sort(list); | ||
Collections.sort(list, Collections.reverseOrder()); | ||
Collections.sort(list, (E e1, E e2) -> e1.compareTo(e2)); | ||
|
||
int index = Collections.binarySearch(list, element); | ||
{{< / highlight >}} | ||
|
||
**Running times**: | ||
|
||
| Operation | Time Complexity | | ||
| --------- | --------------- | | ||
| Get | O(1) | | ||
| IndexOf | O(n) | | ||
| Add | O(1) | | ||
| Remove | O(n) | | ||
|
||
--- | ||
|
||
# Lists | ||
|
||
**LinkedList** | ||
|
||
{{< highlight java >}} | ||
LinkedList<E> list = new LinkedList<>(); | ||
int n = list.size(); | ||
|
||
list.add(element); | ||
list.add(index, element); | ||
list.addAll(collection); | ||
list.addAll(index, collection); | ||
|
||
E element = list.get(index); | ||
|
||
E removed = list.remove(index); | ||
list.clear(); | ||
|
||
boolean isEmpty = list.isEmpty(); | ||
boolean contains = list.contains(element); | ||
int index = list.indexOf(element); | ||
|
||
Collections.sort(list); | ||
Collections.sort(list, Collections.reverseOrder()); | ||
Collections.sort(list, (E e1, E e2) -> e1.compareTo(e2)); | ||
|
||
int index = Collections.binarySearch(list, element); | ||
{{< / highlight >}} | ||
|
||
**Running times**: | ||
|
||
| Operation | Time Complexity | | ||
| --------- | --------------- | | ||
| Get | O(n) | | ||
| IndexOf | O(n) | | ||
| Add | O(n) | | ||
| Remove | O(n) | | ||
|
||
--- | ||
|
||
# Stacks | ||
|
||
**LinkedList** | ||
|
||
{{< highlight java >}} | ||
LinkedList<E> stack = new LinkedList<>(); | ||
|
||
stack.push(element); | ||
E top = stack.peek(); | ||
E popped = stack.pop(); | ||
|
||
boolean isEmpty = stack.isEmpty(); | ||
int size = stack.size(); | ||
{{< / highlight >}} | ||
|
||
**Running times**: | ||
|
||
| Operation | Time Complexity | | ||
| --------- | --------------- | | ||
| Push | O(1) | | ||
| Peek | O(1) | | ||
| Pop | O(1) | | ||
|
||
--- | ||
|
||
# Queues | ||
|
||
**LinkedList** | ||
|
||
{{< highlight java >}} | ||
LinkedList<E> queue = new LinkedList<>(); | ||
|
||
queue.offer(element); | ||
E head = queue.peek(); | ||
E removed = queue.poll(); | ||
|
||
boolean isEmpty = queue.isEmpty(); | ||
int size = queue.size(); | ||
{{< / highlight >}} | ||
|
||
**Running times**: | ||
|
||
| Operation | Time Complexity | | ||
| --------- | --------------- | | ||
| Offer | O(1) | | ||
| Peek | O(1) | | ||
| Poll | O(1) | | ||
|
||
**PriorityQueue** (Heap Implementation) | ||
|
||
{{< highlight java >}} | ||
PriorityQueue<E> priorityQueue = new PriorityQueue<>(); | ||
PriorityQueue<E> priorityQueue = new PriorityQueue<>(Collections.reverseOrder()); | ||
PriorityQueue<E> priorityQueue = new PriorityQueue<>((E e1, E e2) -> e1.compareTo(e2)); | ||
int size = priorityQueue.size(); | ||
|
||
priorityQueue.offer(element); | ||
E head = priorityQueue.peek(); | ||
E removed = priorityQueue.poll(); | ||
|
||
boolean isEmpty = priorityQueue.isEmpty(); | ||
boolean contains = priorityQueue.contains(element); | ||
{{< / highlight >}} | ||
|
||
**Running times**: | ||
|
||
| Operation | Time Complexity | | ||
| --------- | --------------- | | ||
| Offer | O(log n) | | ||
| Peek | O(1) | | ||
| Poll | O(log n) | | ||
|
||
--- | ||
|
||
# Sets | ||
|
||
**HashSet** | ||
|
||
{{< highlight java >}} | ||
HashSet<E> set = new HashSet<>(); | ||
int n = set.size(); | ||
|
||
set.add(element); | ||
|
||
set.remove(element); | ||
set.clear(); | ||
|
||
boolean isEmpty = set.isEmpty(); | ||
boolean contains = set.contains(element); | ||
{{< / highlight >}} | ||
|
||
**Running times**: | ||
|
||
| Operation | Time Complexity | | ||
| --------- | -------------------------------------------- | | ||
| Add | O(1) (degrades to O(n) with hash collisions) | | ||
| Remove | O(1) (degrades to O(n) with hash collisions) | | ||
| Contains | O(1) (degrades to O(n) with hash collisions) | | ||
|
||
**TreeSet** | ||
|
||
{{< highlight java >}} | ||
TreeSet<E> set = new TreeSet<>(); | ||
TreeSet<E> set = new TreeSet<>(Collections.reverseOrder()); | ||
TreeSet<E> set = new TreeSet<>((E e1, E e2) -> e1.compareTo(e2)); | ||
int n = set.size(); | ||
|
||
set.add(element); | ||
|
||
E first = set.first(); | ||
E last = set.last(); | ||
|
||
E before = set.lower(element); | ||
E after = set.higher(element); | ||
|
||
set.remove(element); | ||
set.clear(); | ||
|
||
SortedSet<E> sub = set.subSet(fromElement, fromInclusive, toElement, toInclusive); | ||
SortedSet<E> tail = set.tailSet(fromElement, inclusive); | ||
SortedSet<E> head = set.headSet(toElement, inclusive); | ||
|
||
boolean isEmpty = set.isEmpty(); | ||
boolean contains = set.contains(element); | ||
{{< / highlight >}} | ||
|
||
**Running times**: | ||
|
||
| Operation | Time Complexity | | ||
| --------- | --------------- | | ||
| Add | O(log n) | | ||
| Remove | O(log n) | | ||
| Contains | O(log n) | | ||
|
||
--- | ||
|
||
# Maps | ||
|
||
**HashMap** | ||
|
||
{{< highlight java >}} | ||
HashMap<K, V> map = new HashMap<>(); | ||
int n = map.size(); | ||
|
||
map.put(key, value); | ||
|
||
V value = map.get(key); | ||
|
||
map.remove(key); | ||
map.clear(); | ||
|
||
boolean isEmpty = map.isEmpty(); | ||
boolean containsKey = map.containsKey(key); | ||
boolean containsValue = map.containsValue(value); | ||
{{< / highlight >}} | ||
|
||
**Running times**: | ||
|
||
| Operation | Time Complexity | | ||
| ------------- | -------------------------------------------- | | ||
| Put | O(1) (degrades to O(n) with hash collisions) | | ||
| Get | O(1) (degrades to O(n) with hash collisions) | | ||
| Remove | O(1) (degrades to O(n) with hash collisions) | | ||
| ContainsKey | O(1) (degrades to O(n) with hash collisions) | | ||
| ContainsValue | O(n) | | ||
|
||
**TreeMap** | ||
|
||
{{< highlight java >}} | ||
TreeMap<K, V> map = new TreeMap<>(); | ||
TreeMap<K, V> map = new TreeMap<>(Collections.reverseOrder()); | ||
TreeMap<K, V> map = new TreeMap<>((K k1, K k2) -> k1.compareTo(k2)); | ||
int n = map.size(); | ||
|
||
map.put(key, value); | ||
|
||
V value = map.get(key); | ||
|
||
K firstKey = map.firstKey(); | ||
K lastKey = map.lastKey(); | ||
|
||
K beforeKey = map.lowerKey(key); | ||
K afterKey = map.higherKey(key); | ||
|
||
map.remove(key); | ||
map.clear(); | ||
|
||
NavigableMap<K, V> sub = map.subMap(fromKey, fromInclusive, toKey, toInclusive); | ||
NavigableMap<K, V> tail = map.tailMap(fromKey, inclusive); | ||
NavigableMap<K, V> head = map.headMap(toKey, inclusive); | ||
|
||
boolean isEmpty = map.isEmpty(); | ||
boolean containsKey = map.containsKey(key); | ||
boolean containsValue = map.containsValue(value); | ||
{{< / highlight >}} | ||
|
||
**Running times**: | ||
|
||
| Operation | Time Complexity | | ||
| ------------- | --------------- | | ||
| Put | O(log n) | | ||
| Get | O(log n) | | ||
| Remove | O(log n) | | ||
| ContainsKey | O(log n) | | ||
| ContainsValue | O(n) | |