Java Syntax Notes
Primitive Types
Array
String
Collections
HashMap
HashSet
ArrayList
Heap
Queue
Stack
Linked List
1
Primitive Types
Array
One dimensional: int[] myArr = new int[10]; // Integer array of 10 elements
Two dimensional: int[][] myArr = new int[10][20]; // 10 rows, 20 columns
Array literals: int[] myArr = new int[]{1, 2, 3}; // Length calculated during creation
int[] myArr = {1, 2, 3};
int[][] myArr = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} } // 3 rows & cols
Accessing: for (int i = 0; i < [Link]; i++) { [Link](myArr[i]; }
for (int i : myArr) { [Link](i); }
String
Creation: String gopha = new String(“ok”);
String literal: String gopha = “ok”;
Size: [Link]();
Accessing: char[] chArr = [Link]();
for (char c : chArr) { [Link](c); }
for (int i = 0; i < [Link](); i++) { [Link]([Link](i)); }
2
Collections
HashMap
A data structure that maps keys to values. A map cannot contain duplicate keys and each key
can map to at most one value.
Import required: import [Link];
Creation: HashMap<String, String> hm = new HashMap<>();
Add element: [Link](“gopha”, “ok”); // Key is “gopha”, value is “ok”
Update element: [Link](“gopha”, [Link](“gopha”, “run”));
* Note: Attempts to retrieve the value for the key “gopha” . If not present,
“run” will be used instead and saved for the respective key of “gopha”
Remove element: [Link](“gopha”); // Specify key to remove the entire entry
Size: [Link]();
Accessing: for ([Link]<String, String> entry : [Link]()) {
[Link]([Link]() + “ “ + [Link]());
}
for (String key : [Link]()) { [Link](key); }
for (String value : [Link]()) { [Link](value); }
Time Complexity:
● Access: O(1)
● Search: O(n)
● Insert: O(1)
● Remove: O(1)
3
HashSet
A collection that uses a Hash table for storage, only allowing unique elements to be added.
Import required: import [Link];
Creation: HashSet<String> hs = new HashSet<>();
Add element: [Link](“gopha ok”);
Remove element: [Link](“gopha ok”);
Search element: [Link](“gopha ok”);
Size: [Link]();
Accessing: for (String s : hs) {
[Link](s);
}
Time Complexity:
● Access: O(1)
● Search: O(1)
● Insert: O(1)
● Remove: O(1)
4
ArrayList
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).
Import required: import [Link];
Creation: ArrayList<Integer> list = new ArrayList<>();
List<Integer> list = new ArrayList<>();
Add element: [Link](1);
Update element: [Link](0, 100); // Update index 0’s value to 100
Remove element: [Link](0); // Remove index 0
[Link](); // Remove all elements
Size: [Link]();
Accessing: for (int i = 0; i < [Link](); i++) { [Link]([Link](i)); }
for (String s : list) { [Link](s); }
Sorting: import [Link];
[Link](list); // Sort ascending
[Link](list, [Link]()); // Sort descending
Time Complexity:
● Access: O(1)
● Search: O(n)
● Insert: O(1) (at the back of the ArrayList)
● Remove: O(n)
5
Heap
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.
Import required: import [Link];
Creation: PriorityQueue<Integer> pq = new
PriorityQueue<>([Link]); // Max heap
* Note: Omit “[Link]” for a min heap by default
PriorityQueue<[Link]<String, Integer>> pq = new PriorityQueue<>(
(a, b) -> [Link]().equals([Link]()) ?
[Link]().compareTo([Link]()) :
[Link]() - [Link]()
); // Max heap that contains pairs - if values for pairs are the same,
// then they will be sorted ascending (a-z) according to key
Add element: [Link](10);
View top element: [Link](); // Returns but does not remove the top element
Remove element: [Link](); // Returns and removes the top element
Size: [Link]();
Time Complexity:
● Access Max / Min: O(1)
● Insert: O(log(n))
● Remove Max / Min: O(log(n))
6
Queue
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.
Import required: import [Link];
Creation: Queue<Integer> q = new LinkedList<>(); // Specify as a LinkedList!
Add element: [Link](10);
View top element: [Link](); // Returns head or null if empty
Remove element: [Link](); // Returns head or null if empty
Size: [Link]();
[Link](); // Returns true if the queue is empty
Time Complexity:
● Access: O(n)
● Search: O(n)
● Insert: O(1)
● Remove: O(1)
7
Stack
A collection of elements, with two principle operations: push, which adds to the collection, and
pop, which removes the most recently added element.
Import required: import [Link];
Creation: Stack<Integer> st = new Stack<>();
Add element: [Link](10);
View top element: [Link](); // Returns but does not remove the top element
Remove element: [Link](); // Returns and removes the top element
Size: [Link]();
[Link](); // Returns true if the stack is empty
Time Complexity:
● Access: O(n)
● Search: O(n)
● Insert: O(1)
● Remove: O(1)
8
Linked List
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.
Import required: import [Link];
Creation: LinkedList<Integer> list = new LinkedList<>();
Add element: [Link](1);
Update element: [Link](0, 100); // Update index 0’s value to 100
Remove element: [Link](0); // Remove index 0
[Link](); // Remove all elements
Size: [Link]();
Accessing: for (int i = 0; i < [Link](); i++) { [Link]([Link](i)); }
for (int i : list) { [Link](s); }
Time Complexity:
● Access: O(n)
● Search: O(n)
● Insert: O(1)
● Remove: O(1)