Java Syntax - Common Data Structures Flashcards
Array definition
An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.
Initialize a one dimensional array that can hold 10 integers
int[] myArr = new int[10];
Initialize a two dimensional array of integers with 10 rows and 20 columns
int[][] myArr = new int[10][20];
Initialize an array containing the integers 1, 2, and 3
int[] myArr = new int[]{1, 2, 3};
or
int[] myArr = {1, 2, 3};
Initialize a two dimensional array with 3 rows and 3 columns:
1, 2, 3
4, 5, 6
7, 8, 9
int[][]myArr={{1,2,3},{4,5,6},{7,8,9}}
Iterate through an array
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
}
for (int i : array) {
System.out.print(array[i]);
}
String creation
String s = new String(“hello”);
String s = “hello”;
String size
s.length()
Accessing and iterating through characters in a String, s.
char[] charArray = s.toCharArray();
for (int i = 0; i < charArray.length; i++) {
System.out.print(charArray[i]);
}
for (int i = 0; i < s.length(); i++) {
System.out.print(s.charAt(i));
}
HashMap definition
A HashMap is a data structure that maps keys to values. A map cannot contain duplicate keys and each key can map to at most one value.
HashMap
- Create a new HashMap mapping String to String
- Add an element
- Update element
- Remove element
- Size
- Iterate through entry set
- Iterate through key set
- Iterate through values
- Create a new HashMap:
HashMap map = new HashMap<>();
- Add an element:
map. put(“key”, “value”); - Update element:
map. put(“key”, map.getOrDefault(“key”, “updatedValue”); - Remove element:
map. remove(“key”); - Size
map. size(); - Iterate through entry set
for (Map.Entry entry : map.entrySet()) { System.out.println(entry.getKey() + “ “ +
entry.getValue());
}
- Iterate through key set
for (String key : map.keySet()) {
System.out.println(key);
}
- Iterate through values
for (String value : map.values()) {
System.out.println(value);
}
HashMap Time Complexity
- Access
- Search
- Insert
- Remove
- Access: O(1)
- Search: O(n)
- Insert: O(1)
- Remove: O(1)
HashSet definition
A HashSet is a collection that uses a Hash table for storage, only allowing unique elements to be added.
HashSet
- Create a new HashSet of String
- Add an element
- Remove element
- Search element
- Size
- Iterate through set
- Create a new HashSet of String:
HashSet set = new HashSet<>();
- Add an element:
set. add(“hello”); - Remove element:
set. remove(“hello”); - Search element:
set. contains(“hello”); - Size:
set. size(); - Iterate through set:
for(String s : set) {
System.out.println(s);
}
HashSet Time Complexity
- Access
- Search
- Insert
- Remove
- Access: O(1)
- Search: O(1)
- Insert: O(1)
- Remove: O(1)
ArrayList definition
An ArrayList is a collection of data elements sequentially ordered from 0 to length - 1. This means that we are able to access an element inside an ArrayList by its position (index).
ArrayList
- Create ArrayList of Integer
- Add element
- Update element
- Remove element
- Size
- Accessing
- Sorting
- Create ArrayList of Integer
ArrayList list = new ArrayList<>();
List list = new ArrayList<>();
- Add element
list. add(1); - Update element
list. set(0, 100); // Update index 0’s value to 100 - Remove element
list. remove(0); // Remove index 0
list. clear(); // Remove all elements
- Size
list. size(); - Accessing
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
for (String s : list) {
System.out.println(s);
}
- Sorting
Collections.sort(list); // Sort ascending
Collections.sort(list, Collections.reverseOrder()); // Sort descending
ArrayList Time Complexity
- Access
- Search
- Insert
- Remove
- Access: O(1)
- Search: O(n)
- Insert: O(1) (at the back of the ArrayList)
- Remove: O(n)
O(1) (removing from the back of the list)
Heap definition
A heap is a specialized tree based structure data structure that satisfies the heap property: if A is a parent node of B, then the key (the value) of node A is ordered with respect to the key of node B with the same ordering applying across the entire heap.
A heap can be classified further as either a “max heap” or a “min heap”. In a max heap, the keys of parent nodes are always greater than or equal to those of the children and the highest key is in the root node. In a min heap, the keys of parent nodes are less than or equal to those of the children and the lowest key is in the root node.
Heap
- Create a min heap of Integer
- Create a max heap of Integer
- Add element
- View top element
- Remove top element
- Size
- Create a min heap of Integer
PriorityQueue pq = new PriorityQueue<>(); // Min heap by default
- Create a max heap of Integer
PriorityQueue pq = new PriorityQueue<>(Collections.reverseOrder);
- Add element
pq. add(10); - View top element
pq. peek(); - Remove top element
pq. poll(); - Size
pq. size();
Heap Time Complexity
- Access min or max
- Insert
- Remove min or max
- Access min or max: O(1)
- Insert: O(log(n))
- Remove min or max: O(log(n))
Queue definition
A Queue is a collection of elements, supporting two principle operations: enqueue, which inserts an element into the queue, and dequeue, which removes an element from the queue.
Queue
- Create a new Queue of Integer
- Add element
- View top element
- Remove element
- Size
- Empty check
- Create a new Queue of Integer
Queue q = new LinkedList<>(); // Specify as a LinkedList!
- Add element
q. add(10); - View top element
q. peek(); // Returns head or null if empty - Remove element
q. poll(); // Returns head or null if empty - Size
q. size(); - Empty check
q. isEmpty();
Queue Time Complexity
- Access
- Search
- Insert
- Remove
- Access: O(n)
- Search: O(n)
- Insert: O(1)
- Remove: O(1)
Stack definition
A Stack is a collection of elements, with two principle operations: push, which adds to the collection, and pop, which removes the most recently added element.
Stack
- Create a Stack of Integers
- Add an element
- View top element
- Remove element
- Size
- Create a Stack of Integers
Stack stack = new Stack<>();
- Add an element
stack. push(10); - View top element
stack. peek(); // Returns but does not remove the top element - Remove element
stack. pop(); // Returns an removes the top element - Size
stack. size(); - Empty check
stack. isEmpty();
Stack Time Complexity
- Access
- Search
- Insert
- Remove
- Access: O(n)
- Search: O(n)
- Insert: O(1)
- Remove: O(1)
Linked List definition
A Linked List is a linear collection of data elements, called nodes, each pointing to the next node by means of a pointer. It is a data structure consisting of a group of nodes which together represent a sequence. The Java LinkedList collection is a doubly linked list.
Linked List
- Create a linked list of Integers
- Add element
- Update element
- Remove element
- Size
- Accessing
- Create a linked list of Integers
LinkedList list = new LinkedList<>();
- Add element
list. add(1); - Update element
list. set(0, 100); // Update index 0’s value to 100 - Remove element
list. remove(0); // Remove index 0
list. clear(); // Remove all elements
- Size
list. size(); - Accessing
for(int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
for(int i : list) {
System.out.println(i);
}
Linked List Time Complexity
- Access
- Search
- Insert
- Remove
- Access: O(n)
- Search: O(n)
- Insert: O(1) // At the head or tail
- Remove: O(1) // At the head or tail