Java Syntax - Common Data Structures Flashcards

1
Q

Array definition

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Initialize a one dimensional array that can hold 10 integers

A

int[] myArr = new int[10];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Initialize a two dimensional array of integers with 10 rows and 20 columns

A

int[][] myArr = new int[10][20];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Initialize an array containing the integers 1, 2, and 3

A

int[] myArr = new int[]{1, 2, 3};

or

int[] myArr = {1, 2, 3};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Initialize a two dimensional array with 3 rows and 3 columns:

1, 2, 3
4, 5, 6
7, 8, 9

A

int[][]myArr={{1,2,3},{4,5,6},{7,8,9}}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Iterate through an array

A

for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
}

for (int i : array) {
System.out.print(array[i]);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

String creation

A

String s = new String(“hello”);

String s = “hello”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

String size

A

s.length()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Accessing and iterating through characters in a String, s.

A

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));
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

HashMap definition

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

HashMap

  1. Create a new HashMap mapping String to String
  2. Add an element
  3. Update element
  4. Remove element
  5. Size
  6. Iterate through entry set
  7. Iterate through key set
  8. Iterate through values
A
  1. Create a new HashMap:

HashMap map = new HashMap<>();

  1. Add an element:
    map. put(“key”, “value”);
  2. Update element:
    map. put(“key”, map.getOrDefault(“key”, “updatedValue”);
  3. Remove element:
    map. remove(“key”);
  4. Size
    map. size();
  5. Iterate through entry set

for (Map.Entry entry : map.entrySet()) { System.out.println(entry.getKey() + “ “ +
entry.getValue());
}

  1. Iterate through key set

for (String key : map.keySet()) {
System.out.println(key);
}

  1. Iterate through values

for (String value : map.values()) {
System.out.println(value);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

HashMap Time Complexity

  1. Access
  2. Search
  3. Insert
  4. Remove
A
  1. Access: O(1)
  2. Search: O(n)
  3. Insert: O(1)
  4. Remove: O(1)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

HashSet definition

A

A HashSet is a collection that uses a Hash table for storage, only allowing unique elements to be added.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

HashSet

  1. Create a new HashSet of String
  2. Add an element
  3. Remove element
  4. Search element
  5. Size
  6. Iterate through set
A
  1. Create a new HashSet of String:

HashSet set = new HashSet<>();

  1. Add an element:
    set. add(“hello”);
  2. Remove element:
    set. remove(“hello”);
  3. Search element:
    set. contains(“hello”);
  4. Size:
    set. size();
  5. Iterate through set:

for(String s : set) {
System.out.println(s);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

HashSet Time Complexity

  1. Access
  2. Search
  3. Insert
  4. Remove
A
  1. Access: O(1)
  2. Search: O(1)
  3. Insert: O(1)
  4. Remove: O(1)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

ArrayList definition

A

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).

17
Q

ArrayList

  1. Create ArrayList of Integer
  2. Add element
  3. Update element
  4. Remove element
  5. Size
  6. Accessing
  7. Sorting
A
  1. Create ArrayList of Integer

ArrayList list = new ArrayList<>();
List list = new ArrayList<>();

  1. Add element
    list. add(1);
  2. Update element
    list. set(0, 100); // Update index 0’s value to 100
  3. Remove element

list. remove(0); // Remove index 0
list. clear(); // Remove all elements

  1. Size
    list. size();
  2. Accessing

for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}

for (String s : list) {
System.out.println(s);
}

  1. Sorting

Collections.sort(list); // Sort ascending
Collections.sort(list, Collections.reverseOrder()); // Sort descending

18
Q

ArrayList Time Complexity

  1. Access
  2. Search
  3. Insert
  4. Remove
A
  1. Access: O(1)
  2. Search: O(n)
  3. Insert: O(1) (at the back of the ArrayList)
  4. Remove: O(n)
    O(1) (removing from the back of the list)
19
Q

Heap definition

A

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.

20
Q

Heap

  1. Create a min heap of Integer
  2. Create a max heap of Integer
  3. Add element
  4. View top element
  5. Remove top element
  6. Size
A
  1. Create a min heap of Integer

PriorityQueue pq = new PriorityQueue<>(); // Min heap by default

  1. Create a max heap of Integer

PriorityQueue pq = new PriorityQueue<>(Collections.reverseOrder);

  1. Add element
    pq. add(10);
  2. View top element
    pq. peek();
  3. Remove top element
    pq. poll();
  4. Size
    pq. size();
21
Q

Heap Time Complexity

  1. Access min or max
  2. Insert
  3. Remove min or max
A
  1. Access min or max: O(1)
  2. Insert: O(log(n))
  3. Remove min or max: O(log(n))
22
Q

Queue definition

A

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.

23
Q

Queue

  1. Create a new Queue of Integer
  2. Add element
  3. View top element
  4. Remove element
  5. Size
  6. Empty check
A
  1. Create a new Queue of Integer

Queue q = new LinkedList<>(); // Specify as a LinkedList!

  1. Add element
    q. add(10);
  2. View top element
    q. peek(); // Returns head or null if empty
  3. Remove element
    q. poll(); // Returns head or null if empty
  4. Size
    q. size();
  5. Empty check
    q. isEmpty();
24
Q

Queue Time Complexity

  1. Access
  2. Search
  3. Insert
  4. Remove
A
  1. Access: O(n)
  2. Search: O(n)
  3. Insert: O(1)
  4. Remove: O(1)
25
Q

Stack definition

A

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.

26
Q

Stack

  1. Create a Stack of Integers
  2. Add an element
  3. View top element
  4. Remove element
  5. Size
A
  1. Create a Stack of Integers

Stack stack = new Stack<>();

  1. Add an element
    stack. push(10);
  2. View top element
    stack. peek(); // Returns but does not remove the top element
  3. Remove element
    stack. pop(); // Returns an removes the top element
  4. Size
    stack. size();
  5. Empty check
    stack. isEmpty();
27
Q

Stack Time Complexity

  1. Access
  2. Search
  3. Insert
  4. Remove
A
  1. Access: O(n)
  2. Search: O(n)
  3. Insert: O(1)
  4. Remove: O(1)
28
Q

Linked List definition

A

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.

29
Q

Linked List

  1. Create a linked list of Integers
  2. Add element
  3. Update element
  4. Remove element
  5. Size
  6. Accessing
A
  1. Create a linked list of Integers

LinkedList list = new LinkedList<>();

  1. Add element
    list. add(1);
  2. Update element
    list. set(0, 100); // Update index 0’s value to 100
  3. Remove element

list. remove(0); // Remove index 0
list. clear(); // Remove all elements

  1. Size
    list. size();
  2. Accessing

for(int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}

for(int i : list) {
System.out.println(i);
}

30
Q

Linked List Time Complexity

  1. Access
  2. Search
  3. Insert
  4. Remove
A
  1. Access: O(n)
  2. Search: O(n)
  3. Insert: O(1) // At the head or tail
  4. Remove: O(1) // At the head or tail