Java syntax 1 Flashcards

1
Q

Create a HashMap

A

Map> map = new HashMap<>();

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

Create an array of ints

A

int[] freqArray = new int[128];

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

Loop through a list of strings (no index)

A

for (String s : strs) {

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

Loop through characters in a String (same type)

A

for (int i = 0; i < s.length(); i ++ { s.charAt(i) //to get val

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

Loop through characters in a String (char type)

A

for (char c : s.toCharArray()) {

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

Check if HashMap contains a key

A

map.containsKey(key);

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

Insert a value into a HashMap at a specified key

A

map.put(key, value);

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

Get a list of all a HashMap’s values

A

new ArrayList<>(map.values());

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

Create an ArrayList of type String

A

ArrayList ex = new ArrayList();

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

Create a List of type Integer

A

List list = new ArrayList<>();

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

Get and Set elements in Array vs ArrayList

A

Array -> arr[0], arr[1], arr[0] = 1, arr[1] = 2

ArrayList -> arrL.add(1); arrL.add(2);

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

Get a char at index i in a String

A

s.charAt(i)

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

If var is null -> 0, else set to something

A

int x = (p != null) ? p.val : 0;

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

Sort a int[][] by the first value in each sub []

A

Arrays.sort(slots1, (a, b) -> a[0] - b[0]);

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