# Data Structure

## String

Char Array to String

```
char[] chars = {'a','b','c'};
String s = String.valueOf(chars);
```

## StringBuilder

### Insert at begining

```
sb.insert(0, "string_should_be_at_begining");
```

### Append

```
sb.append("string_to_append");
```

### Set to empty

```
sb.setLength(0);
```

## List

## Set

### Delcare&#x20;

```
Set<Integer> set = new HashSet<>();
```

### Add

```
set.add(123);
```

### Delete

```
set.remove(123);
```

### Check contains

```
set.contains(123); // ture or false
```

##

## Map

### Declaration&#x20;

```
Map<Integer, Integer> map = new HashMap<>();
```

### Add

```
map.put(key, value);
```

### Check contains

```
map.containsKey(key);
map.containsValue(value);
```

### Update

```
map.put(key, map.getOrDefault(key, default_value) + 1);
```

### Iteration for values

```
for (int v : map.values()) {
...
}
```

### Interation for keys

```
for (int k : map.keySet()) {
    ....
}

```

### Interation for element

```
map.forEach((k,v) -> {
  // Can't return anything here
});
```

```
for(Map.Entry<Integer, Interger> e : map.entrySet()) {
    int key = entry.getKey();
    int value = entry.getValue();
    // Can return here
}
```

## Stack

### inintial

```
Stack<Integer> s = new Stack();
```

##

## Array of object

```
Stack<Integer>[] prev = new Stack[26];

// Make sure to initialize each stack in the array
for (int i = 0; i < 26; i++) {
    prev[i] = new Stack<>();
}

```
