Java Collections Framework

The Java Collections Framework provides a set of classes and interfaces for storing and manipulating groups of data. Understanding collections such as List, Set, and Map is essential for effective Java programming.

For a detailed overview of the Java Collections Framework, visit our article: Java Collections Framework.

Questions and Answers on Collections in Java

1. What is a collection in Java?

A collection in Java is a framework that provides an architecture to store and manipulate a group of objects.

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

The main interfaces are Collection, List, Set, Map, and Queue.

3. What is the difference between a List and a Set?

A List allows duplicate elements and maintains the order of insertion, while a Set does not allow duplicates and does not guarantee order.

4. What is the ArrayList class?

ArrayList is a resizable array implementation of the List interface, allowing dynamic arrays that can grow as needed.

List<String> list = new ArrayList<>();
list.add("A");
list.add("B");

5. What is the LinkedList class?

LinkedList is a doubly-linked list implementation of the List interface, allowing for efficient insertions and deletions.

List<String> linkedList = new LinkedList<>();
linkedList.add("A");
linkedList.add("B");

6. What is the HashSet class?

HashSet is an implementation of the Set interface that uses a hash table for storage, allowing for fast access and no duplicate elements.

Set<String> set = new HashSet<>();
set.add("A");
set.add("B");

7. What is the TreeSet class?

TreeSet is a sorted set implementation that uses a red-black tree, maintaining elements in their natural order or according to a specified comparator.

Set<String> treeSet = new TreeSet<>();
treeSet.add("B");
treeSet.add("A");

8. What is the HashMap class?

HashMap is an implementation of the Map interface that uses a hash table for storage, allowing for fast retrieval of key-value pairs.

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

9. What is the TreeMap class?

TreeMap is a sorted map implementation that uses a red-black tree, maintaining keys in their natural order or according to a specified comparator.

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

10. What is the LinkedHashMap class?

LinkedHashMap is an implementation of the Map interface that maintains a linked list of the entries, preserving the order of insertion.

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

11. What is the Queue interface?

The Queue interface represents a collection designed for holding elements prior to processing, typically in a FIFO (first-in-first-out) order.

12. What is the PriorityQueue class?

PriorityQueue is an implementation of the Queue interface that orders elements based on their natural ordering or a specified comparator.

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

13. What is the Deque interface?

The Deque interface represents a double-ended queue that allows elements to be added or removed from both ends.

14. What is the ArrayDeque class?

ArrayDeque is a resizable array implementation of the Deque interface, allowing for efficient insertion and removal of elements from both ends.

Deque<String> arrayDeque = new ArrayDeque<>();
arrayDeque.add("A");
arrayDeque.addFirst("B");

15. What is the Collections class?

The Collections class provides static methods for operating on collections, such as sorting and searching.

16. How do you sort a list using the Collections.sort() method?

You can sort a list using the Collections.sort() method, which sorts the list in natural order.

List<String> list = Arrays.asList("B", "A", "C");
Collections.sort(list);

17. How do you reverse a list using the Collections.reverse() method?

You can reverse a list using the Collections.reverse() method.

Collections.reverse(list);

18. What is the Comparator interface?

The Comparator interface is used to define a custom ordering for objects, allowing for sorting based on specific criteria.

19. How do you implement a custom comparator?

You can implement a custom comparator by creating a class that implements the Comparator interface.

class MyComparator implements Comparator<String> {
    public int compare(String a, String b) {
        return a.length() - b.length(); // Compare by length
    }
}

20. How do you use a comparator to sort a list?

You can use a comparator to sort a list by passing it to the Collections.sort() method.

Collections.sort(list, new MyComparator());

21. What is the ListIterator interface?

The ListIterator interface allows for iterating over a list in both forward and backward directions.

22. How do you create a ListIterator?

You can create a ListIterator by calling the list.listIterator() method on a list.

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

23. What is the Map.Entry interface?

The Map.Entry interface represents a key-value pair in a map.

24. How do you iterate over a map using entrySet()?

You can iterate over a map using the entrySet() method to get a set of entries.

for (Map.Entry<String, Integer> entry : map.entrySet()) {
    System.out.println(entry.getKey() + ": " + entry.getValue());
}

25. What is the Collections.unmodifiableList() method?

The Collections.unmodifiableList() method returns an unmodifiable view of the specified list.

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

26. What is the Collections.synchronizedList() method?

The Collections.synchronizedList() method returns a synchronized (thread-safe) list backed by the specified list.

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

27. What is the Collections.sort() method used for?

The Collections.sort() method is used to sort a list in natural order or according to a specified comparator.

28. What is the Collections.shuffle() method?

The Collections.shuffle() method randomly permutes the elements in a list.

Collections.shuffle(list);

29. What is the Collections.binarySearch() method?

The Collections.binarySearch() method searches for a specified element in a sorted list using the binary search algorithm.

int index = Collections.binarySearch(list, "B");

30. What is the Collections.fill() method?

The Collections.fill() method replaces all elements in a list with the specified element.

Collections.fill(list, "X");

31. What is the Collections.copy() method?

The Collections.copy() method copies elements from one list to another.

Collections.copy(destinationList, sourceList);

32. What is the Collections.indexOfSubList() method?

The Collections.indexOfSubList() method returns the starting position of the first occurrence of a specified sublist in a list.

int index = Collections.indexOfSubList(list, subList);

33. What is the Collections.rotate() method?

The Collections.rotate() method rotates the elements in a list by a specified distance.

Collections.rotate(list, 2);

34. What is the Collections.swap() method?

The Collections.swap() method swaps the elements at the specified positions in a list.

Collections.swap(list, 0, 1);

35. What is the List interface?

The List interface is an ordered collection that allows duplicate elements and provides positional access to elements.

36. What is the Set interface?

The Set interface is a collection that does not allow duplicate elements and does not guarantee the order of elements.

37. What is the Map interface?

The Map interface is a collection that maps keys to values, allowing for unique keys and associated values.

38. What is the Queue interface?

The Queue interface represents a collection designed for holding elements prior to processing, typically in a FIFO (first-in-first-out) order.

39. What is the Deque interface?

The Deque interface represents a double-ended queue that allows elements to be added or removed from both ends.

40. What is the Collections.unmodifiableSet() method?

The Collections.unmodifiableSet() method returns an unmodifiable view of the specified set.

Set<String> unmodifiableSet = Collections.unmodifiableSet(set);

41. What is the Collections.synchronizedSet() method?

The Collections.synchronizedSet() method returns a synchronized (thread-safe) set backed by the specified set.

Set<String> synchronizedSet = Collections.synchronizedSet(new HashSet<>());

42. What is the Collections.unmodifiableMap() method?

The Collections.unmodifiableMap() method returns an unmodifiable view of the specified map.

Map<String, Integer> unmodifiableMap = Collections.unmodifiableMap(map);

43. What is the Collections.synchronizedMap() method?

The Collections.synchronizedMap() method returns a synchronized (thread-safe) map backed by the specified map.

Map<String, Integer> synchronizedMap = Collections.synchronizedMap(new HashMap<>());

44. What is the Collections.nCopies() method?

The Collections.nCopies() method returns an immutable list consisting of n copies of the specified object.

List<String> copies = Collections.nCopies(3, "A");

45. What is the Collections.singletonList() method?

The Collections.singletonList() method returns an immutable list containing only the specified object.

List<String> singleList = Collections.singletonList("A");

46. What is the Collections.singletonMap() method?

The Collections.singletonMap() method returns an immutable map containing a single key-value pair.

Map<String, Integer> singleMap = Collections.singletonMap("A", 1);

47. What is the Collections.disjoint() method?

The Collections.disjoint() method checks if two collections have no elements in common.

boolean disjoint = Collections.disjoint(list1, list2);

48. What is the Collections.rotate() method?

The Collections.rotate() method rotates the elements in a list by a specified distance.

Collections.rotate(list, 2);

49. What is the Collections.swap() method?

The Collections.swap() method swaps the elements at the specified positions in a list.

Collections.swap(list, 0, 1);

50. What is the Collections.frequency() method?

The Collections.frequency() method returns the number of occurrences of a specified element in a collection.

int frequency = Collections.frequency(list, "A");
Go Back Home!!