|
| 1 | +/** |
| 2 | + * @author Robin Panicker |
| 3 | + * @version 1.0.0 |
| 4 | + * @since 0.1.0 |
| 5 | + */ |
| 6 | +package com.appxiom.ax.tuple.demo; |
| 7 | + |
| 8 | +import com.appxiom.ax.tuple.NamedTuple; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +/** |
| 13 | + * This class demonstrates the usage of NamedTuple as a HashMap key. |
| 14 | + * It shows how named keys provide better self-documentation compared to |
| 15 | + * standard Tuples. |
| 16 | + */ |
| 17 | +public class NamedTupleComparison { |
| 18 | + |
| 19 | + public static void main(String[] args) { |
| 20 | + System.out.println("--- Scenario 1: Nested HashMaps ---"); |
| 21 | + demonstrateNestedMap(); |
| 22 | + |
| 23 | + System.out.println("\n--- Scenario 2: Flat HashMap with NamedTuple Key ---"); |
| 24 | + demonstrateNamedTupleMap(); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Demonstrates the use of nested HashMaps to manage sales data. |
| 29 | + */ |
| 30 | + private static void demonstrateNestedMap() { |
| 31 | + // Data Structure: Region -> Year -> Product -> Sales |
| 32 | + Map<String, Map<Integer, Map<String, Double>>> salesData = new HashMap<>(); |
| 33 | + |
| 34 | + // 1. Populating the data |
| 35 | + addDataNested(salesData, "North", 2023, "Widget A", 1500.0); |
| 36 | + addDataNested(salesData, "North", 2023, "Widget B", 2000.0); |
| 37 | + addDataNested(salesData, "South", 2023, "Widget A", 1200.0); |
| 38 | + addDataNested(salesData, "North", 2024, "Widget A", 1800.0); |
| 39 | + |
| 40 | + // 2. Iterating to get final data (Requires 3 nested for-loops) |
| 41 | + for (Map.Entry<String, Map<Integer, Map<String, Double>>> regionEntry : salesData.entrySet()) { |
| 42 | + String region = regionEntry.getKey(); |
| 43 | + for (Map.Entry<Integer, Map<String, Double>> yearEntry : regionEntry.getValue().entrySet()) { |
| 44 | + Integer year = yearEntry.getKey(); |
| 45 | + for (Map.Entry<String, Double> productEntry : yearEntry.getValue().entrySet()) { |
| 46 | + String product = productEntry.getKey(); |
| 47 | + Double sales = productEntry.getValue(); |
| 48 | + System.out.printf("Region: %s, Year: %d, Product: %s -> Sales: $%.2f%n", |
| 49 | + region, year, product, sales); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private static void addDataNested(Map<String, Map<Integer, Map<String, Double>>> salesData, |
| 56 | + String region, Integer year, String product, Double sales) { |
| 57 | + salesData.computeIfAbsent(region, k -> new HashMap<>()) |
| 58 | + .computeIfAbsent(year, k -> new HashMap<>()) |
| 59 | + .put(product, sales); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Demonstrates the use of a flat HashMap with NamedTuple keys to manage sales |
| 64 | + * data. |
| 65 | + */ |
| 66 | + private static void demonstrateNamedTupleMap() { |
| 67 | + // Data Structure: NamedTuple(Region, Year, Product) -> Sales |
| 68 | + Map<NamedTuple, Double> salesData = new HashMap<>(); |
| 69 | + |
| 70 | + // 1. Populating the data |
| 71 | + salesData.put(createNamedKey("North", 2023, "Widget A"), 1500.0); |
| 72 | + salesData.put(createNamedKey("North", 2023, "Widget B"), 2000.0); |
| 73 | + salesData.put(createNamedKey("South", 2023, "Widget A"), 1200.0); |
| 74 | + salesData.put(createNamedKey("North", 2024, "Widget A"), 1800.0); |
| 75 | + |
| 76 | + // 2. Iterating to get final data |
| 77 | + for (Map.Entry<NamedTuple, Double> entry : salesData.entrySet()) { |
| 78 | + NamedTuple key = entry.getKey(); |
| 79 | + String region = key.get("Region"); |
| 80 | + Integer year = key.get("Year"); |
| 81 | + String product = key.get("Product"); |
| 82 | + Double sales = entry.getValue(); |
| 83 | + |
| 84 | + System.out.printf("Region: %s, Year: %d, Product: %s -> Sales: $%.2f%n", |
| 85 | + region, year, product, sales); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Helper to create a NamedTuple key for the demo. |
| 91 | + */ |
| 92 | + private static NamedTuple createNamedKey(String region, Integer year, String product) { |
| 93 | + Map<String, Object> fields = new HashMap<>(); |
| 94 | + fields.put("Region", region); |
| 95 | + fields.put("Year", year); |
| 96 | + fields.put("Product", product); |
| 97 | + return NamedTuple.of(fields); |
| 98 | + } |
| 99 | +} |
0 commit comments