fbpx

Top 100 Java Collection Interview Questions and Answers

Top 100 Java Collection Interview Questions and Answers

Contents show

1. What is a Collection in Java?

Answer:

A Collection in Java is an object that represents a group of elements. It is a framework that provides classes and interfaces to store, manipulate, and retrieve objects.


2. Explain the difference between a Collection and a Collections in Java.

Answer:

  • Collection: It is an interface that represents a group of objects. It is a part of the Java Collections Framework.
  • Collections: It is a utility class that provides static methods for various operations, such as sorting, searching, etc., on collections.

3. What is the Java Collections Framework (JCF)?

Answer:

The Java Collections Framework is a set of classes and interfaces that provides the architecture for storing, organizing, and manipulating a group of objects.


4. What is an ArrayList in Java?

Answer:

An ArrayList is a dynamic array in Java. It implements the List interface and allows dynamic resizing of the array as elements are added or removed.

List<String> arrayList = new ArrayList<>();
arrayList.add("Element 1");
arrayList.add("Element 2");

5. What is a LinkedList in Java?

Answer:

A LinkedList is a data structure in Java that represents a list of elements. It implements both the List and Deque interfaces, providing versatility in operations.

List<String> linkedList = new LinkedList<>();
linkedList.add("Element 1");
linkedList.add("Element 2");

6. Explain the difference between ArrayList and LinkedList.

Answer:

  • ArrayList: Implements a dynamic array. Suitable for random access and iterating through elements.
  • LinkedList: Implements a doubly linked list. Suitable for frequent insertion and deletion operations.

7. What is a HashSet in Java?

Answer:

A HashSet is an implementation of the Set interface. It uses a hash table for storage and provides constant-time performance for basic operations.

Set<String> hashSet = new HashSet<>();
hashSet.add("Element 1");
hashSet.add("Element 2");

8. What is a TreeSet in Java?

Answer:

A TreeSet is an implementation of the SortedSet interface. It stores elements in sorted order and allows efficient retrieval of elements based on their natural ordering.

Set<String> treeSet = new TreeSet<>();
treeSet.add("Element 2");
treeSet.add("Element 1");

9. Explain the difference between HashSet and TreeSet.

Answer:

  • HashSet: Unordered collection. Provides constant-time performance for basic operations.
  • TreeSet: Sorted collection. Elements are stored in sorted order based on their natural ordering.

10. What is a HashMap in Java?

Answer:

A HashMap is an implementation of the Map interface. It uses a hash table to store key-value pairs and provides constant-time performance for basic operations.

Map<String, Integer> hashMap = new HashMap<>();
hashMap.put("Key 1", 1);
hashMap.put("Key 2", 2);

11. What is a TreeMap in Java?

Answer:

A TreeMap is an implementation of the SortedMap interface. It stores key-value pairs in sorted order based on their natural ordering or a custom comparator.

Map<String, Integer> treeMap = new TreeMap<>();
treeMap.put("Key 2", 2);
treeMap.put("Key 1", 1);

12. What is a LinkedHashMap in Java?

Answer:

A LinkedHashMap is an implementation of the Map interface that maintains the order of elements based on the order in which they were inserted.

Map<String, Integer> linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put("Key 1", 1);
linkedHashMap.put("Key 2", 2);

13. Explain the difference between HashMap and TreeMap.

Answer:

  • HashMap: Unordered map. Provides constant-time performance for basic operations.
  • TreeMap: Sorted map. Elements are stored in sorted order based on their natural ordering or a custom comparator.

14. What is a HashSet and how does it ensure uniqueness of elements?

Answer:

A HashSet ensures uniqueness of elements by using the hashCode() method and equals() method. It stores elements in buckets based on their hash code, and ensures that elements with the same hash code are further compared using the equals() method to ensure uniqueness.


15. What is the purpose of the hashCode() and equals() methods in Java?

Answer:

  • hashCode(): Returns a unique integer value for an object. It is used by hash-based collections like HashSet and HashMap to determine the bucket where an element should be stored.
  • equals(): Compares two objects for equality. It is used to ensure uniqueness of elements in hash-based collections.

16. Explain the difference between List, Set, and Map in Java.

Answer:

  • List: Ordered collection that allows duplicate elements.
  • Set: Unordered collection that does not allow duplicate elements.
  • Map: Collection of key-value pairs where each key is associated with exactly one value.

17. How does an Iterator work in Java?

Answer:

An Iterator is an interface that provides a way to access elements one by one in a collection. It has methods like hasNext() to check if there are more elements, and next() to retrieve the next element.

List<String> list = new ArrayList<>();
Iterator<String> iterator = list.iterator();
while(iterator.hasNext()) {
    String element = iterator.next();
    // Process element
}

18. What is the purpose of the Comparable interface in Java?

Answer:

The Comparable interface is used to define the natural ordering of elements in a class. It contains the compareTo() method, which is implemented to specify how objects of the class should be compared.

class MyClass implements Comparable<MyClass> {
    int value;
    // Other methods and attributes

    @Override
    public int compareTo(MyClass other) {
        return Integer.compare(this.value, other.value);
    }
}

19. What is the purpose of the Comparator interface in Java?

Answer:

The Comparator interface is used to define custom comparison logic for classes where the natural ordering is not applicable or needs to be overridden.

class MyComparator implements Comparator<MyClass> {
    @Override
    public int compare(MyClass obj1, MyClass obj2) {
        // Custom comparison logic
    }
}

20. How does the foreach loop work in Java?

Answer:

The foreach loop (enhanced for loop) provides a simplified way to iterate over collections or arrays. It automatically handles getting the next element and terminating the loop.

List<String> list = new ArrayList<>();
for(String element : list) {
    // Process element
}

21. Explain the difference between ArrayList and LinkedList in Java.

Answer:

  • ArrayList: Implements a dynamic array that can grow or shrink. Provides fast access to elements by index.
List<Integer> arrayList = new ArrayList<>();
  • LinkedList: Implements a doubly-linked list. Provides fast insertion and deletion at both ends.
List<Integer> linkedList = new LinkedList<>();

22. What is the purpose of the ListIterator interface in Java?

Answer:

The ListIterator interface extends the Iterator interface to allow bidirectional traversal of a list. It provides methods like previous() and hasPrevious() for backward iteration.

ListIterator<String> iterator = list.listIterator();
while(iterator.hasNext()) {
    String element = iterator.next();
    // Process element
}

23. Explain the purpose of the Queue interface in Java.

Answer:

The Queue interface represents a collection designed for holding elements prior to processing. It follows a FIFO (First-In-First-Out) order.

Queue<String> queue = new LinkedList<>();
queue.offer("Element 1");
queue.offer("Element 2");
String element = queue.poll(); // Retrieves and removes the head of the queue

24. What is a PriorityQueue in Java?

Answer:

PriorityQueue is an implementation of the Queue interface that provides elements in a priority order. Elements are ordered using their natural ordering or a custom Comparator.

PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
priorityQueue.offer(3);
priorityQueue.offer(1);
priorityQueue.offer(2);

25. Explain the purpose of the Deque interface in Java.

Answer:

The Deque interface represents a double-ended queue, allowing elements to be added or removed from both ends. It provides methods like addFirst(), addLast(), removeFirst(), and removeLast().

Deque<String> deque = new LinkedList<>();
deque.addFirst("First");
deque.addLast("Last");

26. What is the purpose of the HashSet class in Java?

Answer:

HashSet is an implementation of the Set interface that uses a hash table to store elements. It provides constant-time performance for basic operations.

Set<String> hashSet = new HashSet<>();
hashSet.add("Element 1");
hashSet.add("Element 2");

27. Explain the difference between HashSet and LinkedHashSet.

Answer:

  • HashSet: Unordered set that provides constant-time performance for basic operations.
  • LinkedHashSet: Maintains insertion order of elements, providing slightly slower performance compared to HashSet.

28. What is the purpose of the TreeSet class in Java?

Answer:

TreeSet is an implementation of the SortedSet interface. It stores elements in sorted order based on their natural ordering or a custom Comparator.

Set<Integer> treeSet = new TreeSet<>();
treeSet.add(3);
treeSet.add(1);
treeSet.add(2);

29. Explain the purpose of the NavigableSet interface in Java.

Answer:

The NavigableSet interface extends SortedSet to provide navigation methods like ceiling(), floor(), higher(), and lower() for finding elements.

NavigableSet<Integer> navigableSet = new TreeSet<>();
navigableSet.add(1);
navigableSet.add(2);
navigableSet.add(3);

30. What is the purpose of the EnumSet class in Java?

Answer:

EnumSet is a specialized implementation of the Set interface for use with enum types. It is more memory-efficient than other Set implementations for enums.

enum Days {MONDAY, TUESDAY, WEDNESDAY};
EnumSet<Days> days = EnumSet.allOf(Days.class);

31. Explain the purpose of the Map interface in Java.

Answer:

The Map interface represents a collection of key-value pairs, where each key is associated with exactly one value. It does not allow duplicate keys.

Map<String, Integer> map = new HashMap<>();
map.put("Key 1", 1);
map.put("Key 2", 2);

32. What is the difference between HashMap and HashTable in Java?

Answer:

  • HashMap: Allows null values and is not synchronized, making it faster in non-thread-safe scenarios.
Map<String, Integer> hashMap = new HashMap<>();
  • HashTable: Does not allow null values and is synchronized, making it thread-safe but potentially slower.
Hashtable<String, Integer> hashTable = new Hashtable<>();

33. Explain the purpose of the LinkedHashMap class in Java.

Answer:

LinkedHashMap extends HashMap to maintain the insertion order of elements. It provides predictable iteration order.

Map<String, Integer> linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put("Key 1", 1);
linkedHashMap.put("Key 2", 2);

34. What is a TreeMap in Java?

Answer:

TreeMap is an implementation of the SortedMap interface. It stores key-value pairs in a sorted order based on the keys’ natural ordering or a custom Comparator.

SortedMap<String, Integer> treeMap = new TreeMap<>();
treeMap.put("Key 3", 3);
treeMap.put("Key 1", 1);
treeMap.put("Key 2", 2);

35. Explain the purpose of the WeakHashMap class in Java.

Answer:

WeakHashMap is an implementation of the Map interface where the keys are weakly referenced. This allows keys to be garbage collected if there are no strong references to them.

Map<String, Integer> weakHashMap = new WeakHashMap<>();
weakHashMap.put("Key 1", 1);
weakHashMap.put("Key 2", 2);

36. What is a ConcurrentHashMap in Java?

Answer:

ConcurrentHashMap is a thread-safe implementation of the Map interface. It allows multiple threads to read and write concurrently without external synchronization.

ConcurrentMap<String, Integer> concurrentMap = new ConcurrentHashMap<>();
concurrentMap.put("Key 1", 1);
concurrentMap.put("Key 2", 2);

37. Explain the purpose of the IdentityHashMap class in Java.

Answer:

IdentityHashMap is an implementation of the Map interface that uses reference equality (==) to compare keys. It does not use the equals method.

Map<String, Integer> identityHashMap = new IdentityHashMap<>();
identityHashMap.put("Key 1", 1);
identityHashMap.put("Key 2", 2);

38. What is a Properties class in Java?

Answer:

Properties is a subclass of Hashtable and is used to maintain lists of values in which the key is a String and the value is also a String.

Properties properties = new Properties();
properties.setProperty("key1", "value1");
properties.setProperty("key2", "value2");

39. Explain the purpose of the EnumMap class in Java.

Answer:

EnumMap is a specialized implementation of the Map interface for use with enum keys. It is highly efficient and type-safe.

enum Colors {RED, GREEN, BLUE};
EnumMap<Colors, Integer> enumMap = new EnumMap<>(Colors.class);
enumMap.put(Colors.RED, 1);
enumMap.put(Colors.GREEN, 2);

40. What is the purpose of the Collections class in Java?

Answer:

The Collections class provides utility methods for working with collections. It includes methods like sort(), shuffle(), reverse(), and more.

List<Integer> list = new ArrayList<>();
Collections.addAll(list, 3, 1, 2);
Collections.sort(list);

41. What is the purpose of the Arrays class in Java?

Answer:

The Arrays class provides various utility methods for working with arrays. It includes methods like sort(), binarySearch(), equals(), and more.

int[] arr = {3, 1, 2};
Arrays.sort(arr);

42. How does the Arrays.asList() method work?

Answer:

Arrays.asList() converts an array into a fixed-size list. Any changes to the list will affect the original array.

String[] arr = {"apple", "banana", "cherry"};
List<String> list = Arrays.asList(arr);

43. What is the purpose of the ListIterator interface?

Answer:

The ListIterator interface provides a way to traverse a list bidirectionally and modify its elements. It extends Iterator and allows for element replacement.

List<String> list = new ArrayList<>();
ListIterator<String> iterator = list.listIterator();

44. Explain the purpose of the Queue interface in Java.

Answer:

The Queue interface represents a collection designed for holding elements prior to processing. It follows the FIFO (First-In-First-Out) principle.

Queue<String> queue = new LinkedList<>();
queue.add("Element 1");
queue.add("Element 2");

45. What is a PriorityQueue in Java?

Answer:

PriorityQueue is an implementation of the Queue interface that provides elements in a sorted order based on their natural ordering or a custom Comparator.

PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
priorityQueue.add(3);
priorityQueue.add(1);
priorityQueue.add(2);

46. Explain the purpose of the Deque interface in Java.

Answer:

The Deque interface represents a double-ended queue. It allows insertion and removal of elements from both ends.

Deque<String> deque = new LinkedList<>();
deque.addFirst("First Element");
deque.addLast("Last Element");

47. What is a LinkedList in Java?

Answer:

LinkedList is an implementation of the List interface that uses a doubly linked list to store elements. It allows for efficient insertion and deletion.

LinkedList<String> linkedList = new LinkedList<>();
linkedList.add("Element 1");
linkedList.add("Element 2");

48. Explain the purpose of the HashSet class in Java.

Answer:

HashSet is an implementation of the Set interface that uses a hash table to store elements. It does not allow duplicate elements.

Set<String> set = new HashSet<>();
set.add("Element 1");
set.add("Element 2");

49. What is a LinkedHashSet in Java?

Answer:

LinkedHashSet is an implementation of the Set interface that maintains the insertion order of elements.

Set<String> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add("Element 1");
linkedHashSet.add("Element 2");

50. Explain the purpose of the TreeSet class in Java.

Answer:

TreeSet is an implementation of the SortedSet interface. It stores elements in a sorted order based on the natural ordering or a custom Comparator.

SortedSet<String> treeSet = new TreeSet<>();
treeSet.add("Element 3");
treeSet.add("Element 1");
treeSet.add("Element 2");

61. What is the purpose of the Deque interface in Java?

Answer:

The Deque interface represents a double-ended queue, allowing elements to be added or removed from both ends. It extends the Queue interface.

Deque<String> deque = new LinkedList<>();

62. Explain the ArrayDeque class in Java.

Answer:

ArrayDeque is an implementation of the Deque interface. It uses a resizable array to store elements and provides efficient insertion and removal from both ends.

Deque<String> deque = new ArrayDeque<>();

63. What is the purpose of the PriorityQueue class in Java?

Answer:

PriorityQueue is an implementation of the Queue interface that stores elements based on their natural ordering or a custom Comparator.

PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();

64. Explain the HashSet class in Java.

Answer:

HashSet is an implementation of the Set interface that uses a hash table for storage. It provides constant-time performance for basic operations.

Set<String> set = new HashSet<>();

65. What is the EnumSet class in Java?

Answer:

EnumSet is a specialized implementation of the Set interface for use with enums. It provides high-performance set operations.

enum Days { MONDAY, TUESDAY, WEDNESDAY }
EnumSet<Days> daysSet = EnumSet.of(Days.MONDAY, Days.TUESDAY);

66. Explain the BitSet class in Java.

Answer:

BitSet is a class that implements a dynamic set of bits. It is used for efficient manipulation of bits and supports operations like AND, OR, XOR, etc.

BitSet bitSet = new BitSet(8);

67. What is the purpose of the WeakHashMap class in Java?

Answer:

WeakHashMap is an implementation of the Map interface where keys are weakly referenced. This allows the garbage collector to remove entries.

WeakHashMap<String, Integer> weakHashMap = new WeakHashMap<>();

68. Explain the IdentityHashMap class in Java.

Answer:

IdentityHashMap is an implementation of the Map interface that uses reference equality (==) to compare keys.

IdentityHashMap<String, Integer> identityHashMap = new IdentityHashMap<>();

69. What is the purpose of the Stack class in Java?

Answer:

Stack is a class that represents a last-in, first-out (LIFO) stack of objects. It extends the Vector class and provides stack-specific operations.

Stack<String> stack = new Stack<>();

70. Explain the Properties class in Java.

Answer:

Properties is a class used for reading configuration and property files. It extends Hashtable and is often used for storing key-value pairs.

Properties properties = new Properties();
properties.load(new FileReader("config.properties"));

71. What is the purpose of the LinkedHashSet class in Java?

Answer:

LinkedHashSet is an implementation of the Set interface that maintains the insertion order of elements. It combines the features of a HashSet and a linked list.

Set<String> set = new LinkedHashSet<>();

72. Explain the TreeSet class in Java.

Answer:

TreeSet is an implementation of the SortedSet interface. It stores elements in a sorted tree structure, allowing for efficient retrieval of elements in a sorted order.

SortedSet<Integer> sortedSet = new TreeSet<>();

73. What is the purpose of the ConcurrentHashMap class in Java?

Answer:

ConcurrentHashMap is a thread-safe implementation of the Map interface. It allows multiple threads to access the map concurrently without the need for external synchronization.

ConcurrentHashMap<String, Integer> concurrentMap = new ConcurrentHashMap<>();

74. Explain the CopyOnWriteArrayList class in Java.

Answer:

CopyOnWriteArrayList is a thread-safe implementation of the List interface. It creates a new copy of the underlying array whenever an element is modified.

List<String> list = new CopyOnWriteArrayList<>();

75. What is the purpose of the BlockingQueue interface in Java?

Answer:

BlockingQueue is an interface that represents a queue with blocking operations. It provides thread-safe operations for adding and removing elements.

BlockingQueue<String> queue = new LinkedBlockingQueue<>();

76. Explain the LinkedBlockingQueue class in Java.

Answer:

LinkedBlockingQueue is an implementation of the BlockingQueue interface. It uses a linked node structure to store elements, allowing for efficient thread-safe operations.

BlockingQueue<String> queue = new LinkedBlockingQueue<>();

77. What is the purpose of the ConcurrentLinkedQueue class in Java?

Answer:

ConcurrentLinkedQueue is a non-blocking implementation of the Queue interface. It uses lock-free algorithms for efficient thread-safe operations.

Queue<String> queue = new ConcurrentLinkedQueue<>();

78. Explain the PriorityBlockingQueue class in Java.

Answer:

PriorityBlockingQueue is an implementation of the BlockingQueue interface that stores elements based on their natural ordering or a custom Comparator.

BlockingQueue<Integer> priorityQueue = new PriorityBlockingQueue<>();

79. What is the purpose of the DelayQueue class in Java?

Answer:

DelayQueue is an implementation of the BlockingQueue interface that holds elements until their specified delay time has passed.

BlockingQueue<DelayedElement> delayQueue = new DelayQueue<>();

80. Explain the EnumMap class in Java.

Answer:

EnumMap is a specialized implementation of the Map interface for use with enums. It provides high-performance map operations.

enum Days { MONDAY, TUESDAY, WEDNESDAY }
EnumMap<Days, String> enumMap = new EnumMap<>(Days.class);

81. What is the purpose of the WeakHashMap class in Java?

Answer:

WeakHashMap is an implementation of the Map interface where the keys are stored as weak references. This means that if there are no strong references to a key, it may be garbage collected.

WeakHashMap<Key, Value> weakMap = new WeakHashMap<>();

82. Explain the IdentityHashMap class in Java.

Answer:

IdentityHashMap is an implementation of the Map interface that uses reference equality (==) in place of object equality (equals) when comparing keys.

IdentityHashMap<Key, Value> identityMap = new IdentityHashMap<>();

83. What is the purpose of the EnumSet class in Java?

Answer:

EnumSet is a specialized implementation of the Set interface for use with enums. It provides a high-performance set implementation for enum types.

EnumSet<Days> enumSet = EnumSet.of(Days.MONDAY, Days.TUESDAY);

84. Explain the BitSet class in Java.

Answer:

BitSet is a class that represents a set of bits. It provides methods to manipulate individual bits, making it useful for tasks like bit manipulation and operations.

BitSet bitSet = new BitSet();

85. What is the purpose of the Preferences class in Java?

Answer:

Preferences is a platform-independent way of storing user and system preferences. It allows applications to store and retrieve configuration data.

Preferences userPrefs = Preferences.userRoot();

86. Explain the Properties class in Java.

Answer:

Properties is a subclass of Hashtable and represents a persistent set of properties. It is commonly used for reading configuration files.

Properties properties = new Properties();

87. What is the purpose of the ServiceLoader class in Java?

Answer:

ServiceLoader is a simple, flexible facility for service providers to register implementations and for clients to find them.

ServiceLoader<IService> serviceLoader = ServiceLoader.load(IService.class);

88. Explain the WeakReference class in Java.

Answer:

WeakReference is a class that allows objects to be garbage collected when there are no strong references to them.

Object obj = new Object();
WeakReference<Object> weakRef = new WeakReference<>(obj);

89. What is the purpose of the SoftReference class in Java?

Answer:

SoftReference is a class that allows objects to be garbage collected when they are not strongly referenced and memory is low.

Object obj = new Object();
SoftReference<Object> softRef = new SoftReference<>(obj);

90. Explain the PhantomReference class in Java.

Answer:

PhantomReference is a class that allows objects to be garbage collected, but provides more flexibility than WeakReference or SoftReference in the cleanup process.

Object obj = new Object();
ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();
PhantomReference<Object> phantomRef = new PhantomReference<>(obj, referenceQueue);

91. Explain the purpose of the Deque interface in Java.

Answer:

The Deque interface in Java represents a double-ended queue, which allows elements to be added or removed from both ends. It extends the Queue interface and provides additional methods for operations at both ends.

Deque<String> deque = new LinkedList<>();

92. What is the difference between ArrayList and LinkedList?

Answer:

  • ArrayList is implemented as a dynamic array, while LinkedList is implemented as a doubly linked list.
  • ArrayList provides fast random access, but slow insertion and removal of elements, while LinkedList provides fast insertion and removal, but slower random access.
ArrayList<String> arrayList = new ArrayList<>();
LinkedList<String> linkedList = new LinkedList<>();

93. Explain the purpose of the BlockingQueue interface in Java.

Answer:

BlockingQueue is an interface that extends the Queue interface and provides additional methods for handling situations where the queue is full (for producers) or empty (for consumers).

BlockingQueue<String> queue = new LinkedBlockingQueue<>();

94. What is the purpose of the ConcurrentHashMap class in Java?

Answer:

ConcurrentHashMap is a thread-safe implementation of the Map interface. It allows multiple threads to access and modify the map concurrently without external synchronization.

ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();

95. Explain the purpose of the CopyOnWriteArrayList class in Java.

Answer:

CopyOnWriteArrayList is a thread-safe variant of ArrayList. It creates a new copy of the underlying array whenever an element is modified, ensuring that it doesn’t interfere with ongoing iterations.

CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();

96. What is the purpose of the ArrayDeque class in Java?

Answer:

ArrayDeque is a resizable array implementation of the Deque interface. It provides better performance compared to LinkedList in most cases due to cache locality.

ArrayDeque<String> deque = new ArrayDeque<>();

97. Explain the purpose of the NavigableMap interface in Java.

Answer:

NavigableMap is an extension of the SortedMap interface that provides navigation methods for retrieving entries based on their relationship to other entries in the map.

NavigableMap<String, Integer> map = new TreeMap<>();

98. What is the purpose of the PriorityQueue class in Java?

Answer:

PriorityQueue is an implementation of a priority queue based on a binary heap. It allows elements to be removed based on their priority.

PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();

99. Explain the purpose of the WeakHashSet class in Java.

Answer:

WeakHashSet is not a standard Java class. However, it could be a custom implementation of a set using WeakReference to allow elements to be garbage collected when not strongly referenced.

Set<WeakReference<Object>> weakHashSet = new HashSet<>();

100. What is the purpose of the LinkedBlockingQueue class in Java?

Answer:

LinkedBlockingQueue is an implementation of the BlockingQueue interface that uses a linked node approach to handle blocking when the queue is full or empty.

LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>();