Java Collections, Spring Framework & Spring Boot - Cheat Sheet
Java Collections Framework
- Collection Framework: Group of interfaces & classes to handle data as objects.
- Hierarchy:
Collection -> List / Set / Queue
Map is separate (for key-value pairs).
- List: Ordered, allows duplicates. (ArrayList, LinkedList, Vector, Stack)
- Set: No duplicates. (HashSet, LinkedHashSet, TreeSet)
- Queue: FIFO (LinkedList, PriorityQueue)
- Map: Key-value pairs. (HashMap, TreeMap, LinkedHashMap, Hashtable)
- Iterator: For traversing collections.
- Comparable: For natural sorting using compareTo().
- Comparator: For custom sorting using compare().
- Properties: For config values in key-value format.
Spring Framework (Core)
- DI (Dependency Injection): Spring manages object dependencies.
- IoC (Inversion of Control): Spring controls object creation.
- AOP: Used for cross-cutting concerns like logging, security.
- Bean Scopes:
Singleton (default), Prototype, Request, Session, Application.
- Autowiring: Automatically inject dependencies using @Autowired.
- Annotations: @Component, @Service, @Repository, @Controller.
- Life Cycle: @PostConstruct, @PreDestroy for init and destroy callbacks.
- Bean Config Styles: XML, Annotation-based, Java-based (@Configuration).
Spring Boot
- Build Systems: Maven/Gradle for dependency management.
- Code Structure: Organized into controller, service, repository.
- Spring Boot Runners: CommandLineRunner for code on startup.
- Logger: Use LoggerFactory for logging.
- RESTful Services:
@RestController for creating APIs.
@GetMapping, @PostMapping, @PutMapping, @DeleteMapping for methods.
@RequestBody: Accept request JSON.
@PathVariable: Extract values from URL.
@RequestParam: Extract query params.
Sample REST Controller Code
@RestController
public class HelloController {
@GetMapping("/hello")
Java Collections, Spring Framework & Spring Boot - Cheat Sheet
public String sayHello() {
return "Hi";
}
@PostMapping("/add")
public String add(@RequestBody Student s) {
return "Added " + s.getName();
}
}