In warehouse logistics, having data sorted alphabetically is crucial for quick navigation and reporting. This project demonstrates the usage of TreeMap, a specialized Map implementation in Java that maintains its keys in their natural order (alphabetical for Strings). Unlike HashMap, which offers no order guarantees, TreeMap ensures that every iteration over the collection reflects a sorted state. We practice data insertion and custom formatting of the sorted output.
- Automatic Sorting: Utilized
TreeMapto ensure "apple", "banana", and "pear" are ordered correctly without manual sorting. - Efficient Iteration: Applied the
entrySet()method to traverse the collection and access both keys and values. - Predictable Output: Met the specific formatting requirement: "Fruit: [Name], Quantity: [Quantity]".
- Type Safety: Ensured keys are
Stringand values areIntegerusing Generics.
- Java 8+ (Collections Framework, TreeMap)
- WarehouseManagerApp: The main class responsible for stock management and sorted display logic.
Fruit: apple, Quantity: 2
Fruit: banana, Quantity: 4
Fruit: pear, Quantity: 6
Project Structure:
JavaBasics_Task_450/
├── src/
│ └── com/yurii/pavlenko/
│ └── app/
│ └── WarehouseManagerApp.java
├── LICENSE
├── TASK.md
├── THEORY.md
└── README.md
Code
package com.yurii.pavlenko.app;
import java.util.Map;
import java.util.TreeMap;
public class WarehouseManagerApp {
public static void main(String[] args) {
Map<String, Integer> fruitStock = new TreeMap<>();
fruitStock.put("pear", 6);
fruitStock.put("apple", 2);
fruitStock.put("banana", 4);
for (Map.Entry<String, Integer> entry : fruitStock.entrySet()) {
System.out.println("Fruit: " + entry.getKey() + ", Quantity: " + entry.getValue());
}
}
}This project is licensed under the MIT License.
Copyright (c) 2026 Yurii Pavlenko
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files...
License: MIT