🚀 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
Fixed Java Code for "Contains Duplicate II" with Sliding Window
More Relevant Posts
-
⚡ 10+ Approaches to Find Max or Min Value in Java 8 Streams — Master It Like a Pro 💪 Working with Java 8 Streams? Here are 10 powerful and elegant ways to find the maximum or minimum value from a list — all clean, modern, and interview-ready 👇 🧩 Sample Data List<Integer> myList = Arrays.asList(10, 15, 8, 49, 25, 98, 98, 32, 15); 🔥 Find Maximum Value — 10 Approaches List<Integer> myList = Arrays.asList(10,15,8,49,25,98,98,32,15); 1️⃣ Using instance comparator (compareTo on elements) myList.stream().max(Integer::compareTo).get(); 2️⃣ Using static comparator (Integer.compare) myList.stream().max(Integer::compare).get(); 3️⃣ Using IntStream for primitive comparison myList.stream().mapToInt(Integer::intValue).max().getAsInt(); 4️⃣ Using reduce() with Integer::max myList.stream().reduce(Integer::max).get(); 5️⃣ Using Collectors.summarizingInt() for all stats myList.stream().collect(Collectors.summarizingInt(i -> i)).getMax(); 6️⃣ Using Comparator.comparingInt() (good for objects too) myList.stream().max(Comparator.comparingInt(Integer::intValue)).get(); 7️⃣ Using Comparator.naturalOrder() for clean syntax myList.stream().max(Comparator.naturalOrder()).get(); 8️⃣ Using Comparable::compareTo (generic Comparable) myList.stream().max(Comparable::compareTo).get(); 9️⃣ Using custom lambda comparator (manual compare logic) myList.stream().max((a,b) -> a>b ? 1 : (a<b ? -1 : 0)).get(); 🔟 Using summaryStatistics() for one-line numeric max myList.stream().mapToInt(Integer::intValue).summaryStatistics().getMax(); 🕚 Using sorted() + skip() to pick last element myList.stream().sorted().skip(myList.size()-1).findFirst().get(); 🌙 Same Approaches for Find Minimum Value List<Integer> myList = Arrays.asList(10, 15, 8, 49, 25, 98, 98, 32, 15); 1️⃣ myList.stream().min(Integer::compareTo).get(); 2️⃣ myList.stream().min(Integer::compare).get(); 3️⃣ myList.stream().mapToInt(Integer::intValue).min().getAsInt(); 4️⃣ myList.stream().reduce(Integer::min).get(); 5️⃣ myList.stream().collect(Collectors.summarizingInt(i -> i)).getMin(); 6️⃣ myList.stream().min(Comparator.comparingInt(Integer::intValue)).get(); 7️⃣ myList.stream().min(Comparator.naturalOrder()).get(); 8️⃣ myList.stream().min(Comparable::compareTo).get(); 9️⃣ myList.stream().min((a, b) -> a > b ? 1 : (a < b ? -1 : 0)).get(); 🔟 myList.stream().mapToInt(Integer::intValue).summaryStatistics().getMin(); 🕚 myList.stream().sorted().findFirst().get(); 💡 Quick Summary for Efficiency 🧠 For numeric lists → mapToInt().max() or summaryStatistics() 🚀 For objects → Comparator.comparing() 📊 For analytics → Collectors.summarizingInt() 💪 For readability → reduce() or naturalOrder() ⚙️ For demos → sorted().skip() (less efficient) 👉 Follow me for more Java 8, Streams, and interview-ready code tips! 🔖 #Java #Java8 #Streams #CodingTips #FunctionalProgramming #InterviewPreparation #CleanCode #Developers #LinkedInLearning #CodeNewbie
To view or add a comment, sign in
-
🚀 𝗠𝗮𝘀𝘁𝗲𝗿𝗶𝗻𝗴 𝗗𝗲𝗽𝗲𝗻𝗱𝗲𝗻𝗰𝘆 𝗜𝗻𝗷𝗲𝗰𝘁𝗶𝗼𝗻 — 𝗧𝗵𝗲 𝗦𝗲𝗰𝗿𝗲𝘁 𝘁𝗼 𝗖𝗹𝗲𝗮𝗻 & 𝗦𝗰𝗮𝗹𝗮𝗯𝗹𝗲 𝗝𝗮𝘃𝗮 𝗖𝗼𝗱𝗲 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
To view or add a comment, sign in
-
Java ke woh secrets jo documentation mein bhi nahi hain! 🔥 --- Post 1: Java Compiler ka "Negative Array Size" bug!🤯 ```java public class NegativeArray { public static void main(String[] args) { try { int[] arr = new int[-5]; // ❌ Negative size } catch (NegativeArraySizeException e) { // Ye exception JVM ke internal memory allocation se aati hai // Java specification ke according, yeh error compulsory hai! } } } ``` Secret: Java specification page 329: "Array creation with negative size must throw NegativeArraySizeException" - ye rule JVM implementers ko follow karna compulsory hai! 💀 --- Post 2: Java ka "Floating-Point Non-Associativity" paradox!🔥 ```java public class FloatingParadox { public static void main(String[] args) { double a = 1.0e308; // Very large number double b = -1.0e308; // Very large negative double c = 1.0; double result1 = (a + b) + c; // (0) + 1 = 1 double result2 = a + (b + c); // Infinity + (-Infinity) = NaN System.out.println(result1); // 1.0 System.out.println(result2); // NaN } } ``` Mathematical Paradox: Java floating-point arithmetic associative nahi hai! IEEE 754 standard ka hidden rule! 💡 --- Post 3: Java ka "Class File Magic Number" ka raaz!🚀 ```java // Har .class file ke start mein ye 4 bytes hote hain: // CA FE BA BE - "Coffee Baby"! // Ye Java creators ne randomly choose kiya tha 1995 mein! public class MagicNumber { public static void main(String[] args) throws Exception { byte[] classBytes = java.nio.file.Files.readAllBytes( java.nio.file.Paths.get("MagicNumber.class") ); // First 4 bytes check karo System.out.printf("%02X %02X %02X %02X", classBytes[0], classBytes[1], classBytes[2], classBytes[3]); // Output: CA FE BA BE } } ``` Historical Secret: James Gosling team ne ye magic number randomly rakha tha! 💪 --- Post 4: Java ka "Null Type" ka compiler-level existence!🔮 ```java public class NullType { public static void main(String[] args) { // Java compiler internally null ka ek special type maintain karta hai // called "the null type" - jiska koi name nahi hota! String s = null; Integer i = null; // Compiler null ko kisi bhi reference type assign kar sakta hai // kyunki uska apna alag "null type" hai! } } ``` Language Theory: Java Language Specification §4.1: "There is also a special null type, the type of the expression null" - ye type sirf compiler internally use karta hai! 💀
To view or add a comment, sign in
-
Java25 : main() , Compact Source, IO In Java 25 : The language significantly simplifies writing a main method by removing much of the traditional boilerplate. The new "Compact Source Files and Instance Main Methods" feature (JEP 512) allows you to write simple programs without a class wrapper, a public or static modifier, or the String[] args parameter. Writing main methods in Java 25 - 1.Traditional main method The classic, fully-featured main method remains the standard for most applications and is fully backward-compatible. // MyProgram.java public class MyProgram { public static void main(String[] args) { System.out.println("Hello, " + args[0] + "!"); } } 2. Simplified main method within a class For smaller programs, you can now write a less verbose main method inside a class, omitting the public, static, and String[] args parts. The JVM will look for this simplified version if it doesn't find the traditional one. java // HelloWorld.java class HelloWorld { void main() { System.out.println("Hello, World!"); } } 3. "Compact source file" with instance main For the most minimal entry point, you can place the simplified main method directly in a file without an enclosing class. The compiler automatically wraps the code in an implicit, unnamed class. // Minimal.java void main() { System.out.println("Hello from a compact source file!"); } 4. Using the java.lang.IO helper class To further reduce boilerplate for console I/O, Java 25 introduces a java.lang.IO helper class, making it easier to read and write from the console. java // IOExample.java import static java.lang.IO.*; void main() { var name = readln("What's your name? "); println("Hello, " + name + "!"); } Article: Java's main method gets a beginner-friendly reboot In its latest long-term support (LTS) version, Java 25, the platform has made a significant update to how developers, particularly beginners, can write the entry point to their programs. By finalizing JEP 512, which introduces "Compact Source Files and Instance Main Methods," Java has shed some of its long-standing "boilerplate" to become more accessible and intuitive. The feature, which was refined over several preview releases (including Java 21–24), provides a smoother on-ramp for new programmers by removing the need to understand complex, enterprise-level concepts like public, static, and the String[] args parameter from day one.
To view or add a comment, sign in
-
💡 Java 8 Feature Spotlight: Stream API 🚀 Before Java 8: Manual Iteration Pain: Working with collections meant lots of for-loops or iterators for tasks like filtering, transforming, or aggregating data. Mutability Hassles: Needed temporary lists and mutable storage, cluttering logic. Difficult Parallelization: Making iteration parallel was tricky and error-prone. Example (pre-Java 8): Filtering: ----------> List<Employee> highEarners = new ArrayList<>(); for (Employee e : employees) { if (e.getSalary() > 100000) { highEarners.add(e); } } ----------> Example (pre-Java 8): Transformation/Mapping: ----------> List<String> names = new ArrayList<>(); for (Employee e : employees) { names.add(e.getName()); } ----------> With Java 8’s Stream API: What is the Stream API? Stream API introduces a new abstraction for processing sequences of data, letting you write concise, declarative code to filter, map, sort, and aggregate. Mapping Operations with .map(): What is Mapping? Mapping is the process of converting each element in a stream to another form using a function. Why it Matters: In Java 8, the .map() method applies a transformation to every item in a stream, producing a new stream with the results. It makes data transformation intuitive and reduces boilerplate. Examples with Stream API: Elegant Filtering: ----------> List<Employee> highEarners = employees.stream() .filter(e -> e.getSalary() > 100000) .collect(Collectors.toList()); ----------> Elegant Mapping: ----------> List<String> names = employees.stream() .map(Employee::getName) .collect(Collectors.toList()); ----------> Numeric Mapping: List<Integer> doubledNumbers = numbers.stream() .map(n -> n * 2) .collect(Collectors.toList()); ----------> Key Improvements and Benefits: ✅ Less Code, More Clarity: Express complex operations in just a few lines. ✅ Readable and Declarative: Focus on what you want done, not how. ✅ Powerful Chaining: Easily combine mapping, filtering, sorting, and grouping. ✅ Effortless Parallelism: Easily unlock multi-core processing with .parallelStream(). In summary: Java 8’s Stream API—including its powerful mapping operations—makes your data workflows cleaner, faster, and much more expressive! #Java8 #StreamAPI #MapOperation #FunctionalProgramming #DataTransformation #TechInnovation #JavaProgramming
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
-
Java ke parallel universe ke secrets! 🔥 --- Post 1: Java ka "Hidden Class" API - Java 15+ ka best kept secret!🤯 ```java import java.lang.invoke.*; public class HiddenClassMagic { public static void main(String[] args) throws Throwable { MethodHandles.Lookup lookup = MethodHandles.lookup(); byte[] classBytes = getClassBytes(); // Bytecode bytes // Hidden class banayi jo reflection mein visible nahi hogi! Class<?> hiddenClass = lookup.defineHiddenClass(classBytes, true).lookupClass(); MethodHandle mh = lookup.findStatic(hiddenClass, "secretMethod", MethodType.methodType(void.class)); mh.invoke(); // ✅ Chalega but Class.forName() se nahi milegi! } } ``` Secret: Hidden classes reflection mein visible nahi hoti, par perfectly work karti hain!💀 --- Post 2: Java ka "Thread Local Handshakes" ka JVM level magic!🔥 ```java public class ThreadHandshake { static { // JVM internally sab threads ko pause kiye bina // single thread ko stop kar sakta hai! // Ye Java 9+ mein aaya for better profiling // -XX:ThreadLocalHandshakes=true } } ``` Internal Use Cases: · Stack sampling without stopping all threads · Lightweight performance monitoring · Better garbage collection Secret: JVM ab single thread ko individually manipulate kar sakta hai! 💡 --- Post 3: Java ka "Contended" annotation for false sharing prevention!🚀 ```java import jdk.internal.vm.annotation.Contended; public class FalseSharingFix { // Ye do variables different cache lines mein store honge! @Contended public long value1 = 0L; @Contended public long value2 = 0L; } ``` JVM Option Required: ``` -XX:-RestrictContended ``` Performance Impact: Multi-threaded apps mein 30-40%performance improvement! 💪 --- Post 4: Java ka "CDS Archives" - Application startup 10x faster!🔮 ```java // Kuch nahi karna - bas JVM options use karo: // Dump CDS archive: // -Xshare:dump -XX:SharedArchiveFile=app.jsa // Use CDS archive: // -Xshare:on -XX:SharedArchiveFile=app.jsa ``` Internal Magic: · Pre-loaded classes shared memory mein · Startup time dramatically kam · Memory footprint reduce Result: Spring Boot apps 3-4 seconds se 400-500ms startup!💀 ---
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
👍