In interviews, they usually ask us to solve problems like this using Java 8 Let’s solve them using Java 8 Streams 3. 𝐆𝐢𝐯𝐞𝐧 𝐚 𝐥𝐢𝐬𝐭 𝐨𝐟 𝐢𝐧𝐭𝐞𝐠𝐞𝐫𝐬, 𝐫𝐞𝐦𝐨𝐯𝐞 𝐝𝐮𝐩𝐥𝐢𝐜𝐚𝐭𝐞𝐬 𝐚𝐧𝐝 𝐬𝐨𝐫𝐭 𝐭𝐡𝐞𝐦 𝐢𝐧 𝐚𝐬𝐜𝐞𝐧𝐝𝐢𝐧𝐠 𝐨𝐫𝐝𝐞𝐫. 3️⃣ Remove duplicates & sort List<Integer> numbers = Arrays.asList(5, 3, 8, 3, 1, 5, 10); List<Integer> result = numbers.stream() .distinct() .sorted() .collect(Collectors.toList()); System.out.println(result); output [1, 3, 5, 8, 10] 4. 𝐅𝐫𝐨𝐦 𝐚 𝐥𝐢𝐬𝐭 𝐨𝐟 𝐧𝐚𝐦𝐞𝐬, 𝐟𝐢𝐧𝐝 𝐚𝐥𝐥 𝐧𝐚𝐦𝐞𝐬 𝐬𝐭𝐚𝐫𝐭𝐢𝐧𝐠 𝐰𝐢𝐭𝐡 𝐭𝐡𝐞 𝐥𝐞𝐭𝐭𝐞𝐫 '𝐀' (𝐜𝐚𝐬𝐞-𝐢𝐧𝐬𝐞𝐧𝐬𝐢𝐭𝐢𝐯𝐞). 4️⃣ Names starting with 'A' (case-insensitive) List<String> names = Arrays.asList("Anu", "john", "Arjun", "alex", "Bob"); List<String> result = names.stream() .filter(name -> name.toLowerCase().startsWith("a")) .collect(Collectors.toList()); System.out.println(result); output : [Anu, Arjun, alex] 5. 𝐆𝐢𝐯𝐞𝐧 𝐚 𝐥𝐢𝐬𝐭 𝐨𝐟 𝐢𝐧𝐭𝐞𝐠𝐞𝐫𝐬, 𝐬𝐤𝐢𝐩 𝐭𝐡𝐞 𝐟𝐢𝐫𝐬𝐭 𝟑 𝐞𝐥𝐞𝐦𝐞𝐧𝐭𝐬 𝐚𝐧𝐝 𝐜𝐨𝐥𝐥𝐞𝐜𝐭 𝐭𝐡𝐞 𝐫𝐞𝐬𝐭. 5️⃣ Skip first 3 elements List<Integer> numbers = Arrays.asList(10, 20, 30, 40, 50, 60); List<Integer> result = numbers.stream() .skip(3) .collect(Collectors.toList()); System.out.println(result); output : [40, 50, 60] 6. 𝐅𝐫𝐨𝐦 𝐚 𝐥𝐢𝐬𝐭 𝐨𝐟 𝐢𝐧𝐭𝐞𝐠𝐞𝐫𝐬, 𝐟𝐢𝐧𝐝 𝐭𝐡𝐞 𝐬𝐪𝐮𝐚𝐫𝐞 𝐨𝐟 𝐞𝐚𝐜𝐡 𝐧𝐮𝐦𝐛𝐞𝐫 𝐚𝐧𝐝 𝐜𝐨𝐥𝐥𝐞𝐜𝐭 𝐨𝐧𝐥𝐲 𝐭𝐡𝐨𝐬𝐞 𝐬𝐪𝐮𝐚𝐫𝐞𝐬 𝐠𝐫𝐞𝐚𝐭𝐞𝐫 𝐭𝐡𝐚𝐧 𝟓𝟎. 6️⃣ Square & filter > 50 List<Integer> numbers = Arrays.asList(2, 5, 8, 10); List<Integer> result = numbers.stream() .map(n -> n * n) .filter(n -> n > 50) .collect(Collectors.toList()); System.out.println(result); output: [64, 100] What we used here? ▪️ "distinct()" → removes duplicates ▪️ "sorted()" → sorts elements ▪️ "filter()" → applies condition ▪️ "skip()" → skips elements ▪️ "map()" → transforms data (applies a specific function to every item) Let’s continue this in the next post with more Java 8 interview questions you shouldn’t miss Have you faced similar questions? Drop your experience below 🗯️ Let’s learn together 🤝 #Java #Java8 #Streams #CodingInterview #JavaDeveloper #Programming #LearnBySharing #InterviewPrep
Java 8 Streams Interview Questions
More Relevant Posts
-
🎯 𝗝𝗮𝘃𝗮 𝗖𝗼𝗹𝗹𝗲𝗰𝘁𝗶𝗼𝗻𝘀 𝗶𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗾𝘂𝗲𝘀𝘁𝗶𝗼𝗻𝘀 𝘁𝗵𝗮𝘁 𝗸𝗲𝗲𝗽 𝘀𝗵𝗼𝘄𝗶𝗻𝗴 𝘂𝗽 — 𝘄𝗶𝘁𝗵 𝗿𝗲𝗮𝗹 𝗮𝗻𝘀𝘄𝗲𝗿𝘀 If you have a Java interview coming up, this is the one topic you cannot afford to skip. Collections — especially HashMap — appears in every Java interview at every level. Here are the questions that actually get asked, with answers that impress 👇 𝗤: 𝗛𝗼𝘄 𝗱𝗼𝗲𝘀 𝗛𝗮𝘀𝗵𝗠𝗮𝗽 𝘄𝗼𝗿𝗸 𝗶𝗻𝘁𝗲𝗿𝗻𝗮𝗹𝗹𝘆? 𝗜𝘁 𝘂𝘀𝗲𝘀𝗲𝘀 𝗮𝗻 𝗮𝗿𝗿𝗮𝘆 𝗼𝗳 𝗯𝘂𝗰𝗸𝗲𝘁𝘀. Flow: hashCode() → perturbation → bucket index → data stored. Since Java 8 — if a bucket exceeds 8 nodes, it converts to a Red-Black Tree. O(1) average. O(log n) worst case. Know this cold. 𝗤: 𝗪𝗵𝘆 𝗶𝘀 𝘁𝗵𝗲 𝗱𝗲𝗳𝗮𝘂𝗹𝘁 𝗹𝗼𝗮𝗱 𝗳𝗮𝗰𝘁𝗼𝗿 𝟬.𝟳𝟱? Sweet spot between memory and performance. When entries exceed capacity × 0.75, HashMap doubles and rehashes. Pro tip: pre-size your HashMap if you know the expected size. Avoid rehashing. 𝗤: 𝗪𝗵𝘆 𝗺𝘂𝘀𝘁 𝗛𝗮𝘀𝗵𝗠𝗮𝗽 𝗸𝗲𝘆𝘀 𝗯𝗲 𝗶𝗺𝗺𝘂𝘁𝗮𝗯𝗹𝗲? Mutate a key after putting it → hashCode changes → different bucket → get() returns null. The entry becomes permanently unreachable — this is a memory leak. This is why String and Integer are the best map keys. 𝗤: 𝗛𝗮𝘀𝗵𝗠𝗮𝗽 𝘃𝘀 𝗖𝗼𝗻𝗰𝘂𝗿𝗿𝗲𝗻𝘁𝗛𝗮𝘀𝗵𝗠𝗮𝗽 𝘃𝘀 𝗛𝗮𝘀𝗵𝘁𝗮𝗯𝗹𝗲? HashMap — not thread-safe, fastest single-threaded. Hashtable — locks entire map, legacy, never use it. ConcurrentHashMap — locks only the bucket. Lock-free reads. 10-100× faster than Hashtable. 𝗤: 𝗙𝗮𝗶𝗹-𝗳𝗮𝘀𝘁 𝘃𝘀 𝗳𝗮𝗶𝗹-𝘀𝗮𝗳𝗲 𝗶𝘁𝗲𝗿𝗮𝘁𝗼𝗿𝘀? Fail-fast (HashMap, ArrayList) — throws ConcurrentModificationException if modified during iteration. Fail-safe (ConcurrentHashMap, CopyOnWriteArrayList) — works on a snapshot, never throws. 𝗤: 𝗛𝗼𝘄 𝘁𝗼 𝗶𝗺𝗽𝗹𝗲𝗺𝗲𝗻𝘁 𝗮𝗻 𝗟𝗥𝗨 𝗖𝗮𝗰𝗵𝗲 𝗶𝗻 𝗝𝗮𝘃𝗮? LRU Cache = keeps the most recently used items, throws away the oldest ones when full. In Java - Extend LinkedHashMap with accessOrder=true. Override removeEldestEntry() to evict when size exceeds capacity. 10 lines of code. Interviewers love this answer. 𝗧𝗵𝗲 𝗳𝗼𝗹𝗹𝗼𝘄-𝘂𝗽 𝘁𝗵𝗲𝘆 𝗮𝗹𝘄𝗮𝘆𝘀 𝗮𝘀𝗸: "𝗪𝗵𝗮𝘁 𝗵𝗮𝗽𝗽𝗲𝗻𝘀 𝘄𝗵𝗲𝗻 𝘁𝘄𝗼 𝗸𝗲𝘆𝘀 𝗵𝗮𝘃𝗲 𝘁𝗵𝗲 𝘀𝗮𝗺𝗲 𝗵𝗮𝘀𝗵𝗖𝗼𝗱𝗲?" Hash collision → same bucket → linked list → equals() finds the exact match. This is why 𝗵𝗮𝘀𝗵𝗖𝗼𝗱𝗲() AND 𝗲𝗾𝘂𝗮𝗹𝘀() must both be correctly implemented. 👉 Follow Aman Mishra for more backend insights,content, and interview-focused tech breakdowns!🚀 𝗜'𝘃𝗲 𝗰𝗼𝘃𝗲𝗿𝗲𝗱 𝘁𝗵𝗶𝘀 𝗶𝗻 𝗱𝗲𝗽𝘁𝗵, 𝗚𝗖 𝘁𝘂𝗻𝗶𝗻𝗴, 𝗝𝗩𝗠 𝗳𝗹𝗮𝗴𝘀, 𝗮𝗻𝗱 𝗿𝗲𝗮𝗹 𝗶𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗤&𝗔𝘀 — 𝗶𝗻 𝗺𝘆 𝗝𝗮𝘃𝗮 𝗖𝗼𝗿𝗲 & 𝗔𝗱𝘃𝗮𝗻𝗰𝗲𝗱 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗠𝗮𝘀𝘁𝗲𝗿𝘆 𝗚𝘂𝗶𝗱𝗲.𝗢𝗳𝗳𝗲𝗿𝗶𝗻𝗴 𝟱𝟬% 𝗼𝗳𝗳 𝗳𝗼𝗿 𝗮 𝗹𝗶𝗺𝗶𝘁𝗲𝗱 𝘁𝗶𝗺𝗲! 👇 𝗚𝗲𝘁 𝘁𝗵𝗲 𝗴𝘂𝗶𝗱𝗲 𝗵𝗲𝗿𝗲: https://lnkd.in/gn3AG7Cm 𝗨𝘀𝗲 𝗰𝗼𝗱𝗲 𝗝𝗔𝗩𝗔𝟱𝟬
To view or add a comment, sign in
-
-
🎯 𝗝𝗮𝘃𝗮 𝗖𝗼𝗹𝗹𝗲𝗰𝘁𝗶𝗼𝗻𝘀 𝗶𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗾𝘂𝗲𝘀𝘁𝗶𝗼𝗻𝘀 𝘁𝗵𝗮𝘁 𝗸𝗲𝗲𝗽 𝘀𝗵𝗼𝘄𝗶𝗻𝗴 𝘂𝗽 — 𝘄𝗶𝘁𝗵 𝗿𝗲𝗮𝗹 𝗮𝗻𝘀𝘄𝗲𝗿𝘀 If you have a Java interview coming up, this is the one topic you cannot afford to skip. Collections — especially HashMap — appears in every Java interview at every level. Here are the questions that actually get asked, with answers that impress 👇 𝗤: 𝗛𝗼𝘄 𝗱𝗼𝗲𝘀 𝗛𝗮𝘀𝗵𝗠𝗮𝗽 𝘄𝗼𝗿𝗸 𝗶𝗻𝘁𝗲𝗿𝗻𝗮𝗹𝗹𝘆? 𝗜𝘁 𝘂𝘀𝗲𝘀𝗲𝘀 𝗮𝗻 𝗮𝗿𝗿𝗮𝘆 𝗼𝗳 𝗯𝘂𝗰𝗸𝗲𝘁𝘀. Flow: hashCode() → perturbation → bucket index → data stored. Since Java 8 — if a bucket exceeds 8 nodes, it converts to a Red-Black Tree. O(1) average. O(log n) worst case. Know this cold. 𝗤: 𝗪𝗵𝘆 𝗶𝘀 𝘁𝗵𝗲 𝗱𝗲𝗳𝗮𝘂𝗹𝘁 𝗹𝗼𝗮𝗱 𝗳𝗮𝗰𝘁𝗼𝗿 𝟬.𝟳𝟱? Sweet spot between memory and performance. When entries exceed capacity × 0.75, HashMap doubles and rehashes. Pro tip: pre-size your HashMap if you know the expected size. Avoid rehashing. 𝗤: 𝗪𝗵𝘆 𝗺𝘂𝘀𝘁 𝗛𝗮𝘀𝗵𝗠𝗮𝗽 𝗸𝗲𝘆𝘀 𝗯𝗲 𝗶𝗺𝗺𝘂𝘁𝗮𝗯𝗹𝗲? Mutate a key after putting it → hashCode changes → different bucket → get() returns null. The entry becomes permanently unreachable — this is a memory leak. This is why String and Integer are the best map keys. 𝗤: 𝗛𝗮𝘀𝗵𝗠𝗮𝗽 𝘃𝘀 𝗖𝗼𝗻𝗰𝘂𝗿𝗿𝗲𝗻𝘁𝗛𝗮𝘀𝗵𝗠𝗮𝗽 𝘃𝘀 𝗛𝗮𝘀𝗵𝘁𝗮𝗯𝗹𝗲? HashMap — not thread-safe, fastest single-threaded. Hashtable — locks entire map, legacy, never use it. ConcurrentHashMap — locks only the bucket. Lock-free reads. 10-100× faster than Hashtable. 𝗤: 𝗙𝗮𝗶𝗹-𝗳𝗮𝘀𝘁 𝘃𝘀 𝗳𝗮𝗶𝗹-𝘀𝗮𝗳𝗲 𝗶𝘁𝗲𝗿𝗮𝘁𝗼𝗿𝘀? Fail-fast (HashMap, ArrayList) — throws ConcurrentModificationException if modified during iteration. Fail-safe (ConcurrentHashMap, CopyOnWriteArrayList) — works on a snapshot, never throws. 𝗤: 𝗛𝗼𝘄 𝘁𝗼 𝗶𝗺𝗽𝗹𝗲𝗺𝗲𝗻𝘁 𝗮𝗻 𝗟𝗥𝗨 𝗖𝗮𝗰𝗵𝗲 𝗶𝗻 𝗝𝗮𝘃𝗮? LRU Cache = keeps the most recently used items, throws away the oldest ones when full. In Java - Extend LinkedHashMap with accessOrder=true. Override removeEldestEntry() to evict when size exceeds capacity. 10 lines of code. Interviewers love this answer. 𝗧𝗵𝗲 𝗳𝗼𝗹𝗹𝗼𝘄-𝘂𝗽 𝘁𝗵𝗲𝘆 𝗮𝗹𝘄𝗮𝘆𝘀 𝗮𝘀𝗸: "𝗪𝗵𝗮𝘁 𝗵𝗮𝗽𝗽𝗲𝗻𝘀 𝘄𝗵𝗲𝗻 𝘁𝘄𝗼 𝗸𝗲𝘆𝘀 𝗵𝗮𝘃𝗲 𝘁𝗵𝗲 𝘀𝗮𝗺𝗲 𝗵𝗮𝘀𝗵𝗖𝗼𝗱𝗲?" Hash collision → same bucket → linked list → equals() finds the exact match. This is why 𝗵𝗮𝘀𝗵𝗖𝗼𝗱𝗲() AND 𝗲𝗾𝘂𝗮𝗹𝘀() must both be correctly implemented. 👉 Follow Sabarish S for more backend insights,content, and interview-focused tech breakdowns!🚀
To view or add a comment, sign in
-
-
Let’s take one step at a time 💥 Java 8 interview Questions 7️⃣ 𝐆𝐢𝐯𝐞𝐧 𝐚 𝐬𝐞𝐧𝐭𝐞𝐧𝐜𝐞, 𝐬𝐩𝐥𝐢𝐭 𝐢𝐭 𝐢𝐧𝐭𝐨 𝐰𝐨𝐫𝐝𝐬 𝐚𝐧𝐝 𝐜𝐨𝐥𝐥𝐞𝐜𝐭 𝐢𝐧𝐭𝐨 𝐚 𝐥𝐢𝐬𝐭 import java.util.*; import java.util.stream.*; public class Demo { public static void main(String[] args) { String sentence = "Java is powerful and simple"; List<String> words = Arrays.stream(sentence.split(" ")) .collect(Collectors.toList()); System.out.println(words); } } output : [Java, is, powerful, and, simple] 8️⃣ 𝐅𝐫𝐨𝐦 𝐚 𝐥𝐢𝐬𝐭 𝐨𝐟 𝐬𝐭𝐫𝐢𝐧𝐠𝐬, 𝐠𝐫𝐨𝐮𝐩 𝐭𝐡𝐞𝐦 𝐛𝐲 𝐭𝐡𝐞𝐢𝐫 𝐥𝐞𝐧𝐠𝐭𝐡 import java.util.*; import java.util.stream.*; public class Demo { public static void main(String[] args) { List<String> names = Arrays.asList("Java", "Spring", "API", "Code"); Map<Integer, List<String>> result = names.stream() .collect(Collectors.groupingBy(String::length)); System.out.println(result); } } output : {3=[API], 4=[Java, Code], 6=[Spring]} 9️⃣ 𝐆𝐢𝐯𝐞𝐧 𝐚 𝐥𝐢𝐬𝐭 𝐨𝐟 𝐢𝐧𝐭𝐞𝐠𝐞𝐫𝐬, 𝐩𝐚𝐫𝐭𝐢𝐭𝐢𝐨𝐧 𝐢𝐧𝐭𝐨 𝐞𝐯𝐞𝐧 𝐚𝐧𝐝 𝐨𝐝𝐝 import java.util.*; import java.util.stream.*; public class Demo { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(10, 15, 20, 25, 30); Map<Boolean, List<Integer>> result = numbers.stream() .collect(Collectors.partitioningBy(n -> n % 2 == 0)); System.out.println(result); } } output: {true=[10,20,30], false=[15,25]} 🔟 𝐅𝐫𝐨𝐦 𝐚 𝐥𝐢𝐬𝐭 𝐨𝐟 𝐢𝐧𝐭𝐞𝐠𝐞𝐫𝐬, 𝐟𝐢𝐧𝐝 𝐬𝐮𝐦 𝐨𝐟 𝐧𝐮𝐦𝐛𝐞𝐫𝐬 𝐠𝐫𝐞𝐚𝐭𝐞𝐫 𝐭𝐡𝐚𝐧 𝟏𝟎𝟎 import java.util.*; import java.util.stream.*; public class Demo { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(50, 120, 200, 30); int sum = numbers.stream() .filter(n -> n > 100) .mapToInt(Integer::intValue) .sum(); System.out.println(sum); } } output : 320 💭 A small thought Just showing up daily and solving one problem like this is what builds real confidence over time 💬 Have you faced similar Java 8 questions in interviews? Let’s learn together 🤝 #Java #Java8 #JVM #OpenJDK #Streams #CodingInterview #JavaDeveloper #Programming #JavaCommunity #AI #Spring #JavaEnterprise #Microservices #JakartaEE #SpringBoot #Framework #LearnBySharing #BackendDeveloper #Coding #TechRevolution #TechCareer #SoftwareEngineering
To view or add a comment, sign in
-
Spring Boot Interview Series — Java — Post 21 Java has two types of data - primitives and non-primitives. Understanding the difference tells you how data is stored, accessed, and managed in memory. 8 Primitive Types byte, short, int, long, float, double, char, boolean Stored directly on the Stack. Fixed size. No methods. Cannot be null. Faster than objects - direct value access, no object overhead. Non-Primitives - Reference Types -> String - immutable. Stored in the String Pool on the Heap. Has methods. Can be null. -> Arrays - fixed size. Stored on the Heap. Can hold both primitives and reference types. -> Classes - objects created with new. Stored on the Heap. Can be null. -> Interfaces - reference types. Cannot be instantiated directly. Define a contract. Stack vs Heap -> Stack - stores primitive values and object references. Thread-specific, LIFO, fast. Automatically cleared when the method exits. int x = 10; // stored directly on stack -> Heap - stores all objects and arrays. Shared across threads. Managed by Garbage Collector. Slower than stack. BankAccount acc = new BankAccount(); // object on heap, reference on stack Wrapper Classes + Autoboxing Every primitive has a wrapper class- int → Integer, double → Double, boolean → Boolean, char → Character. Autoboxing - automatic conversion from primitive to wrapper. Unboxing - automatic conversion from wrapper to primitive. int x = 5; Integer y = x; // autoboxing int z = y; // unboxing Interview trap - Integer cache: Integer caches values from -128 to 127. Outside this range, == returns false because new objects are created. Integer.valueOf(127)
To view or add a comment, sign in
-
-
This tiny Java interview task looks strange, but it's a catch. Solve them all, and you're a giga-catch. Today I'm sharing code for reviewing. In the past examples I shared, I pointed out problems with using Java and Spring Boot. Today, I focus on problems in the business logic. Most of them are critical, so let's fight them. 1. Any user-input data should be validated, especially authentication data. Hackers will manipulate it to get access. You have to be paranoid here. 2. Logging sensitive data is a bad idea. If you want to do a hacker a favor, then log any user auth requests. In the event of a data leak, it'll help hackers collect many more credentials than ever. I have seen this two times in production code and immediately raised concerns and fixed it. 3. Creating an objectMapper in each call is a bad idea. ObjectMapper creation is not a cheap operation in terms of memory and CPU consumption, and you have to reuse it. You can get it from the Spring context or make your own copy in the service. That problem I saw while performing high-load testing and analyzing flame graphs - I noticed an intensive method where 70% of CPU time was spent creating an object mapper, nice, isn't it? 4. Code is loaded with different parts of the JWT token into memory and fetches the signature algorithm, but never validates the value inside it, and does not validate the signature at all, but it will be the next topic. 5. Dynamic deserialization - an easy way to deserialize the data because you don't need to write a class for JSON. Anyone could pass bad JSON to your service, and your service should deserialize it. Next, you should get the value from that a map object and pray it is in the expected type. Don't make your work harder - ask AI to generate a class for JSON. 6. JWT tokens contain different fields, but one of the important things is checking the expiration time. It helps to avoid problems when an attacker steals your token and logs in to a system. 7. The elephant in the room, hello mr. I'm not checking the JWT signature. A JWT has three parts: the header, the payload, and the signature. Anyone can try to manipulate the token's data, but the signature protects it from tampering. Only the private key owner can sign a token. On the receiver's side, you have to verify it using the public key. If the signature mismatches, the data has been changed. You always have to verify the signature to ensure it was issued by a trusted issuer. 8. Finally, because the signature wasn't checked, we are passing the subject to the query to check if the user is present. If so, we authenticate a user, regardless of whether they have the necessary rights. In other words, if you rely only on user input, you're opening the door to hell. I reached the post size limit many times. Some of the problems in the screenshot aren't described, but I know you can handle them on your own.
To view or add a comment, sign in
-
-
🙃 Java Interview Questions – Answers Explained Thank you for the great responses on my previous post! Here are the answers to the Java interview questions 👇 --- 🔹 Q1: What are the 4 pillars of OOP? Encapsulation, Abstraction, Inheritance, Polymorphism --- 🔹 Q2: Encapsulation vs Abstraction? Encapsulation → Hides data (security) Abstraction → Hides implementation (complexity) --- 🔹 Q3: Why no multiple inheritance in Java? Java doesn’t support multiple inheritance using classes due to ambiguity (diamond problem). It is achieved using interfaces. --- 🔹 Q4: Overloading vs Overriding? Overloading → Compile-time, same method name with different parameters Overriding → Runtime, child class provides its own implementation --- 🔹 Q5: List vs Set vs Map? List → Ordered, duplicates allowed Set → No duplicates Map → Key-value pairs --- 🔹 Q6: Why is HashMap fast? Uses hashing to directly access bucket → O(1) time complexity --- 🔹 Q7: hashCode() vs equals()? hashCode() → Finds bucket location equals() → Compares objects --- 🔹 Q8: What is collision? When two keys have same hashCode → stored in same bucket Handled using LinkedList (Java 7) or Tree (Java 8) --- 🔹 Q9: Why ArrayList slow for insertion? Requires shifting elements and resizing --- 🔹 Q10: Why String is immutable? For security, performance (string pool), and thread safety --- 🔹 Q11: Checked vs Unchecked exceptions? Checked → Compile-time (IOException) Unchecked → Runtime (NullPointerException) --- 🔹 Q12: Will finally always execute? No — it won’t execute if JVM stops (e.g., System.exit()) --- 🔹 Q13: What is race condition? When multiple threads modify shared data → inconsistent results --- 🔹 Q14: start() vs run()? start() → creates new thread run() → normal method call --- 🔹 Q15: Lambda expression? Short way to implement functional interface --- 🔹 Q16: map() vs filter()? map() → transforms data filter() → applies condition --- 💡 Consistently revising fundamentals to become interview-ready 🚀 #Java #JavaDeveloper #InterviewPrep #LearningInPublic #SoftwareEngineering #TechCareers
To view or add a comment, sign in
-
Real Java interviews are not about syntax — they are about how you handle production issues. 1. Your application throws OutOfMemoryError after running for a few hours. How will you identify and fix the root cause? 2. You start getting StackOverflowError in production. What could cause it and how will you debug it? 3. Your application shows memory growth over time. How will you detect and fix memory leaks? 4. Multiple threads are updating shared data causing inconsistent results. How will you handle concurrency? 5. Your application faces deadlock between threads. How will you detect and resolve it? 6. You observe high CPU usage but low request throughput. How will you debug it? 7. A thread pool gets exhausted under load. How will you fix and tune it? 8. Your application throws ConcurrentModificationException. How will you resolve it? 9. You see NullPointerException in production but not locally. How will you debug it? 10. Your application becomes slow due to excessive object creation. How will you optimize it? 11. You get ClassNotFoundException or NoClassDefFoundError in production. What could be the issue? 12. A scheduled task runs multiple times unexpectedly. How will you fix it? 13. Your application hangs due to blocked threads. How will you identify the issue? 14. You face race conditions in a critical section. How will you prevent them? 15. Your application throws InterruptedException frequently. How will you handle thread interruptions properly? 16. You see high garbage collection pauses affecting performance. How will you optimize GC? 17. Your application fails due to improper synchronization. How will you fix thread safety? 18. A future or async task never completes. How will you debug it? 19. Your application throws IllegalStateException due to incorrect state handling. How will you fix it? 20. You observe thread starvation in your application. How will you resolve it? 21. A service crashes due to unhandled exceptions. How will you design proper exception handling? 22. Your application shows inconsistent results due to caching issues. How will you fix it? 23. You face issues with serialization/deserialization in Java. How will you debug it? 24. A critical process fails silently without logs. How will you improve observability? 25. Your application behaves differently in production vs local environment. How will you approach debugging? If you can confidently solve these, you are thinking like a production-level Java developer. For detailed Questions and answers and real-world explanations, comment your experience below.
To view or add a comment, sign in
-
Java Interview Topic: equals() and hashCode() In Java, equals() and hashCode() are two very important methods, especially when working with collections like HashMap, HashSet, and Hashtable. By default, equals() checks whether two object references point to the same memory location. But in real-world applications, we usually want to compare objects based on their data. Example: class Employee { int id; String name; } Now imagine two Employee objects: Employee e1 = new Employee(1, "Ram"); Employee e2 = new Employee(1, "Ram"); Logically, both employees are the same because their id and name are the same. But without overriding equals(), Java may treat them as different objects. That is why we override equals(). But why hashCode()? Because hash-based collections like HashMap and HashSet first use hashCode() to decide where to store or find the object. Important rule: If two objects are equal according to equals(), they must have the same hashCode(). But if two objects have the same hashCode(), they are not always equal. So whenever you override equals(), you should also override hashCode(). Simple formula to remember: equals() → checks object equality hashCode() → helps in faster searching inside hash-based collections This is one of the most commonly asked Java interview topics, but it is also very important in real-world development. Understanding this concept helps you avoid bugs in HashMap, HashSet, and object comparison logic. #Java #CoreJava #JavaDeveloper #BackendDevelopment #SpringBoot #Programming #SoftwareDevelopment #Coding #InterviewPreparation #HashMap #ObjectOrientedProgramming #100DaysOfCode
To view or add a comment, sign in
-
-
Java Strings — Part 2: String Constant Pool (SCP) Deep Dive 🔥 If you don’t understand String Pool, you’ll struggle with at least 30% of Java interview questions on Strings. Let’s break it down clearly 👇 ⸻ 🔹 What is String Constant Pool (SCP)? 👉 A special memory area inside the Heap where Java stores unique String literals String s1 = "abc"; String s2 = "abc"; 💡 Both s1 and s2 point to the same object in the String Pool ⸻ 🔹 Why String Pool exists? ✔ Memory optimization (no duplicate objects) ✔ Faster comparisons using references ✔ Improves performance ⸻ 🔹 Object Creation — Most Important Concept 🔥 ✅ Case 1: Using Literal String s1 = "hello"; String s2 = "hello"; 👉 Only 1 object created in SCP ⸻ ✅ Case 2: Using new keyword String s1 = new String("hello"); 👉 2 objects created: 1. One in SCP (“hello”) 2. One in Heap (new object) ⸻ ✅ Case 3: Combination String s1 = "hello"; String s2 = new String("hello"); 👉 Total objects: * 1 in SCP * 1 in Heap ⸻ 🔹 == vs .equals() (Interview Trap ⚠️) String s1 = "abc"; String s2 = "abc"; System.out.println(s1 == s2); // true String s3 = new String("abc"); System.out.println(s1 == s3); // false ✔ == → compares references ✔ .equals() → compares values ⸻ 🔹 intern() Method (Advanced 🔥) String s1 = new String("hello"); String s2 = s1.intern(); 👉 intern() forces the String into SCP 💡 Now s2 will point to SCP reference ⸻ 🔹 Tricky Interview Questions ⚠️ Q1: How many objects? String s = new String("xyz"); 👉 Answer: 2 objects ⸻ ⚠️ Q2: How many objects? String s1 = "a" + "b"; 👉 Answer: 1 object (compile-time optimization) ⸻ ⚠️ Q3: How many objects? String s1 = "a"; String s2 = s1 + "b"; 👉 Answer: 2 objects (runtime concatenation) ⸻ 🔹 Compile-Time vs Runtime Concatenation ✔ Compile-time → goes to SCP ✔ Runtime → creates new object in Heap ⸻ 🔥 Final Takeaway ✔ String Pool avoids duplicate objects ✔ Literals use SCP, new creates extra object ✔ == vs .equals() is a must-know ✔ intern() is an advanced optimization tool #Java #SDET #AutomationTesting #JavaInterview #StringPool #Programming #TechLearning :::
To view or add a comment, sign in
-
Java basic Interview Questions solved with Advance Java Q1. Reverse a String ? //Reverse the String String reverseString = "Java Programming"; System.out.println(new StringBuilder(reverseString).reverse()); Q2. Find Duplicate Characters --> Input : “automation” --> Output: “a-2 t-2 o-2" //Find duplicate Character String duplicate = "automation"; getMapForOccurance(duplicate).entrySet().stream().filter(e -> e.getValue() > 1) .forEach(e -> System.out.println(e.getKey() + "-" + e.getValue())); public static Map < Character, Integer > getMapForOccurance(String abc) { Map < Character, Integer > map = new HashMap < Character, Integer > (); for (char c: abc.toCharArray()) { map.put(c, map.getOrDefault(c, 0) + 1); } return map; } Q3. Count Occurrence of Each Character Input : “test” Output : “ t=2, e=1, s=1” // Count occurrence of Each Character String occurrence = "test"; getMapForOccurance(occurrence).forEach((key, value) -> { System.out.println(key + "-" + value); }); Q4: Palindrome Check < Verify with enter list of numbers > “madam” -> true “Hello” -> false //Palindrom check Scanner sc = new Scanner(System.in); boolean flag = true; List < String > palindrom = new ArrayList < String > (); System.out.println("Enter all the strings to verify Palindrom./n type \"last\" for end the input"); while (flag) { String input = sc.nextLine(); if (input.equalsIgnoreCase("last")) { break; } palindrom.add(input); } palindrom.forEach((pal) -> { if (pal.equals(new StringBuilder(pal).reverse().toString())) { System.out.println(pal + " is Palindrom"); } }); Q5. Find Largest & Smallest in Array // Find largest & Smallest Array int[] values={5,2,9,1}; System.out.println("Largest Number :"+ Arrays.stream(values).max().getAsInt()); System.out.println("Smallest Number :"+ Arrays.stream(values).min().getAsInt()); Q6. Find Largest & Smallest in Collection // Find largest & Smallest Collections List<Integer> values=List.of(5,2,9,1); System.out.println("Largest Number :"+ Collections.max(values)); System.out.println("Smallest Number :"+ Collections.min(values)); Q7. Remove Duplicates from Array //Remove Duplicates from ArrayList int[] input = {1,2,2,3,4,4}; // Output : [1,2,3,4] Set<Integer> set= Arrays.stream(input).boxed() .collect(Collectors.toSet()); System.out.println(set); #AdvanceJava #Java #JavaInterviewQuestioins #QA #AutomationTesting
To view or add a comment, sign in
Explore related topics
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