This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.CopyOnWriteArrayList; | |
public class CopyOnWriteArrayListExample { | |
public static void main(String[] args) { | |
CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>(); | |
list.add("A"); | |
list.add("B"); | |
// Thread for adding elements | |
Thread writerThread = new Thread(() -> { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.ConcurrentHashMap; | |
public class ConcurrentHashMapExample { | |
public static void main(String[] args) { | |
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>(); | |
map.put("A", 1); | |
map.put("B", 2); | |
// Concurrent read and write operations | |
map.computeIfAbsent("C", k -> 3); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ✅ Good | |
class Service { | |
private final Repository repo; | |
public Service(Repository repo) { | |
this.repo = repo; | |
} | |
} | |
// ❌ Bad |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ✅ Good | |
interface Engine { | |
void start(); | |
} | |
class Car { | |
private final Engine engine;//Injection | |
public Car(Engine engine) {//Constructor Injection | |
this.engine = engine; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ✅ Good - Builder Pattern | |
public class Car { | |
private final String engine; | |
private final int wheels; | |
private Car(Builder builder) { | |
this.engine = builder.engine; | |
this.wheels = builder.wheels; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apiVersion: v1 | |
kind: Service # Defines the type of Kubernetes object, which is a Service. | |
metadata: | |
name: nginx | |
spec: # Defines the desired state of the service | |
type: LoadBalancer | |
selector: | |
app: nginx | |
ports: | |
- protocol: TCP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int[][] dir = new int[][] {{-1,0},{1,0},{0,-1},{0,1}}; | |
if(row < 0 || col < 0 || row == grid.length || col == grid[0].length || grid[row][col] == '0')// 0 is bad case | |
return; | |
for(int i = 0; i<dir.length; i++){ | |
.... | |
//use row+dir[i][0], col+dir[i][1] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int[][] neighbors = {{r, c + 1}, {r, c - 1}, {r + 1, c}, {r - 1, c}};//All 4 directions | |
for (int direction = 0; direction < 4; direction++) {//for 4 directions | |
int newR = neighbors[direction][0];//direction = 0,1,2,3 | |
int newC = neighbors[direction][1]; | |
if (Math.min(newR, newC) < 0 || newR == ROWS || newC == COLS || visit[newR][newC] == 1 || grid[newR][newC] == 1) { | |
continue; | |
} | |
queue.add(neighbors[j]); | |
visit[newR][newC] = 1; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void dfs(char[][] grid, int row, int col){ | |
int[][] dir = new int[][] {{-1,0},{1,0},{0,-1},{0,1}}; | |
if(row < 0 || col < 0 || row == grid.length || col == grid[0].length || grid[row][col] == '0')// 0 is bad case | |
return; | |
grid[row][col] = '0';//INSTEAD OF VISIT ARRAY, Just modify the original grid | |
for(int i = 0; i<dir.length; i++){ | |
dfs(grid, row+dir[i][0], col+dir[i][1]); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<List<Integer>> graph = new ArrayList<>(n); | |
for(int i = 0; i<n; i++){ | |
graph.add(new ArrayList<>()); | |
} | |
for(int[] edge: edges){ | |
graph.get(edge[0]).add(edge[1]); | |
graph.get(edge[1]).add(edge[0]); | |
} |
NewerOlder