0% found this document useful (0 votes)
26 views5 pages

Java Collections Basis 50 Question Answer

The document provides a comprehensive overview of the Java Collections Framework, detailing key interfaces such as Collection, List, Set, and Map, along with their characteristics and differences. It explains various collection types, their functionalities, and performance aspects, including thread safety and sorting mechanisms. Additionally, it covers specific classes like ArrayList, HashMap, and TreeSet, and discusses concepts like iterators, comparators, and the differences between arrays and ArrayLists.

Uploaded by

rajmayank1121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

Java Collections Basis 50 Question Answer

The document provides a comprehensive overview of the Java Collections Framework, detailing key interfaces such as Collection, List, Set, and Map, along with their characteristics and differences. It explains various collection types, their functionalities, and performance aspects, including thread safety and sorting mechanisms. Additionally, it covers specific classes like ArrayList, HashMap, and TreeSet, and discusses concepts like iterators, comparators, and the differences between arrays and ArrayLists.

Uploaded by

rajmayank1121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

1. What is the Java Collections Framework?

Answer: A unified architecture for storing and manipulating groups of objects. It


includes interfaces (List, Set, Map) and classes (ArrayList, HashMap, etc.).

2. What are the main interfaces in the Java Collections Framework?

Answer:

Collection

List

Set

Queue

Map (not part of Collection but still part of Collections Framework)

3. What is the difference between Collection and Collections?

Answer:

Collection is a root interface.

Collections is a utility class with static methods like sort(), reverse(), etc.

4. What is a List in Java?

Answer: An ordered collection that allows duplicates. Examples: ArrayList,


LinkedList.

5. What is a Set in Java?

Answer: A collection that does not allow duplicate elements. Examples: HashSet,
LinkedHashSet, TreeSet.

6. What is a Map in Java?

Answer: A key-value pair data structure. Keys are unique. Examples: HashMap,
TreeMap, LinkedHashMap.

7. Difference between ArrayList and LinkedList?

Answer:

ArrayList: Fast for random access, slow for insert/delete

LinkedList: Slow for access, fast for insert/delete

8. What is the difference between HashSet and TreeSet?

Answer:

HashSet: Unordered, faster

TreeSet: Sorted order, slower

9. What is the difference between HashMap and TreeMap?


Answer:

HashMap: Unordered

TreeMap: Sorted by keys

10. What is the initial capacity of ArrayList?

Answer: 10 (by default)

11. Can we store null in a Collection?

Answer:

Yes, most collections allow null (e.g., ArrayList, HashMap), but TreeMap and
TreeSet do not allow null as a key or element.

12. How to make a collection thread-safe?

Answer:

Use Collections.synchronizedList() or ConcurrentHashMap, etc.

13. What is Iterator in Java?

Answer: An object to iterate (traverse) elements in a collection using methods like


hasNext() and next().

14. What is the difference between Iterator and ListIterator?

Answer:

Iterator: Only forward traversal

ListIterator: Both forward and backward traversal (only for Lists)

15. What is fail-fast in Java Collections?

Answer: Iterator throws ConcurrentModificationException if collection is modified


while iterating.

16. What is fail-safe Iterator?

Answer: Doesn't throw exception if collection is modified. Example:


CopyOnWriteArrayList, ConcurrentHashMap.

17. What is the use of Collections.sort()?

Answer: To sort a List in natural or custom order.

18. What is Comparable in Java?

Answer: An interface for natural ordering of objects. Implement compareTo() method.

19. What is Comparator in Java?

Answer: An interface for custom sorting. Implement compare() method.


20. What is the difference between Comparable and Comparator?

Answer:

Comparable: Natural order, modifies class

Comparator: Custom order, separate class

21. What is the difference between HashMap and Hashtable?

Answer:

HashMap: Not synchronized, allows null

Hashtable: Synchronized, no null key/value

22. What is LinkedHashMap?

Answer: HashMap with insertion order maintained.

23. What is TreeMap?

Answer: A sorted map based on natural ordering or Comparator.

24. How do you sort a Map by keys or values?

Answer:

By keys: Use TreeMap

By values: Use a custom Comparator with List<Map.Entry<K,V>>

25. How to remove duplicates from a list in Java?

Answer:
Use Set:

List<String> list = new ArrayList<>(new HashSet<>(originalList));

26. What is PriorityQueue?

Answer: A queue that orders elements based on natural order or custom comparator.

27. Can a Map have duplicate keys?

Answer: No, keys must be unique.

28. Can a Map have duplicate values?

Answer: Yes.

29. What is EnumSet in Java?

Answer: A high-performance Set implementation for enum types only.

30. What is the difference between peek(), poll(), and remove() in Queue?

Answer:
peek(): Retrieves head without removing

poll(): Retrieves and removes head

remove(): Same as poll(), but throws exception if empty

31. What is a Deque?

Answer: A double-ended queue; elements can be inserted or removed from both ends.

32. What is Stack in Java?

Answer: A subclass of Vector that implements LIFO (Last-In-First-Out).

33. What is Vector in Java?

Answer: A synchronized, dynamic array. Legacy class, rarely used now.

34. What is CopyOnWriteArrayList?

Answer: A thread-safe version of ArrayList where updates create a new copy.

35. What is ConcurrentHashMap?

Answer: A thread-safe, high-performance Map that allows concurrent read and write
operations.

36. What is IdentityHashMap?

Answer: Uses reference equality (==) instead of .equals() for comparing keys.

37. What is WeakHashMap?

Answer: A Map where keys are weak references and can be garbage collected.

38. What is NavigableMap?

Answer: Extends SortedMap and provides navigation methods like lowerKey(),


higherKey().

39. What is the difference between Array and ArrayList?

Answer:

Array: Fixed size, supports primitives

ArrayList: Resizable, only objects

40. How to convert an array to a list in Java?

Answer:

List<String> list = Arrays.asList(array);

41. How to synchronize a List in Java?

Answer:

List<String> syncList = Collections.synchronizedList(new ArrayList<>());


42. How to sort a List of custom objects?

Answer:
Use Comparable or Comparator:

Collections.sort(list, Comparator.comparing(Employee::getSalary));

43. What is a singleton list?

Answer:

List<String> list = Collections.singletonList("only");

44. What is an unmodifiable collection?

Answer: A collection that cannot be changed:

List<String> list = Collections.unmodifiableList(originalList);

45. How to check if a collection is empty?

Answer:

if (collection.isEmpty()) { ... }

46. How to get the size of a collection?

Answer:

int size = collection.size();

47. What is the difference between remove() in List and Set?

Answer:

List: Removes by index or object

Set: Removes object only

48. How to iterate over a Map?

Answer:

for (Map.Entry<K, V> entry : map.entrySet()) {


System.out.println(entry.getKey() + ":" + entry.getValue());
}

49. What is the default load factor of HashMap?

Answer: 0.75

50. What is the time complexity of HashMap operations?

Answer:

Get/Put: O(1) average case

Worst case: O(n), if many hash collisions

You might also like