🚀 𝗠𝗮𝘀𝘁𝗲𝗿𝗶𝗻𝗴 𝗗𝗲𝗽𝗲𝗻𝗱𝗲𝗻𝗰𝘆 𝗜𝗻𝗷𝗲𝗰𝘁𝗶𝗼𝗻 — 𝗧𝗵𝗲 𝗦𝗲𝗰𝗿𝗲𝘁 𝘁𝗼 𝗖𝗹𝗲𝗮𝗻 & 𝗦𝗰𝗮𝗹𝗮𝗯𝗹𝗲 𝗝𝗮𝘃𝗮 𝗖𝗼𝗱𝗲 Java learning focus: Dependency Injection (DI) — a powerful pattern that separates object creation from object usage. 𝗜𝗻 𝘁𝗿𝗮𝗱𝗶𝘁𝗶𝗼𝗻𝗮𝗹 𝗰𝗼𝗱𝗲: 𝗰𝗹𝗮𝘀𝘀 𝗖𝗮𝗿 { 𝗽𝗿𝗶𝘃𝗮𝘁𝗲 𝗘𝗻𝗴𝗶𝗻𝗲 𝗲𝗻𝗴𝗶𝗻𝗲 = 𝗻𝗲𝘄 𝗘𝗻𝗴𝗶𝗻𝗲(); } 𝘛𝘩𝘦 𝘊𝘢𝘳 𝘪𝘴 𝘵𝘪𝘨𝘩𝘵𝘭𝘺 𝘤𝘰𝘶𝘱𝘭𝘦𝘥 𝘸𝘪𝘵𝘩 𝘌𝘯𝘨𝘪𝘯𝘦. 𝘊𝘩𝘢𝘯𝘨𝘦 𝘵𝘩𝘦 𝘌𝘯𝘨𝘪𝘯𝘦 𝘵𝘺𝘱𝘦, 𝘢𝘯𝘥 𝘺𝘰𝘶 𝘳𝘦𝘸𝘳𝘪𝘵𝘦 𝘵𝘩𝘦 𝘊𝘢𝘳. 😩 𝗪𝗶𝘁𝗵 𝗗𝗜: 𝗰𝗹𝗮𝘀𝘀 𝗖𝗮𝗿 { 𝗽𝗿𝗶𝘃𝗮𝘁𝗲 𝗘𝗻𝗴𝗶𝗻𝗲 𝗲𝗻𝗴𝗶𝗻𝗲; 𝗽𝘂𝗯𝗹𝗶𝗰 𝗖𝗮𝗿(𝗘𝗻𝗴𝗶𝗻𝗲 𝗲𝗻𝗴𝗶𝗻𝗲) { 𝘁𝗵𝗶𝘀.𝗲𝗻𝗴𝗶𝗻𝗲 = 𝗲𝗻𝗴𝗶𝗻𝗲; } } 𝘕𝘰𝘸, 𝘢𝘯𝘺 𝘦𝘯𝘨𝘪𝘯𝘦 𝘤𝘢𝘯 𝘣𝘦 𝘪𝘯𝘫𝘦𝘤𝘵𝘦𝘥 — 𝘗𝘦𝘵𝘳𝘰𝘭, 𝘌𝘭𝘦𝘤𝘵𝘳𝘪𝘤, 𝘰𝘳 𝘏𝘺𝘣𝘳𝘪𝘥 🚗⚡ 💡 𝗪𝗵𝘆 𝗗𝗜 𝗺𝗮𝘁𝘁𝗲𝗿𝘀: 𝘗𝘳𝘰𝘮𝘰𝘵𝘦𝘴 𝘭𝘰𝘰𝘴𝘦 𝘤𝘰𝘶𝘱𝘭𝘪𝘯𝘨 𝘉𝘰𝘰𝘴𝘵𝘴 𝘵𝘦𝘴𝘵𝘢𝘣𝘪𝘭𝘪𝘵𝘺 𝘌𝘯𝘢𝘣𝘭𝘦𝘴 𝘐𝘯𝘷𝘦𝘳𝘴𝘪𝘰𝘯 𝘰𝘧 𝘊𝘰𝘯𝘵𝘳𝘰𝘭 𝘊𝘰𝘳𝘦 𝘰𝘧 𝘚𝘱𝘳𝘪𝘯𝘨 𝘍𝘳𝘢𝘮𝘦𝘸𝘰𝘳𝘬 𝘢𝘳𝘤𝘩𝘪𝘵𝘦𝘤𝘵𝘶𝘳𝘦 Frameworks like Spring take DI further using @Autowired, making object wiring automatic and clean. Think of DI like a phone getting its battery inserted — the phone doesn’t build it, it just uses it efficiently. 🔋 💼 𝗜𝗺𝗽𝗼𝗿𝘁𝗮𝗻𝘁 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗤𝘂𝗲𝘀𝘁𝗶𝗼𝗻𝘀 & 𝗔𝗻𝘀𝘄𝗲𝗿𝘀 𝗤𝟭: 𝗪𝗵𝗮𝘁 𝗶𝘀 𝗗𝗲𝗽𝗲𝗻𝗱𝗲𝗻𝗰𝘆 𝗜𝗻𝗷𝗲𝗰𝘁𝗶𝗼𝗻? ➡ It’s a design pattern where an object receives its dependencies from an external source rather than creating them internally. 𝗤𝟮: 𝗛𝗼𝘄 𝗶𝘀 𝗗𝗜 𝗿𝗲𝗹𝗮𝘁𝗲𝗱 𝘁𝗼 𝗜𝗻𝘃𝗲𝗿𝘀𝗶𝗼𝗻 𝗼𝗳 𝗖𝗼𝗻𝘁𝗿𝗼𝗹 (𝗜𝗼𝗖)? ➡ DI is a form of IoC — instead of the object controlling its dependencies, the framework or external code controls it. 𝗤𝟯: 𝗪𝗵𝗮𝘁 𝗮𝗿𝗲 𝘁𝗵𝗲 𝗯𝗲𝗻𝗲𝗳𝗶𝘁𝘀 𝗼𝗳 𝗗𝗜? ➡ Loose coupling, easier unit testing, flexibility, and cleaner code. 𝗤𝟰: 𝗪𝗵𝗮𝘁 𝗮𝗿𝗲 𝘁𝗵𝗲 𝗱𝗶𝗳𝗳𝗲𝗿𝗲𝗻𝘁 𝘁𝘆𝗽𝗲𝘀 𝗼𝗳 𝗗𝗜 𝗶𝗻 𝗦𝗽𝗿𝗶𝗻𝗴? ➡ Constructor Injection, Setter Injection, and Field Injection. 𝗤𝟱: 𝗪𝗵𝗶𝗰𝗵 𝗶𝗻𝗷𝗲𝗰𝘁𝗶𝗼𝗻 𝘁𝘆𝗽𝗲 𝗶𝘀 𝗽𝗿𝗲𝗳𝗲𝗿𝗿𝗲𝗱 𝗶𝗻 𝗦𝗽𝗿𝗶𝗻𝗴? ➡ Constructor Injection — ensures dependencies are immutable and makes testing easier. 𝗤𝟲: 𝗖𝗮𝗻 𝘆𝗼𝘂 𝘂𝘀𝗲 𝗗𝗜 𝘄𝗶𝘁𝗵𝗼𝘂𝘁 𝗳𝗿𝗮𝗺𝗲𝘄𝗼𝗿𝗸𝘀 𝗹𝗶𝗸𝗲 𝗦𝗽𝗿𝗶𝗻𝗴? ➡ Yes, DI is a pattern — you can manually implement it even in plain Java (as shown above). #Java #SpringBoot #DependencyInjection #OOP #SoftwareDesign #IoC #CleanCode #ProgrammingTips #DesignPatterns #JavaDeveloper
Understanding Dependency Injection in Java with Spring
More Relevant Posts
-
🚀 Fixed a Logical Bug in “Contains Duplicate II” (Sliding Window Problem in Java) Today I revisited one of the seemingly simple problems — “Contains Duplicate II” — and realized how small syntax choices can completely change logic flow 😅 At first, I wrote the code using a mix of C++ and Java syntax, which caused compilation and logical issues. But while debugging, I learned some key lessons that made my solution cleaner, faster, and interview-ready 💪 🧩 The Problem Given an integer array nums and an integer k, return true if there are two distinct indices i and j such that: nums[i] == nums[j] && |i - j| <= k ⚙️ My Fixed Java Code import java.util.HashSet; class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { HashSet<Integer> set = new HashSet<>(); int i = 0; int j = 0; int n = nums.length; while (j < n) { if (Math.abs(j - i) > k) { set.remove(nums[i]); i++; } if (set.contains(nums[j])) { return true; } set.add(nums[j]); j++; } return false; } } ❌ Mistakes I Made Initially 1️⃣ Used set.get() and set.end — which belong to C++, not Java. ✅ Fixed by using set.contains(nums[j]). 2️⃣ Didn’t maintain a proper sliding window size. ✅ Simplified with if (Math.abs(j - i) > k) to ensure the window never exceeds k elements. 3️⃣ Didn’t clearly understand the window logic — that we remove older elements as we move forward. ✅ Learned to visualize the window as a moving frame over the array. 4️⃣ Overcomplicated the logic. ✅ Cleaned it up using a simple while loop and HashSet. 🧠 What I Learned Clean, minimal code > fancy syntax. Always check for language-specific methods — C++ vs Java can trick you! The sliding window technique isn’t about moving two pointers randomly — it’s about maintaining constraints efficiently. Debugging teaches more than success on the first try. 🎯 Targeting Product-Based & EdTech Companies As part of my SDE preparation for product-based and EdTech companies like FloBiz, Scaler, Razorpay, Unacademy, and Swiggy, I’m focusing on building strong fundamentals — not just solving problems, but deeply understanding why they work. 💬 Final Thought Every “wrong submission” is just a lesson wrapped in logic. Keep coding, keep debugging, and every small improvement compounds over time 💻🔥 Masai Prepleaf by Masai Newton School PhonePe Paytm Paytm Payments Bank Razorpay Razor Group PayU #Java #DSA #ProblemSolving #LeetCode #LearningInPublic #SoftwareDevelopment #ProductBasedPreparation #CodeWithDilsah #EdTech #FrontendDeveloper #FloBiz
To view or add a comment, sign in
-
-
💡 𝗝𝗮𝘃𝗮 𝟴: 𝗣𝗼𝘄𝗲𝗿 𝗼𝗳 𝗟𝗮𝗺𝗯𝗱𝗮 𝗘𝘅𝗽𝗿𝗲𝘀𝘀𝗶𝗼𝗻𝘀 One of the biggest milestones in J𝗮𝘃𝗮 𝟴 was the introduction of Lambda Expressions — a way to write clean, expressive, and functional-style code. Before Java 8, we relied heavily on anonymous classes to pass behavior. With Lambdas, we can define the same logic in a single line. 𝗘𝘅𝗮𝗺𝗽𝗹𝗲: @𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝗮𝗹𝗜𝗻𝘁𝗲𝗿𝗳𝗮𝗰𝗲 𝗶𝗻𝘁𝗲𝗿𝗳𝗮𝗰𝗲 𝗖𝗮𝗹𝗰𝘂𝗹𝗮𝘁𝗼𝗿 { 𝗶𝗻𝘁 𝗼𝗽𝗲𝗿𝗮𝘁𝗲(𝗶𝗻𝘁 𝗮, 𝗶𝗻𝘁 𝗯); } 𝗖𝗮𝗹𝗰𝘂𝗹𝗮𝘁𝗼𝗿 𝗮𝗱𝗱 = (𝗮, 𝗯) -> 𝗮 + 𝗯; 𝗦𝘆𝘀𝘁𝗲𝗺.𝗼𝘂𝘁.𝗽𝗿𝗶𝗻𝘁𝗹𝗻(𝗮𝗱𝗱.𝗼𝗽𝗲𝗿𝗮𝘁𝗲(𝟱, 𝟯)); // 𝟴 𝗟𝗮𝗺𝗯𝗱𝗮𝘀 𝘄𝗼𝗿𝗸 𝘄𝗶𝘁𝗵 𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝗮𝗹 𝗜𝗻𝘁𝗲𝗿𝗳𝗮𝗰𝗲𝘀 (𝗼𝗻𝗲 𝗮𝗯𝘀𝘁𝗿𝗮𝗰𝘁 𝗺𝗲𝘁𝗵𝗼𝗱), 𝗲𝗻𝗮𝗯𝗹𝗶𝗻𝗴 𝗱𝗲𝘃𝗲𝗹𝗼𝗽𝗲𝗿𝘀 𝘁𝗼: ✅ Reduce boilerplate ✅ Improve code readability ✅ Write elegant Stream API pipelines ✅ Handle asynchronous logic more easily 𝗥𝗲𝗮𝗹 𝘂𝘀𝗲 𝗰𝗮𝘀𝗲𝘀: Event handling (ActionListener e -> ...) Thread execution (new Thread(() -> ...)) Data filtering with Streams Lambdas represent a shift from object-oriented to functional thinking in Java — bringing flexibility, clarity, and speed to your everyday code. ⚙️ 💼 𝗜𝗺𝗽𝗼𝗿𝘁𝗮𝗻𝘁 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗤𝘂𝗲𝘀𝘁𝗶𝗼𝗻𝘀 & 𝗔𝗻𝘀𝘄𝗲𝗿𝘀 𝗤𝟭. 𝗪𝗵𝗮𝘁 𝗶𝘀 𝗮 𝗟𝗮𝗺𝗯𝗱𝗮 𝗘𝘅𝗽𝗿𝗲𝘀𝘀𝗶𝗼𝗻 𝗶𝗻 𝗝𝗮𝘃𝗮? 👉 A short block of code that can be passed around and executed, used to represent a functional interface. 𝗤𝟮. 𝗖𝗮𝗻 𝗟𝗮𝗺𝗯𝗱𝗮 𝗘𝘅𝗽𝗿𝗲𝘀𝘀𝗶𝗼𝗻𝘀 𝗯𝗲 𝘂𝘀𝗲𝗱 𝘄𝗶𝘁𝗵𝗼𝘂𝘁 𝗮 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝗮𝗹 𝗶𝗻𝘁𝗲𝗿𝗳𝗮𝗰𝗲? 👉 No. They can only be used with interfaces that have exactly one abstract method. 𝗤𝟯. 𝗔𝗿𝗲 𝗟𝗮𝗺𝗯𝗱𝗮 𝗘𝘅𝗽𝗿𝗲𝘀𝘀𝗶𝗼𝗻𝘀 𝗼𝗯𝗷𝗲𝗰𝘁𝘀? 👉 No, but at runtime, they are represented as instances of functional interfaces. 𝗤𝟰. 𝗪𝗵𝗮𝘁 𝗮𝗿𝗲 𝘁𝗵𝗲 𝗯𝗲𝗻𝗲𝗳𝗶𝘁𝘀 𝗼𝗳 𝘂𝘀𝗶𝗻𝗴 𝗟𝗮𝗺𝗯𝗱𝗮𝘀? 👉 Less boilerplate code, cleaner syntax, improved readability, and easier use of Streams and functional APIs. 𝗤𝟱. 𝗖𝗮𝗻 𝗮 𝗟𝗮𝗺𝗯𝗱𝗮 𝗮𝗰𝗰𝗲𝘀𝘀 𝘃𝗮𝗿𝗶𝗮𝗯𝗹𝗲𝘀 𝗳𝗿𝗼𝗺 𝘁𝗵𝗲 𝗲𝗻𝗰𝗹𝗼𝘀𝗶𝗻𝗴 𝘀𝗰𝗼𝗽𝗲? 👉 Yes, but those variables must be final or effectively final. 𝗤𝟲. 𝗪𝗵𝗮𝘁 𝗶𝘀 𝘁𝗵𝗲 𝗱𝗶𝗳𝗳𝗲𝗿𝗲𝗻𝗰𝗲 𝗯𝗲𝘁𝘄𝗲𝗲𝗻 𝗟𝗮𝗺𝗯𝗱𝗮 𝗮𝗻𝗱 𝗔𝗻𝗼𝗻𝘆𝗺𝗼𝘂𝘀 𝗖𝗹𝗮𝘀𝘀? 👉 Lambdas are more concise and don’t create a separate class file at compile time; anonymous classes do. #Java #LambdaExpressions #Java8 #FunctionalProgramming #CleanCode #SoftwareDevelopment #BackendEngineering #JavaDeveloper
To view or add a comment, sign in
-
🚀 𝗠𝗮𝘀𝘁𝗲𝗿𝗶𝗻𝗴 𝘁𝗵𝗲 𝗦𝘁𝗿𝗲𝗮𝗺𝘀 𝗔𝗣𝗜 : 𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝗮𝗹 𝗣𝗿𝗼𝗴𝗿𝗮𝗺𝗺𝗶𝗻𝗴 𝗠𝗮𝗱𝗲 𝗦𝗶𝗺𝗽𝗹𝗲 𝗶𝗻 𝗝𝗮𝘃𝗮 𝟴 One of the biggest leaps Java took with version 8 was introducing the Streams API — a game-changer for writing clean, declarative, and functional-style code. 💡𝗕𝗲𝗳𝗼𝗿𝗲 𝘀𝘁𝗿𝗲𝗮𝗺𝘀: 𝗟𝗶𝘀𝘁<𝗦𝘁𝗿𝗶𝗻𝗴> 𝗿𝗲𝘀𝘂𝗹𝘁 = 𝗻𝗲𝘄 𝗔𝗿𝗿𝗮𝘆𝗟𝗶𝘀𝘁<>(); 𝗳𝗼𝗿 (𝗦𝘁𝗿𝗶𝗻𝗴 𝗻𝗮𝗺𝗲 : 𝗻𝗮𝗺𝗲𝘀) { 𝗶𝗳 (𝗻𝗮𝗺𝗲.𝘀𝘁𝗮𝗿𝘁𝘀𝗪𝗶𝘁𝗵("𝗦")) { 𝗿𝗲𝘀𝘂𝗹𝘁.𝗮𝗱𝗱(𝗻𝗮𝗺𝗲.𝘁𝗼𝗨𝗽𝗽𝗲𝗿𝗖𝗮𝘀𝗲()); } } 💡𝗔𝗳𝘁𝗲𝗿 𝘀𝘁𝗿𝗲𝗮𝗺𝘀: 𝗟𝗶𝘀𝘁<𝗦𝘁𝗿𝗶𝗻𝗴> 𝗿𝗲𝘀𝘂𝗹𝘁 = 𝗻𝗮𝗺𝗲𝘀.𝘀𝘁𝗿𝗲𝗮𝗺() .𝗳𝗶𝗹𝘁𝗲𝗿(𝗻 -> 𝗻.𝘀𝘁𝗮𝗿𝘁𝘀𝗪𝗶𝘁𝗵("𝗦")) .𝗺𝗮𝗽(𝗦𝘁𝗿𝗶𝗻𝗴::𝘁𝗼𝗨𝗽𝗽𝗲𝗿𝗖𝗮𝘀𝗲) .𝗰𝗼𝗹𝗹𝗲𝗰𝘁(𝗖𝗼𝗹𝗹𝗲𝗰𝘁𝗼𝗿𝘀.𝘁𝗼𝗟𝗶𝘀𝘁()); 💡 𝗪𝗵𝘆 𝗶𝘁 𝗺𝗮𝘁𝘁𝗲𝗿𝘀: 👉No need for explicit loops 👉Supports filtering, mapping, sorting, reducing 👉Enables parallel processing 👉Encourages functional programming 𝗦𝘁𝗿𝗲𝗮𝗺𝘀 𝗱𝗼𝗻’𝘁 𝘀𝘁𝗼𝗿𝗲 𝗱𝗮𝘁𝗮 — they process it in a pipeline, like water flowing through filters until only the desired output remains. When you start writing streams, your code becomes not only shorter but also expressive and efficient. 👉 If you’re aiming for clean, modern Java code, mastering the Streams API is non-negotiable. 💼 𝗜𝗺𝗽𝗼𝗿𝘁𝗮𝗻𝘁 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗤𝘂𝗲𝘀𝘁𝗶𝗼𝗻𝘀 & 𝗔𝗻𝘀𝘄𝗲𝗿𝘀 𝗤𝟭: 𝗪𝗵𝗮𝘁 𝗶𝘀 𝘁𝗵𝗲 𝗱𝗶𝗳𝗳𝗲𝗿𝗲𝗻𝗰𝗲 𝗯𝗲𝘁𝘄𝗲𝗲𝗻 𝗖𝗼𝗹𝗹𝗲𝗰𝘁𝗶𝗼𝗻 𝗮𝗻𝗱 𝗦𝘁𝗿𝗲𝗮𝗺? ➡ A Collection stores data, while a Stream processes data from a collection. 𝗤𝟮: 𝗔𝗿𝗲 𝘀𝘁𝗿𝗲𝗮𝗺𝘀 𝗿𝗲𝘂𝘀𝗮𝗯𝗹𝗲? ➡ No. Once a stream is consumed (after a terminal operation), it cannot be reused. 𝗤𝟯: 𝗪𝗵𝗮𝘁 𝗶𝘀 𝗹𝗮𝘇𝘆 𝗲𝘃𝗮𝗹𝘂𝗮𝘁𝗶𝗼𝗻 𝗶𝗻 𝘀𝘁𝗿𝗲𝗮𝗺𝘀? ➡ Intermediate operations (like filter or map) are executed only when a terminal operation (like collect) is called. 𝗤𝟰: 𝗪𝗵𝗮𝘁 𝗶𝘀 𝘁𝗵𝗲 𝗱𝗶𝗳𝗳𝗲𝗿𝗲𝗻𝗰𝗲 𝗯𝗲𝘁𝘄𝗲𝗲𝗻 𝗺𝗮𝗽() 𝗮𝗻𝗱 𝗳𝗹𝗮𝘁𝗠𝗮𝗽()? ➡ map() transforms elements one-to-one, while flatMap() flattens nested structures (like lists of lists). 𝗤𝟱: 𝗛𝗼𝘄 𝗱𝗼 𝘆𝗼𝘂 𝗰𝗼𝗻𝘃𝗲𝗿𝘁 𝗮 𝗹𝗶𝘀𝘁 𝗼𝗳 𝘀𝘁𝗿𝗶𝗻𝗴𝘀 𝘁𝗼 𝗮 𝗰𝗼𝗺𝗺𝗮-𝘀𝗲𝗽𝗮𝗿𝗮𝘁𝗲𝗱 𝘀𝘁𝗿𝗶𝗻𝗴 𝘂𝘀𝗶𝗻𝗴 𝘀𝘁𝗿𝗲𝗮𝗺𝘀? String result = list.stream().collect(Collectors.joining(",")); 𝗤𝟲: 𝗪𝗵𝗮𝘁’𝘀 𝘁𝗵𝗲 𝗱𝗶𝗳𝗳𝗲𝗿𝗲𝗻𝗰𝗲 𝗯𝗲𝘁𝘄𝗲𝗲𝗻 𝗦𝘁𝗿𝗲𝗮𝗺.𝗼𝗳() 𝗮𝗻𝗱 𝗔𝗿𝗿𝗮𝘆𝘀.𝘀𝘁𝗿𝗲𝗮𝗺()? ➡ Stream.of() creates a stream from objects, while Arrays.stream() specifically works with arrays. #Java #StreamsAPI #Java8 #FunctionalProgramming #Coding #Developers #CleanCode #OOP
To view or add a comment, sign in
-
I've worked with this library a lot and can say it's really helps with more or less simple object mapping, but if you have rather complicated relations between your classes it takes some time to build correct MapStruct infrastructure. I'd recomment everybody to dive deeper into the documentation and make your life easier. 😎 👍
💡 MapStruct in the project. 👉 MapStruct is a code—generation library for automatic mapping between Java objects. 👉 It works at the compilation stage, generates clean code without reflection, many times faster than ModelMapper and Dozer. 1️⃣ Adding dependencies: <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct</artifactId> </dependency> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> </dependency> OR implementation("org.mapstruct:mapstruct") implementation("org.mapstruct:mapstruct-processor") ⚠️ You need to add two dependencies: the mapstruct library itself and the mapstruct processor. The processor is responsible for code generation during compilation. 2️⃣ Create a Mapper interface: You are creating a regular Java interface with the @Mapper annotation (ComponentModel = "spring"). The ComponentModel parameter specifies that MapStruct should generate a Spring bean that can be injected through the constructor. In the interface, declare converter methods: for example, UserDTO toDto(User user) or List<UserDTO> toDtoList(List<User> users). MapStruct will figure out which fields to map into if the names match. 3️⃣ Set up custom mapping. When the field names are different or additional logic is needed, use the @Mapping annotation: ▪️ source/target — for renaming fields: @Mapping(target = "fullName", source = "firstName") expression — for Java expressions: you can write simple logic directly in the annotation. ▪️ ignore — to ignore the field: @Mapping(target = "password", ignore = true) DateFormat — for formatting dates: automatic conversion of LocalDateTime to String. ▪️ qualifiedByName — for calling custom converter methods. 4️⃣ Working with nested objects: MapStruct can automatically map nested structures. If you have an Address field in the User, and the UserDTO has an AddressDto, create a separate AddressMapper and specify it in the uses = parameter. {AddressMapper.class }. MapStruct will find the necessary mapper itself and apply it to the nested objects. This works recursively for any nesting depth. 5️⃣ Updating existing facilities: It is often necessary not to create a new object, but to update an existing one. To do this, use the @MappingTarget annotation on the second parameter of the method. MapStruct will generate code that will update only the required fields. Add @BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE) — then null values from the DTO will not overwrite existing data in the Entity. ⁉️ What's going on under the hood? MapStruct analyzes your interface at compile time, examining the types of fields, their names, and annotations. Then it generates simple Java code with direct value assignments— no magic, reflection, or proxies. 📒 Documentation: https://lnkd.in/dMdKbmSb #programmingtips #softwaredevelopment #mapper
To view or add a comment, sign in
-
-
⚡️ Day 6 — Diving into Functional Programming in Java Continuing my 100 Days of Java Backend, Spring Boot & AI Integration journey — today was all about revisiting how Java has evolved into a functional powerhouse 💻✨ Even though I’ve worked with these concepts before, revisiting them reminded me how Java blends object-oriented design with functional simplicity. ------------------------------------------------------------------------------- 🧠 What I Revised Today 🧩 Functional Interfaces — single abstract method interfaces like Runnable, Callable, and custom ones using @FunctionalInterface. 💡 Lambda Expressions — writing clean, concise logic using arrow functions — especially useful for passing behavior as parameters. 🔗 Method References — a shorthand way to link existing methods using :: — improving readability and reusability. ⚙️ Common Functional Interfaces: 1) BiFunction → takes 2 inputs, returns 1 output 2) Consumer / BiConsumer → performs actions, returns nothing 3) Predicate → returns boolean results 4) Supplier → provides data without input 🌊 Java Streams API — revisited how to process data with stream pipelines (source → intermediate ops → terminal ops). Used filter(), map(), collect(), and reduce() to perform functional-style data manipulation. 🧱 Error Handling in Streams — learned how to handle exceptions gracefully in lambda pipelines without breaking flow. ------------------------------------------------------------------------------- It’s fascinating how Java’s functional features make code cleaner, more expressive, and perfectly aligned with modern backend & AI-driven workflows ⚙️🤖 Tomorrow, I’ll be exploring Java 25 updates & advanced modern syntax improvements 🚀 #100DaysOfJavaBackend #Day6 #JavaDeveloper #SpringBoot #BackendDevelopment #FunctionalProgramming #StreamsAPI #LambdaExpressions #SoftwareDevelopment #LearningJourney #CodingJourney #AIDeveloper
To view or add a comment, sign in
-
Master the art of shaping JSON in Java: explore how slickly the Jackson annotations in give you fine-tuned control over serialization, field formats, nested objects, and polymorphism. Read more:👇https://lnkd.in/dQRsvDKg #Java #Jackson #JSON #JavaDevelopers #BackendDevelopment #CodingMadeEasy #GeoTech
To view or add a comment, sign in
-
🚀 JVM Architecture in 2025: What Every Java Developer Should Know The Java Virtual Machine (JVM) has quietly evolved into one of the most sophisticated runtime environments in modern software engineering. With Java 25, the JVM is faster, smarter, and more scalable than ever — and understanding its architecture can seriously level up how you write, debug, and tune Java code. 🔹 1. Class Loader Subsystem Loads .class files into memory using a layered delegation model: Bootstrap Loader – Loads core Java classes (java.base) from the module system. Platform Loader – Loads platform modules (like java.logging, java.sql) – modular since Java 9. Application Loader – Loads application-specific classes from the classpath/module path. Custom Loaders – Frameworks like Spring, Quarkus, and containers use these for dynamic class loading. 👉 In Java 25, the module system (jlink, jmod) and sealed types mean more control over what’s visible and loaded. 🧠 2. Runtime Data Areas Where your application lives during execution: Heap – Shared memory for objects. Modern collectors like ZGC and Shenandoah offer near-pause-less GC even at massive scales. Method Area – Holds class metadata, now part of Metaspace (off-heap since Java 8). Stacks – Each thread (including virtual threads in Java 21+) gets its own stack for method calls and local variables. PC Register – Keeps track of the current bytecode instruction per thread. Native Method Stack – Supports calls to native (non-Java) code via JNI. 🔍 Java 25+ virtual threads (Project Loom) are radically efficient because they require far less stack memory. ⚙️ 3. Execution Engine Turns bytecode into real execution: Interpreter – Quick to start, reads bytecode one instruction at a time. JIT Compiler – Just-in-time compiles hot methods to native machine code using C2 and Graal. GC Engine – Modern collectors like ZGC offer ultra-low pause times, and adaptive memory regions. 💡 JVMs now self-tune aggressively using runtime profiling and tiered compilation strategies. 🌉 4. Native Interface (JNI) & Foreign Function Support JNI – Traditional way to call C/C++ code (still widely used). Project Panama (Java 22+) – Introduced the Foreign Function & Memory API, making native interop easier, faster, and safer — no more verbose JNI boilerplate. 🌐 JVM in 2025: Modern Capabilities ✅ Virtual threads: Lightweight concurrency, ideal for millions of parallel tasks. ✅ Record classes & sealed hierarchies: Better modeling with strong typing and compiler safety. ✅ Pattern matching: Cleaner, more expressive instanceof, switch, and deconstruction logic. ✅ Improved startup & native images: With tools like GraalVM and jlink, you can generate lean, fast-starting runtimes. #Java25 #JVM #VirtualThreads #JavaInternals
To view or add a comment, sign in
-
🧵 WebFlux vs Virtual Threads — The Real Talk (2025 Edition) When Java 8 introduced CompletableFuture, it felt revolutionary. Then came Spring WebFlux — non-blocking, reactive, super-scalable. And then most of us said: “Wait… do I need a PhD to read this code?” 😅 Now, Java 21’s Virtual Threads (Project Loom) have changed the game again. So… is WebFlux still worth it? Let’s be real 👇 ⚡ WebFlux (Reactive Programming) Built on Reactor, WebFlux is all about streams and back-pressure — you push data through Flux and Mono like an event pipeline. ✅ Best for: - Real-time data: WebSockets, SSE - Reactive databases: R2DBC, Mongo - Streaming APIs: Kafka, MQTT ❌ But also... - Steep learning curve - Complex API surface (hundreds of operators) - Business logic becomes a maze of .flatMap(), .switchIfEmpty(), .zipWith(), .concatMap(), .onErrorResume() 😵💫 - Debugging stack traces feels like archaeology You don’t use WebFlux — you learn it like a language. 🧵 Virtual Threads (Imperative Programming) Then Java 21 arrived and said: “What if we just made threads cheap instead?” Now you can write clean, blocking-style code that scales like async code. No callbacks, no reactive streams, no operator jungle. ✅ Best for: - REST APIs with blocking dependencies (JDBC, external APIs) - Legacy system migrations - Teams who want simplicity without losing scalability 🧠 Example: @GetMapping("/users") public List<User> getUsers() { return repository.findAll(); // blocking, but virtual threads handle it } Feels like old Spring MVC, performs like WebFlux. ⚖️ So who wins? - Real-time streaming (SSE, WebSocket) : ⚡WebFlux - Reactive DB or Kafka integration :⚡WebFlux - Simple REST APIs / blocking I/O : 🧵Virtual Threads - Readable, maintainable logic : 🧵Virtual Threads 💬 My Take: - WebFlux isn’t dead — it’s just specialized. - Virtual Threads bring async scalability to the masses — no reactive gymnastics required. - The smartest engineers in 2025? They know when to go reactive, and when to just write clean code. 👉 What’s your team using right now — WebFlux, Virtual Threads, or still Servlet threads? Drop your thoughts below 👇 #Java #SpringBoot #WebFlux #VirtualThreads #ProjectLoom #ReactiveProgramming #SoftwareArchitecture #Coding
To view or add a comment, sign in
-
-
𝗩𝗶𝗿𝘁𝘂𝗮𝗹 𝗧𝗵𝗿𝗲𝗮𝗱𝘀: 𝗧𝗵𝗲 𝗙𝘂𝘁𝘂𝗿𝗲 𝗼𝗳 𝗝𝗮𝘃𝗮 𝗖𝗼𝗻𝗰𝘂𝗿𝗿𝗲𝗻𝗰𝘆 🔥 Java's concurrency model is evolving. For years, we relied on OS threads - powerful but heavy. Now, Project Loom changes everything. 𝟭. 𝗧𝗵𝗲 𝗧𝗿𝗮𝗱𝗶𝘁𝗶𝗼𝗻𝗮𝗹 𝗧𝗵𝗿𝗲𝗮𝗱 𝗠𝗼𝗱𝗲𝗹 Java threads mapped directly to OS threads. This worked initially but became costly: - 1 MB memory per thread - Expensive context switching - Limited to thousands of threads - Blocking I/O wasted resources This architecture makes it difficult to build highly concurrent applications such as modern APIs or event-driven systems. 𝟮. 𝗪𝗵𝘆 𝗥𝗲𝗮𝗰𝘁𝗶𝘃𝗲 𝗣𝗿𝗼𝗴𝗿𝗮𝗺𝗺𝗶𝗻𝗴 𝗘𝗺𝗲𝗿𝗴𝗲𝗱 To address these scalability issues, Reactive Programming introduced a new approach using non-blocking I/O and event loops. Frameworks like Spring WebFlux leveraged this model to handle thousands of concurrent requests efficiently. - Complex programming models - Difficult debugging - Steep learning curves (Mono, Flux) 𝟯. 𝗘𝗻𝘁𝗲𝗿 𝗣𝗿𝗼𝗷𝗲𝗰𝘁 𝗟𝗼𝗼𝗺 Java 21 introduced Virtual Threads - lightweight, JVM-managed threads that scale to millions: - Blocking is now cheap - Context switching happens in the JVM - 𝗪𝗿𝗶𝘁𝗲 𝘀𝘆𝗻𝗰𝗵𝗿𝗼𝗻𝗼𝘂𝘀 𝗰𝗼𝗱𝗲 𝘁𝗵𝗮𝘁 𝗽𝗲𝗿𝗳𝗼𝗿𝗺𝘀 𝗹𝗶𝗸𝗲 𝗮𝘀𝘆𝗻𝗰 𝘀𝘆𝘀𝘁𝗲𝗺𝘀 Virtual Threads are managed by the JVM, not the operating system. They’re cheap to create, use minimal memory, and can scale to millions of concurrent operations. 𝟰. 𝗪𝗵𝗮𝘁 𝗟𝗼𝗼𝗺 𝗖𝗵𝗮𝗻𝗴𝗲𝘀 Project Loom eliminates the simplicity vs. scalability trade-off: - Millions of threads without event loops - Readable stack traces - Works with existing Java libraries (JDBC, RestTemplate) 𝟱. 𝗦𝗽𝗿𝗶𝗻𝗴 𝗠𝗩𝗖 + 𝗟𝗼𝗼𝗺 𝘃𝘀 𝗪𝗲𝗯𝗙𝗹𝘂𝘅 With Loom: Spring MVC achieves WebFlux-level scalability while keeping the traditional model. WebFlux remains best for: - Real-time streaming (SSE, WebSockets) - Reactive pipelines - Backpressure scenarios Loom doesn't replace WebFlux—it redefines where each fits. Spring Framework 𝗹𝗲𝗮𝗱 𝗝𝘂𝗲𝗿𝗴𝗲𝗻 𝗛𝗼𝗲𝗹𝗹𝗲𝗿 𝗮𝗻𝗱 𝗰𝗼𝗿𝗲 𝗱𝗲𝘃𝗲𝗹𝗼𝗽𝗲𝗿 𝗦é𝗯𝗮𝘀𝘁𝗶𝗲𝗻 𝗗𝗲𝗹𝗲𝘂𝘇𝗲 have confirmed: “With Project Loom, Spring MVC becomes as scalable as WebFlux. WebFlux will remain the best choice for reactive and streaming use cases.” 𝟲. 𝗧𝗵𝗲 𝗙𝘂𝘁𝘂𝗿𝗲 𝗼𝗳 𝗝𝗮𝘃𝗮 𝗖𝗼𝗻𝗰𝘂𝗿𝗿𝗲𝗻𝗰𝘆 Virtual threads are production-ready in Java 21, with Spring Framework 6.1+ and Boot 3.2+ supporting them natively. For developers: - Spring MVC scales for modern workloads - WebFlux focuses on reactive/streaming cases - Prioritize readability without losing performance 𝟳. 𝗖𝗼𝗻𝗰𝗹𝘂𝘀𝗶𝗼𝗻 Project Loom bridges traditional and reactive programming. Write clean, synchronous code that scales effortlessly. The future is clear: Simplicity, scalability, and choice. #100DaysOfCode #Java #Springboot #ProjectLoom #VirtualThreads #Spring #SpringMVC #WebFlux #ReactiveProgramming #Microservices #JavaConcurrency #Backend #TechLeadership
To view or add a comment, sign in
-
-
💡 Tech Deep Dive: Decoding JSON Arrays with Gson Ever noticed how most of our Spring (or general Java) application logic revolves around a single JSON object for requests and responses? It’s the standard practice. But for a recent personal project, I hit a technical snag: I needed my program to consume and process an array of JSON objects, not just one. I had a string containing a list of objects, and the standard Gson constructor—which maps a single JSON string to a Java object—wasn't cutting it. That's when I uncovered two elegant techniques within the Gson library: the TypeToken and the JsonArray. The Solution: Using TypeToken (My Choice) The standard way to map a JSON to a Java object works great: Java // Works for a single JSON object MyObject obj = gson.fromJson(jsonString, MyObject.class); But when dealing with an array of objects ([{}, {}, ...]), Gson can't tell the difference between a List<MyObject> and just MyObject.class because of type erasure at runtime. The solution? TypeToken! By using TypeToken, we provide Gson with the full, parameterized type information (List<MyObject>), allowing for a direct, one-line mapping: Java // Works for a JSON array to a List<MyObject> Type type = new TypeToken<List<MyObject>>() {}.getType(); List<MyObject> list = gson.fromJson(jsonArrayString, type); This approach is super clean! It instantly converts the entire JSON array into a readily usable List that I could then easily run lambda operations and other stream logic on. No extra looping required! The Alternative: Using JsonArray The other interesting path is using JsonArray. This approach first parses the raw JSON string into a JsonArray object, which is part of Gson's DOM (Document Object Model) representation. Java // Step 1: Parse the string into a JsonArray object JsonArray jsonArray = JsonParser.parseString(jsonArrayString).getAsJsonArray(); // Step 2: Manually iterate and map each element List<MyObject> list = new ArrayList<>(); for (JsonElement element : jsonArray) { MyObject obj = gson.fromJson(element, MyObject.class); list.add(obj); } While it requires an extra step of manual iteration, it’s a powerful approach when you need to inspect or modify the JSON elements before converting them into Java objects. It’s always fascinating how diving into the specifics of a library reveals such powerful and nuanced solutions. Highly recommend looking into TypeToken if you ever run into a complex JSON mapping scenario! --------------------------------------------------------------------------------- Img Source - My own unpublished code #Java #Gson #JSON #TypeToken #SoftwareDevelopment #TechTip #Software #ThirdPartyLibrary #Google #ObjectArray #Programming #PersonalProject #Learning
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development