A hands-on educational project demonstrating the Java Collections Framework through a REST API built with Spring Boot.
This project serves as a practical companion to learning Java Collections, showcasing how to use each collection type in real-world scenarios through interactive API endpoints.
| Technology | Version |
|---|---|
| Java | 17+ |
| Spring Boot | 3.2 |
| SpringDoc OpenAPI | 2.3 |
| Maven | 3.8+ |
| JUnit 5 | - |
src/main/java/com/giovaneitosa/collections/
├── CollectionsApplication.java
├── config/
│ └── OpenApiConfig.java # Swagger/OpenAPI configuration
├── model/
│ ├── Produto.java # Record with Comparable
│ └── Pessoa.java # Record with equals/hashCode
├── service/
│ ├── ListService.java # ArrayList, LinkedList, Iterator
│ ├── SetService.java # HashSet, LinkedHashSet, TreeSet
│ ├── MapService.java # HashMap, TreeMap, LinkedHashMap
│ └── QueueService.java # Queue, Deque, PriorityQueue
├── controller/
│ └── CollectionController.java # REST endpoints with @Tag grouping
├── comparator/
│ ├── ProdutoPorNomeComparator.java
│ └── ProdutoPorPrecoComparator.java
└── exception/
└── GlobalExceptionHandler.java # Global error handling
- Java 17 or higher
- Maven 3.8+
# Clone the repository
git clone https://github.com/giofeitosa-dev/java-collections-demo.git
# Navigate to project directory
cd java-collections-demo
# Run the application
mvn spring-boot:runThe API will be available at: http://localhost:8080
Once the application is running, access:
- Swagger UI: http://localhost:8080/swagger-ui.html
- OpenAPI JSON: http://localhost:8080/api-docs
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/collections/list/arraylist |
Create ArrayList of products |
| GET | /api/collections/list/linkedlist |
Create LinkedList with addFirst/addLast |
| GET | /api/collections/list/ordenar-preco |
Sort by price (Comparator) |
| GET | /api/collections/list/ordenar-nome |
Sort by name (Comparable) |
| GET | /api/collections/list/sublist |
Return sublist |
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/collections/set/hashset |
HashSet without duplicates |
| GET | /api/collections/set/linkedhashset |
LinkedHashSet preserving order |
| GET | /api/collections/set/treeset |
TreeSet sorted naturally |
| GET | /api/collections/set/tags?texto=... |
Create unique tags |
| GET | /api/collections/set/operacoes |
Intersection, union, difference |
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/collections/map/hashmap |
HashMap key-value pairs |
| GET | /api/collections/map/treemap |
TreeMap sorted by key |
| GET | /api/collections/map/linkedhashmap |
LinkedHashMap preserving order |
| GET | /api/collections/map/buscar?chave=... |
Search with getOrDefault |
| GET | /api/collections/map/ordenar-valor |
Sort by value |
| GET | /api/collections/map/contar |
Count word occurrences |
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/collections/queue/fila |
FIFO queue with LinkedList |
| GET | /api/collections/queue/prioridade |
PriorityQueue by price |
| GET | /api/collections/queue/pilha |
LIFO stack with Deque |
| Collection | Use Case | When to Use |
|---|---|---|
| ArrayList | Fast random access | Read-heavy operations |
| LinkedList | Fast insert/delete | Frequent add/remove at ends |
| HashSet | Unique elements | Fast lookup, no ordering |
| TreeSet | Sorted unique elements | Need sorted order |
| LinkedHashSet | Insertion-order preserved | Need uniqueness + order |
| HashMap | Key-value pairs | Fast lookup by key |
| TreeMap | Sorted keys | Need sorted keys |
| LinkedHashMap | Insertion-order preserved | Need order + key-value |
| Queue | FIFO processing | Sequential processing |
| Deque | Stack/Queue flexibility | Both LIFO and FIFO |
- Java Records: Immutable models (
Produto,Pessoa) - Comparable: Natural ordering (
ProdutoimplementsComparable) - Comparator: Custom ordering (
ProdutoPorNomeComparator) - @FunctionalInterface: Lambda-ready comparators
- Iterator: Safe collection traversal
- Collections Utility:
sort,shuffle,reverse,frequency - Stream API: Functional operations
- Validation: Record constructors with validation
curl http://localhost:8080/api/collections/list/arraylistcurl http://localhost:8080/api/collections/map/buscar?chave=notebookcurl "http://localhost:8080/api/collections/set/tags?texto=java collections spring boot"curl http://localhost:8080/api/collections/list/ordenar-preco# Run all tests (27 tests)
mvn test
# Run specific test suites
mvn test -Dtest=ListServiceTest
mvn test -Dtest=SetServiceTest
mvn test -Dtest=MapServiceTest
mvn test -Dtest=QueueServiceTestThis project is designed to help developers understand:
- When to use each collection type based on performance characteristics
- Comparable vs Comparator for sorting strategies
- Java Records for immutable data models
- Spring Boot REST API best practices
- API documentation with SpringDoc OpenAPI
Giovani Feitosa - Educational material on Java Collections Framework
Contributions are welcome! Feel free to open issues or submit pull requests.
This project is educational material for learning purposes.