: : Method References 👉 It’s all about who is the object. 🧠 1️⃣ Static Method Reference ClassName::staticMethod No object needed. Just direct method mapping. Equivalent to: x -> ClassName.method(x) 🧠 2️⃣ Instance Method Reference (Specific Object) object::method Here, the object is fixed. The stream elements are passed as arguments. Equivalent to: x -> object.method(x) The object never changes. 🧠 3️⃣ Arbitrary Object Method Reference ClassName::instanceMethod The class name is just type information. The stream element is the real object. This is where it gets interesting. Here, each stream element becomes the object. Equivalent to: x -> x.method() The object changes for every element. The method stays the same. That’s the real difference. 👍 Instance method reference (specific object): one fixed object, stream elements are just arguments to its method. 👍 👍 Arbitrary method reference: each stream element becomes the object, and its own method is called. 🧠 4️⃣ Constructor Reference ClassName::new Just a cleaner version of: x -> new ClassName(x)| 💡 The Big Realization Method references are not magic. They are just cleaner lambdas. And they only work because of functional interfaces GitHub Link: https://lnkd.in/gUr-ifF8 🔖Frontlines EduTech (FLM) #java #MethodReferences #Java8 #FunctionalProgramming #LambdaExpressions #StreamsAPI #OOPDesign #BackendDevelopment #Programming #CleanCode #ResourceManagement #AustraliaJobs #SwitzerlandJobs #NewZealandJobs #USJobs #MethodReferenceOperator
Prasanna Peethambaram’s Post
More Relevant Posts
-
#Post4 In the previous post(https://lnkd.in/dBEvfvyM), we explored how ConcurrentHashMap achieves thread safety. During that discussion, two important concepts came up: 1. CAS (Compare-And-Swap) 2. Volatile keyword In this post, let’s understand these two concepts in detail. 1. CAS (Compare-And-Swap) CAS is a hardware-level atomic operation used to update a value without using locks. It works using three components: • Memory location → where the value is stored • Expected value → the value we assume is currently present • New value → value to update if expected matches Working: If current value == expected value → update to new value Else → operation fails This makes CAS: • Atomic • Lock-free • Efficient for concurrent systems ABA Problem CAS only checks the current value, not the history. Example: A value changes like this: 10 → 12 → 10 Now CAS sees expected = 10 and current = 10 → it succeeds, even though the value changed in between. This is called the ABA problem. It can be solved using versioning or timestamps (e.g., AtomicStampedReference in Java). 2. Volatile Keyword Volatile is used to ensure visibility across threads. Without volatile: A thread may read a stale value from its local cache. With volatile: Every read gets the latest value from main memory. Example: Thread-1 updates a variable Thread-2 immediately sees the updated value (if volatile is used) How CAS and Volatile Work Together CAS alone is not enough. It needs visibility to work correctly. • Volatile ensures all threads see the latest value • CAS ensures updates happen safely without locking Together, they enable high-performance concurrent systems. Key takeaway CAS → ensures atomic updates without locks Volatile → ensures visibility across threads Together → enable efficient and thread-safe operations (as used in ConcurrentHashMap) In upcoming posts, we will explore these concepts even deeper and understand how threads work internally in Java. #Java #SoftwareEngineering #Multithreading #ConcurrentProgramming #BackendDevelopment #Programming
To view or add a comment, sign in
-
🚀 LeetCode #1281 | Simple Logic, Strong Foundation Sometimes the easiest problems build the strongest logic 💡 🔢 Problem: Given an integer "n", return the difference between the product of its digits and the sum of its digits. 👉 Example: Input: "n = 234" Product = 2 × 3 × 4 = 24 Sum = 2 + 3 + 4 = 9 Output = 24 - 9 = 15 --- 🧠 Approach (Step-by-Step): 1. Extract digits using "% 10" 2. Add digits → Sum 3. Multiply digits → Product 4. Return "product - sum" --- 💻 Java Code: class Solution { public int subtractProductAndSum(int n) { int sum = 0; int product = 1; while(n > 0){ int digit = n % 10; sum += digit; product *= digit; n /= 10; } return product - sum; } } --- 🎯 Key Learnings: ✔ Digit extraction using "%" and "/" ✔ Difference between accumulation (sum) & multiplication (product) ✔ Clean loop logic --- 💡 Real-Life Analogy: Think of digits as daily tasks: - Sum = total effort - Product = combined impact 👉 Result shows how much impact differs from effort! --- 🔥 Why this matters? Even simple problems sharpen your fundamentals — and strong basics = strong interviews 💪 --- #LeetCode #Java #Coding #DSA #ProblemSolving #Placements #SoftwareEngineer #LearningJourney
To view or add a comment, sign in
-
💡 Understanding Collection-Based Dependency Injection in Spring When we talk about Dependency Injection (DI) in Spring, most of us think about injecting a single object. But did you know you can inject multiple beans at once using collections? 🤔 Let’s break it down in a simple way 👇 🔹 What is Collection-Based DI? Instead of injecting one object, Spring can inject a list, set, or map of beans into your class. 👉 This is useful when you have multiple implementations of the same interface. 🔹 What’s Happening Here? ✅ Spring scans all beans of type PaymentService ✅ It collects them into a List ✅ Injects them automatically into PaymentProcessor 🔹 Other Supported Collections You can also use: ✔ List<Interface> ✔ Set<Interface> ✔ Map<String, Interface> → Key = bean name Example: Map<String, PaymentService> paymentMap; 🔹 Why is this Useful? ✔ Helps implement strategy pattern easily ✔ Avoids manual bean selection ✔ Makes code more flexible & scalable 🔹 Pro Tip 🚀 Spring injects collections in a specific order if you use @Order or @Priority. 🔚 Summary Collection-based DI = Inject multiple beans → Handle dynamically → Write cleaner code If you're learning Spring deeply, this concept is a game changer when building scalable systems 🔥 #Java #SpringBoot #DependencyInjection #BackendDevelopment #CleanCode
To view or add a comment, sign in
-
-
📘 Strengthening my knowledge on the 12 Rules of Interface 1️⃣ Interface is a contract 👉 Defines rules that classes must follow ✔ Used for standardization 2️⃣ Cannot create object of interface 👉 Interfaces are incomplete (no full implementation) ❌ new InterfaceName() not allowed 3️⃣ Methods are public & abstract by default 👉 No need to write public abstract ✔ Compiler adds it automatically 4️⃣ Variables are public, static, final 👉 Constant values only ✔ Must be initialized 5️⃣ No constructor in interface 👉 Because object creation is not possible 6️⃣ A class uses implements keyword class A implements InterfaceName 7️⃣ Class must implement all abstract methods 👉 Otherwise class must be declared abstract 8️⃣ Interface supports multiple inheritance 👉 A class can implement multiple interfaces class A implements I1, I2 9️⃣ Interface can extend another interface interface B extends A 🔟 Interface cannot extend a class 👉 Only interfaces can extend interfaces 1️⃣1️⃣ Default methods (JDK 8) 👉 Method with body inside interface ✔ Supports backward compatibility 1️⃣2️⃣ Static methods (JDK 8) 👉 Access using interface name InterfaceName.method() ❌ Not inherited / overridden 🎯 Shortcut Memory Tip 👉 Interface = Contract + Abstract + Constants + JDK8 features #Java #OOP #Interfaces #Programming #Learning #CodingJourney #Developers #TapAcademy
To view or add a comment, sign in
-
-
🚨 𝗦𝘁𝗼𝗽 𝗰𝗼𝗻𝗳𝘂𝘀𝗶𝗻𝗴 𝗳𝗶𝗻𝗮𝗹, 𝗳𝗶𝗻𝗮𝗹𝗹𝘆, 𝗮𝗻𝗱 𝗳𝗶𝗻𝗮𝗹𝗶𝘇𝗲() 𝗶𝗻 𝗝𝗮𝘃𝗮! 🚨 These three look similar but serve very different purposes. Let’s break it down 👇 🔹 𝗳𝗶𝗻𝗮𝗹 keyword • Used with variables, methods, and classes • Variable → value cannot be changed • Method → cannot be overridden • Class → cannot be inherited 🔸 𝗳𝗶𝗻𝗮𝗹𝗹𝘆 block • Part of exception handling • Always executes after try/catch • Great for cleanup code (closing files, releasing resources) 🔹 𝗳𝗶𝗻𝗮𝗹𝗶𝘇𝗲() method • Defined in Object class • Called by 𝗚𝗮𝗿𝗯𝗮𝗴𝗲 𝗖𝗼𝗹𝗹𝗲𝗰𝘁𝗼𝗿 before object is destroyed • Rarely used in modern Java (better alternatives exist) 💡 𝗘𝘅𝗮𝗺𝗽𝗹𝗲: 𝘧𝘪𝘯𝘢𝘭 𝘪𝘯𝘵 𝘹 = 10; // 𝘤𝘢𝘯𝘯𝘰𝘵 𝘤𝘩𝘢𝘯𝘨𝘦 𝘹 𝘵𝘳𝘺 { // 𝘳𝘪𝘴𝘬𝘺 𝘤𝘰𝘥𝘦 } 𝘤𝘢𝘵𝘤𝘩(𝘌𝘹𝘤𝘦𝘱𝘵𝘪𝘰𝘯 𝘦) { // 𝘩𝘢𝘯𝘥𝘭𝘦 𝘦𝘳𝘳𝘰𝘳 } 𝘧𝘪𝘯𝘢𝘭𝘭𝘺 { // 𝘤𝘭𝘦𝘢𝘯𝘶𝘱 𝘤𝘰𝘥𝘦 } @𝘖𝘷𝘦𝘳𝘳𝘪𝘥𝘦 𝘱𝘳𝘰𝘵𝘦𝘤𝘵𝘦𝘥 𝘷𝘰𝘪𝘥 𝘧𝘪𝘯𝘢𝘭𝘪𝘻𝘦() 𝘵𝘩𝘳𝘰𝘸𝘴 𝘛𝘩𝘳𝘰𝘸𝘢𝘣𝘭𝘦 { // 𝘤𝘭𝘦𝘢𝘯𝘶𝘱 𝘣𝘦𝘧𝘰𝘳𝘦 𝘎𝘊 } 📌 𝗧𝗟;𝗗𝗥: final → restriction keyword finally → cleanup block finalize() → GC hook (legacy, avoid in new code) #Java #CodingTips #Programming #SoftwareEngineering #Learning #SpringBoot #garbagecollection
To view or add a comment, sign in
-
-
🚀 Dependency Injection in Spring — But Which Type Should You Use? If you're learning Spring Boot, you've probably heard about Dependency Injection (DI). But here’s where many beginners get confused 👇 👉 Constructor vs Setter vs Field Injection Let’s break it down simply: 🔹 1. Constructor Injection (⭐ Recommended) @Component public class OrderService { private final PaymentService paymentService; public OrderService(PaymentService paymentService) { this.paymentService = paymentService; } } ✅ Best for mandatory dependencies ✅ Promotes immutability ✅ Easier to test (no reflection magic) 💡 Spring automatically injects dependency if there's only one constructor. 🔹 2. Setter Injection @Component public class OrderService { private PaymentService paymentService; @Autowired public void setPaymentService(PaymentService paymentService) { this.paymentService = paymentService; } } ✅ Useful for optional dependencies ✅ Allows changing dependency later ⚠️ Can lead to partially initialized objects 🔹 3. Field Injection (❌ Avoid in Production) @Component public class OrderService { @Autowired private PaymentService paymentService; } ✅ Quick and concise ❌ Hard to test ❌ Breaks encapsulation ❌ Uses reflection (less control) 🧠 So what should you use? 👉 Constructor Injection = Default Choice 👉 Setter Injection = When dependency is optional 👉 Field Injection = Only for quick demos 🔥 Pro Tip If you're preparing for interviews or building real projects: Always prefer Constructor Injection. 💬 What do you use in your projects? #Java #SpringBoot #DependencyInjection #BackendDevelopment #Coding #SoftwareEngineering
To view or add a comment, sign in
-
𝐃𝐚𝐲 𝟒𝟗 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on checking whether a string can be segmented into valid dictionary words. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Word Break 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 – 𝐃𝐅𝐒 + 𝐌𝐞𝐦𝐨𝐢𝐳𝐚𝐭𝐢𝐨𝐧 • Converted the word list into a HashSet for fast lookup • Used recursion to try breaking the string into prefixes • If prefix exists in dictionary, recursively check the remaining string • Stored results in a memo map to avoid recomputation This avoids exponential re-checking of the same substrings. 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • Memoization helps optimize recursive solutions • Breaking problems into smaller substrings is a common pattern • HashSet improves lookup efficiency • DP problems often start with recursion and get optimized 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(n²) (with memoization) • Space: O(n) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 When recursion feels slow, adding memoization can transform it into an efficient solution. 49 days consistent 🚀 On to Day 50. #DSA #Arrays #DynamicProgramming #Recursion #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
🚀 𝐌𝐚𝐬𝐭𝐞𝐫𝐢𝐧𝐠 𝐍𝐚𝐦𝐢𝐧𝐠 𝐂𝐨𝐧𝐯𝐞𝐧𝐭𝐢𝐨𝐧𝐬 𝐢𝐧 𝐏𝐫𝐨𝐠𝐫𝐚𝐦𝐦𝐢𝐧𝐠 🚀 Consistency in naming isn’t just style—it’s clarity, collaboration, and long-term success. Here are some of the most common conventions every developer should know: 🔹 𝐂𝐚𝐦𝐞𝐥 𝐂𝐚𝐬𝐞 👉 Example: firstName 💡 Usage: Variables & functions 🔹 𝐏𝐚𝐬𝐜𝐚𝐥 𝐂𝐚𝐬𝐞 👉 Example: UserAccount 💡 Usage: Classes, interfaces, enums 🔹 𝐒𝐧𝐚𝐤𝐞 𝐂𝐚𝐬𝐞 👉 Example: user_account 💡 Usage: Python variables, DB fields 🔹 𝐊𝐞𝐛𝐚𝐛 𝐂𝐚𝐬𝐞 👉 Example: user-account 💡 Usage: URLs, CSS classes 🔹 𝐒𝐜𝐫𝐞𝐚𝐦𝐢𝐧𝐠 𝐂𝐚𝐬𝐞 👉 Example: MAX_SIZE 💡 Usage: Constants 🔹 𝐇𝐮𝐧𝐠𝐚𝐫𝐢𝐚𝐧 𝐍𝐨𝐭𝐚𝐭𝐢𝐨𝐧 👉 Example: strFirstName, arrUserNames 💡 Usage: Legacy C/C++ style ✨ 𝐁𝐞𝐬𝐭 𝐏𝐫𝐚𝐜𝐭𝐢𝐜𝐞𝐬 1. Stay consistent across your codebase 2. Use clear, descriptive names 3. Follow language-specific guidelines (PEP 8, Java conventions, etc.) 𝘎𝘰𝘰𝘥 𝘯𝘢𝘮𝘪𝘯𝘨 = 𝘳𝘦𝘢𝘥𝘢𝘣𝘭𝘦, 𝘮𝘢𝘪𝘯𝘵𝘢𝘪𝘯𝘢𝘣𝘭𝘦, 𝘢𝘯𝘥 𝘴𝘤𝘢𝘭𝘢𝘣𝘭𝘦 𝘤𝘰𝘥𝘦. 🧑💻 #Programming #CodeQuality #BestPractices #SoftwareDevelopment #CleanCode #DevTips
To view or add a comment, sign in
-
To achieve a good Object-Oriented Design, you need to embrace the Dependency Inversion Principle. Read more 👉 https://lttr.ai/AoK3T #DependencyInversionPrinciple #java #SoftwareDesign #SOLIDPrinciples
To view or add a comment, sign in
-
🚀 DAY 64/150 — SIMULATING A QUEUE PROCESS! 🚀 Day 64 of my 150 Days DSA Challenge in Java and today I solved an interesting problem based on queue simulation and logical observation 💻🧠 📌 Problem Solved: Time Needed to Buy Tickets 📌 LeetCode: #2073 📌 Difficulty: Easy–Medium The problem describes a queue of people where each person wants to buy a certain number of tickets. Each second, the person at the front buys one ticket and moves to the end of the queue if they still need more tickets. The goal is to calculate the total time required for the person at index k to finish buying their tickets. 🔹 Approach Used Instead of fully simulating the queue step by step, I used logical observation: • People before index k can buy up to tickets[k] tickets • People after index k can buy up to tickets[k] - 1 tickets • Sum these contributions to calculate the total time directly This avoids unnecessary queue simulation and leads to a cleaner solution. ⏱ Complexity Time Complexity: O(n) Space Complexity: O(1) 🧠 What I Learned • Some simulation problems can be optimized by analyzing patterns instead of simulating every step • Understanding the behavior of queue operations helps simplify the logic • Observational optimization often turns a brute-force simulation into a linear solution 💡 Key Takeaway This problem taught me that not every queue problem needs an actual queue implementation — sometimes understanding the process mathematically can produce a more efficient solution. ✅ Day 64 completed 🚀 86 days to go 🔗 Java Solution on GitHub: 👉 https://lnkd.in/gTanCWzM 💡 Good problem solving is about recognizing patterns, not just writing loops. #DSAChallenge #Java #LeetCode #Queue #ProblemSolving #150DaysOfCode #CodingJourney #InterviewPrep #LearningInPublic
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