Java Collections, Spring Framework & Spring Boot - Detailed Guide
1. Collection in Java & Collection Framework
A Collection is an object that groups multiple elements into a single unit.
Java Collection Framework provides ready-to-use classes and interfaces for data handling.
Example:
List<String> list = new ArrayList<>();
list.add("Apple"); list.add("Banana");
2. Hierarchy of Collection Framework
Collection (Interface)
List (ArrayList, LinkedList, Vector, Stack)
Set (HashSet, LinkedHashSet, TreeSet)
Queue (PriorityQueue, LinkedList)
Map (Interface) is a separate hierarchy.
Example:
Map<Integer, String> map = new HashMap<>();
map.put(1, "One");
3. Iterator Interface
Used to iterate over elements of a collection.
Example:
Iterator<String> it = list.iterator();
while(it.hasNext()) System.out.println(it.next());
4. List Interface & Implementations
ArrayList: Dynamic array, fast random access.
LinkedList: Doubly linked, fast insert/delete.
Vector: Thread-safe version of ArrayList.
Stack: LIFO structure.
Example:
List<String> items = new ArrayList<>();
items.add("Pen"); items.add("Book");
5. Queue Interface
Queue is FIFO (First-In-First-Out).
Example:
Queue<String> q = new LinkedList<>();
q.add("A"); q.add("B");
System.out.println(q.poll()); // Outputs "A"
6. Set Interface & Implementations
Java Collections, Spring Framework & Spring Boot - Detailed Guide
HashSet: Unordered, no duplicates.
LinkedHashSet: Maintains insertion order.
TreeSet: Sorted set.
Example:
Set<String> s = new HashSet<>();
s.add("Dog"); s.add("Cat");
7. Map Interface & Classes
Map stores key-value pairs.
HashMap: Unordered, fast access.
LinkedHashMap: Maintains insertion order.
TreeMap: Sorted keys.
Hashtable: Thread-safe.
Example:
Map<String, Integer> scores = new HashMap<>();
scores.put("Math", 95);
8. Comparable and Comparator Interfaces
Comparable: Used for natural ordering.
class Student implements Comparable<Student> {
int marks;
public int compareTo(Student s) {
return this.marks - s.marks;
}
}
Comparator: Custom sorting.
Collections.sort(list, (a, b) -> a.name.compareTo(b.name));
9. Properties Class in Java
Used for configuration as key-value pairs.
Example:
Properties p = new Properties();
p.setProperty("url", "localhost");
10. Spring Core Basics and Dependency Injection
Spring uses IoC (Inversion of Control) to inject dependencies.
@Component
class Car {
void drive() { System.out.println("Driving..."); }
}
Java Collections, Spring Framework & Spring Boot - Detailed Guide
@Component
class Person {
@Autowired Car car;
void travel() { car.drive(); }
}
11. Spring AOP & Bean Scopes
AOP is used to define cross-cutting concerns like logging.
@Aspect
class Logger {
@Before("execution(* service.*.*(..))")
void log() { System.out.println("Log before call"); }
}
Bean Scopes: singleton, prototype, request, session, application.
@Scope("prototype")
12. Spring Autowiring, Annotations, Lifecycle
@Autowired: Injects dependencies automatically.
@PostConstruct/@PreDestroy: Lifecycle methods.
@Component, @Service, @Repository: For bean definition.
13. Spring Bean Configuration Styles
- XML Configuration
- Annotation-based Configuration
- Java-based Configuration
@Configuration
class AppConfig {
@Bean
Car car() { return new Car(); }
}
14. Spring Boot Build Systems & Code Structure
Build systems: Maven (pom.xml), Gradle (build.gradle)
Code Structure:
src/main/java
src/main/resources
15. Spring Boot Runner, Logger, and REST API
@SpringBootApplication
class App implements CommandLineRunner {
Java Collections, Spring Framework & Spring Boot - Detailed Guide
public void run(String... args) {
System.out.println("Started");
}
}
@RestController
class Hello {
@GetMapping("/hi")
public String greet() { return "Hello"; }
}
16. Request Mapping, RequestBody, PathVariable, RequestParam
@GetMapping("/item")
@PostMapping("/add")
@PutMapping("/update")
@DeleteMapping("/delete")
@RequestBody: Parses JSON request body
@PathVariable: Extracts value from URL
@RequestParam: Gets query parameter