Mostly Asked Java Collections Interview Questions
Q1. What is the Java Collection Framework? What is the Java Collection Framework?
The Java Collection Framework is a set of interfaces and classes that handle groups of objects. It includes List, Set, Map, Queue, etc., and provides ready-made algorithms for sorting, searching, etc.
Java Collection Framework इंटरफेस और क्लासेस का एक सेट है जो ऑब्जेक्ट्स के ग्रुप को मैनेज करने में मदद करता है। इसमें List, Set, Map, Queue आदि शामिल हैं और यह सॉर्टिंग, सर्चिंग जैसे एल्गोरिदम भी प्रदान करता है।
Q2. Difference between List, Set, and Map? Difference between List, Set, and Map?
List allows duplicates and maintains insertion order. Set doesn’t allow duplicates. Map stores key-value pairs and keys must be unique.
List डुप्लिकेट्स को अनुमति देता है और insertion order बनाए रखता है। Set डुप्लिकेट्स को अनुमति नहीं देता। Map key-value pair को स्टोर करता है और keys यूनिक होती हैं।
Q3. Iterating through a List of Employee objects Iterating through a List of Employee objects
You can use for-each loop, iterator, or stream API.
आप for-each loop, iterator या stream API का उपयोग कर सकते हैं।
Listlist = new ArrayList<>(); for (Employee e : list) { System.out.println(e.getName()); }
Q4. Sorting Employee list by name Sorting Employee list by name
Use Collections.sort with a custom Comparator or use List.sort().
Collections.sort के साथ custom Comparator या List.sort() का उपयोग करें।
list.sort(Comparator.comparing(Employee::getName));
Q5. What is HashMap? What is HashMap?
HashMap is a class that implements the Map interface. It stores key-value pairs and uses hashCode() to store/retrieve data efficiently.
HashMap एक क्लास है जो Map इंटरफेस को इम्प्लीमेंट करता है। यह key-value pairs को स्टोर करता है और hashCode() का उपयोग करता है।
Q6. Can HashMap have null keys and values? Can HashMap have null keys and values?
Yes. HashMap allows one null key and multiple null values.
हाँ, HashMap एक null key और कई null values को अनुमति देता है।
Q7. Difference between ArrayList and LinkedList? Difference between ArrayList and LinkedList?
ArrayList is backed by an array, so it's faster for indexing. LinkedList uses nodes, so it's better for frequent insertions/deletions.
ArrayList एक array से जुड़ा होता है, इसलिए indexing के लिए तेज होता है। LinkedList nodes का उपयोग करता है, इसलिए frequent insertions/deletions के लिए बेहतर होता है।
Q8. How to remove duplicates from a list? How to remove duplicates from a list?
You can use a Set to remove duplicates: Set
डुप्लिकेट हटाने के लिए Set का उपयोग करें: Set
Q9. Thread-safe alternative to HashMap? Thread-safe alternative to HashMap?
Use ConcurrentHashMap for thread-safe operations.
Thread-safe operations के लिए ConcurrentHashMap का उपयोग करें।
Q10. Java code to group Employee by department Java code to group Employee by department
Use Java 8 streams: list.stream().collect(Collectors.groupingBy(Employee::getDepartment));
Java 8 streams का उपयोग करें: list.stream().collect(Collectors.groupingBy(Employee::getDepartment));
Map> grouped = list.stream().collect(Collectors.groupingBy(Employee::getDepartment));