Java Collections Framework: Complete Guide with Examples

The Java Collections Framework is essential for every Java developer. Here’s a comprehensive guide. Collection Hierarchy Collection ├── List │ ├── ArrayList │ ├── LinkedList │ └── Vector ├── Set │ ├── HashSet │ ├── LinkedHashSet │ └── TreeSet └── Queue ├── PriorityQueue └── Deque └── ArrayDeque Lists ArrayList List<String> list = new ArrayList<>(); list.add("Java"); list.add("Python"); list.get(0); // "Java" list.remove(0); LinkedList List<String> list = new LinkedList<>(); list.add("First"); list.add("Last"); // Better for frequent insertions/deletions Sets HashSet Set<String> set = new HashSet<>(); set.add("Java"); set.add("Java"); // Duplicate ignored set.contains("Java"); // true TreeSet Set<String> set = new TreeSet<>(); set.add("Zebra"); set.add("Apple"); // Automatically sorted: [Apple, Zebra] Maps Map<String, Integer> map = new HashMap<>(); map.put("Java", 1); map.put("Python", 2); map.get("Java"); // 1 map.containsKey("Java"); // true Best Practices Use ArrayList for random access Use LinkedList for frequent insertions Use HashSet for unique elements Use TreeSet for sorted unique elements Use HashMap for key-value pairs Conclusion Master Java Collections to write efficient code! 📚

December 15, 2023 · DevCraft Studio · 4289 views

PHP-FPM Tuning Guide

Process manager modes pm=dynamic for most apps; pm=static only when workload is predictable and memory bounded. Key knobs: pm.max_children, pm.start_servers, pm.min_spare_servers, pm.max_spare_servers. Size max_children = (available RAM - OS/webserver/DB) / avg worker RSS. Opcache Enable: opcache.enable=1, opcache.enable_cli=0, opcache.memory_consumption sized for codebase, opcache.interned_strings_buffer, opcache.max_accelerated_files. Avoid opcache.revalidate_freq=0 in prod unless you control deploy restarts; prefer deploy-triggered reloads. Timeouts & queues Keep request_terminate_timeout sane (e.g., 30s-60s); long requests move to queues. Use pm.max_requests to recycle leaky workers (e.g., 500-2000). Watch slowlog to catch blocking I/O or heavy CPU. Observability Export status_path and scrape: active/idle/slow requests, max children reached. Correlate with Nginx/Apache logs for upstream latency and 502/504s. Alert on max children reached, slowlog entries, and rising worker RSS. Checklist Pool sizing validated under load test. Opcache enabled and sized; reload on deploy. Timeouts/queues tuned; slowlog on. Status endpoint protected and scraped.

December 3, 2023 · DevCraft Studio · 4127 views

Spring WebFlux: When to Use It and How to Build With It

Spring WebFlux enables reactive programming in Spring. Here’s when and how to use it. When to Use WebFlux Use WebFlux When: High concurrency requirements Non-blocking I/O needed Streaming data Microservices with reactive backends Use MVC When: Traditional request-response Blocking operations Existing Spring MVC codebase Simpler mental model needed Basic Setup Dependencies <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> Reactive Controller @RestController public class UserController { @GetMapping("/users") public Flux<User> getUsers() { return userService.findAll(); } @GetMapping("/users/{id}") public Mono<User> getUser(@PathVariable Long id) { return userService.findById(id); } @PostMapping("/users") public Mono<User> createUser(@RequestBody Mono<User> user) { return userService.save(user); } } Reactive Types Mono // Single value or empty Mono<String> mono = Mono.just("Hello"); Mono<String> empty = Mono.empty(); Mono<String> error = Mono.error(new RuntimeException()); Flux // Multiple values Flux<String> flux = Flux.just("A", "B", "C"); Flux<Integer> range = Flux.range(1, 10); Reactive Repository public interface UserRepository extends ReactiveCrudRepository<User, Long> { Flux<User> findByStatus(String status); Mono<User> findByEmail(String email); } Error Handling @GetMapping("/users/{id}") public Mono<User> getUser(@PathVariable Long id) { return userService.findById(id) .switchIfEmpty(Mono.error(new UserNotFoundException())) .onErrorResume(error -> { // Handle error return Mono.empty(); }); } Testing @SpringBootTest @AutoConfigureWebTestClient class UserControllerTest { @Autowired private WebTestClient webTestClient; @Test void shouldGetUsers() { webTestClient.get() .uri("/users") .exchange() .expectStatus().isOk() .expectBodyList(User.class); } } Best Practices Use reactive types throughout Avoid blocking operations Handle errors properly Use backpressure Monitor performance Conclusion Spring WebFlux provides: ...

October 15, 2023 · DevCraft Studio · 4206 views

Java Evolution: From Java 8 to Java 25 - Complete Guide

Java has evolved significantly from Java 8 to Java 25. Here’s a comprehensive guide to the major changes. Java 8 (2014) Lambda Expressions List<String> names = Arrays.asList("Java", "Python"); names.forEach(name -> System.out.println(name)); Streams API List<String> filtered = names.stream() .filter(name -> name.startsWith("J")) .collect(Collectors.toList()); Optional Optional<String> name = Optional.of("Java"); name.ifPresent(System.out::println); Java 9 (2017) Modules module com.example { requires java.base; exports com.example.api; } Private Methods in Interfaces interface MyInterface { default void method() { helper(); } private void helper() { // Private method } } Java 11 (2018) String Methods String text = " Java "; text.strip(); // "Java" text.repeat(3); // " Java Java Java " HTTP Client HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://api.example.com")) .build(); Java 17 (2021) Sealed Classes public sealed class Shape permits Circle, Rectangle { } public final class Circle extends Shape { } Pattern Matching if (obj instanceof String s) { System.out.println(s.length()); } Java 21 (2023) Virtual Threads Thread.ofVirtual().start(() -> { // Lightweight thread }); Pattern Matching for Switch String result = switch (value) { case Integer i -> "Integer: " + i; case String s -> "String: " + s; default -> "Unknown"; }; Java 25 (2025) Latest Features Enhanced pattern matching Improved performance Better memory management New API additions Migration Guide Update dependencies Test thoroughly Use new features gradually Monitor performance Update tooling Conclusion Java continues to evolve with: ...

September 10, 2023 · DevCraft Studio · 3589 views

Spring Boot Validation: Complete Guide with @Valid and @Validated

Validation is crucial in Spring Boot applications. Here’s a complete guide to using @Valid and @Validated. Basic Validation Dependencies <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> Bean Validation public class CreateUserRequest { @NotBlank(message = "Name is required") private String name; @Email(message = "Invalid email format") @NotBlank private String email; @Min(value = 18, message = "Age must be at least 18") @Max(value = 120, message = "Age must be at most 120") private Integer age; } @Valid vs @Validated @Valid @PostMapping("/users") public ResponseEntity<User> createUser(@Valid @RequestBody CreateUserRequest request) { // Validates request object } @Validated @RestController @Validated public class UserController { @GetMapping("/users/{id}") public User getUser(@PathVariable @Min(1) Long id) { // Validates path variable } } Custom Validators @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Constraint(validatedBy = PhoneNumberValidator.class) public @interface PhoneNumber { String message() default "Invalid phone number"; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; } public class PhoneNumberValidator implements ConstraintValidator<PhoneNumber, String> { @Override public boolean isValid(String value, ConstraintValidatorContext context) { return value != null && value.matches("^\\+?[1-9]\\d{1,14}$"); } } Validation Groups public interface CreateGroup {} public interface UpdateGroup {} public class UserRequest { @NotNull(groups = UpdateGroup.class) private Long id; @NotBlank(groups = {CreateGroup.class, UpdateGroup.class}) private String name; } @PostMapping("/users") public ResponseEntity<User> create(@Validated(CreateGroup.class) @RequestBody UserRequest request) { // Only validates CreateGroup fields } Error Handling @RestControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity<ErrorResponse> handleValidation( MethodArgumentNotValidException ex) { Map<String, String> errors = new HashMap<>(); ex.getBindingResult().getFieldErrors().forEach(error -> errors.put(error.getField(), error.getDefaultMessage()) ); return ResponseEntity.badRequest() .body(new ErrorResponse("Validation failed", errors)); } } Best Practices Validate at controller level Use appropriate annotations Create custom validators Handle validation errors Use validation groups Conclusion Spring Boot validation provides: ...

June 10, 2023 · DevCraft Studio · 4051 views

Docker Best Practices: Building Efficient Images

Building efficient Docker images requires following best practices. Here’s how. 1. Use Multi-Stage Builds # Build stage FROM node:18 AS builder WORKDIR /app COPY package*.json ./ RUN npm ci COPY . . RUN npm run build # Production stage FROM node:18-alpine WORKDIR /app COPY --from=builder /app/dist ./dist COPY --from=builder /app/node_modules ./node_modules CMD ["node", "dist/index.js"] 2. Layer Caching # Bad: Changes invalidate cache COPY . . RUN npm install # Good: Dependencies cached COPY package*.json ./ RUN npm install COPY . . 3. Use .dockerignore node_modules .git .env dist *.log 4. Minimize Layers # Bad: Multiple layers RUN apt-get update RUN apt-get install -y python RUN apt-get install -y git # Good: Single layer RUN apt-get update && \ apt-get install -y python git && \ rm -rf /var/lib/apt/lists/* 5. Use Specific Tags # Bad: Latest tag FROM node:latest # Good: Specific version FROM node:18.17.0-alpine Best Practices Multi-stage builds Optimize layer order Use .dockerignore Minimize image size Security scanning Conclusion Build efficient Docker images! 🐳

January 15, 2023 · DevCraft Studio · 3173 views

Redis Caching Strategies: Patterns and Best Practices

Redis is powerful for caching. Here are effective caching strategies. Cache-Aside Pattern async function getUser(id) { // Check cache let user = await redis.get(`user:${id}`); if (user) { return JSON.parse(user); } // Cache miss - fetch from DB user = await db.query('SELECT * FROM users WHERE id = ?', [id]); // Store in cache await redis.setex(`user:${id}`, 3600, JSON.stringify(user)); return user; } Write-Through Pattern async function updateUser(id, data) { // Update database const user = await db.update('users', id, data); // Update cache await redis.setex(`user:${id}`, 3600, JSON.stringify(user)); return user; } Cache Invalidation async function deleteUser(id) { // Delete from database await db.delete('users', id); // Invalidate cache await redis.del(`user:${id}`); } Best Practices Set appropriate TTL Handle cache misses Invalidate properly Monitor cache hit rate Use consistent key patterns Conclusion Implement effective Redis caching strategies! 🔴

December 20, 2022 · DevCraft Studio · 4170 views

MongoDB Query Optimization: Indexing and Performance Tips

Optimizing MongoDB queries is essential for performance. Here’s how. Indexing Create Indexes // Single field index db.users.createIndex({ email: 1 }); // Compound index db.users.createIndex({ status: 1, createdAt: -1 }); // Text index db.posts.createIndex({ title: "text", content: "text" }); Explain Queries db.users.find({ email: "[email protected]" }).explain("executionStats"); Query Optimization Use Projection // Bad: Fetches all fields db.users.find({ status: "active" }); // Good: Only needed fields db.users.find( { status: "active" }, { name: 1, email: 1, _id: 0 } ); Limit Results db.users.find({ status: "active" }) .limit(10) .sort({ createdAt: -1 }); Best Practices Create appropriate indexes Use projection Limit result sets Avoid $regex without index Monitor slow queries Conclusion Optimize MongoDB for better performance! 🍃

November 15, 2022 · DevCraft Studio · 4347 views

Elasticsearch Basics: Getting Started with Search

Elasticsearch is powerful for search. Here’s how to get started. Basic Operations Create Index await client.indices.create({ index: 'users', body: { mappings: { properties: { name: { type: 'text' }, email: { type: 'keyword' }, age: { type: 'integer' } } } } }); Index Document await client.index({ index: 'users', id: '1', body: { name: 'John Doe', email: '[email protected]', age: 30 } }); Search const result = await client.search({ index: 'users', body: { query: { match: { name: 'John' } } } }); Best Practices Design mappings carefully Use appropriate analyzers Optimize queries Monitor cluster health Backup regularly Conclusion Master Elasticsearch for powerful search! 🔍

October 10, 2022 · DevCraft Studio · 4106 views

RabbitMQ Message Queues: Patterns and Implementation

RabbitMQ enables reliable message queuing. Here’s how to use it effectively. Basic Setup Producer const amqp = require('amqplib'); async function sendMessage() { const connection = await amqp.connect('amqp://localhost'); const channel = await connection.createChannel(); const queue = 'tasks'; const message = 'Hello RabbitMQ!'; await channel.assertQueue(queue, { durable: true }); channel.sendToQueue(queue, Buffer.from(message), { persistent: true }); console.log("Sent:", message); await channel.close(); await connection.close(); } Consumer async function receiveMessage() { const connection = await amqp.connect('amqp://localhost'); const channel = await connection.createChannel(); const queue = 'tasks'; await channel.assertQueue(queue, { durable: true }); channel.consume(queue, (msg) => { if (msg) { console.log("Received:", msg.content.toString()); channel.ack(msg); } }); } Patterns Work Queues // Distribute tasks among workers channel.prefetch(1); // Fair dispatch Pub/Sub // Exchange for broadcasting await channel.assertExchange('logs', 'fanout', { durable: false }); channel.publish('logs', '', Buffer.from(message)); Best Practices Use durable queues Acknowledge messages Handle errors Monitor queues Use exchanges for routing Conclusion Implement reliable message queuing with RabbitMQ! 🐰

September 15, 2022 · DevCraft Studio · 4028 views