Java Collections Interview Questions
Java Collections Interview Questions
Java Collections Interview Questions
In Java, collection interview questions are most asked by the interviewers. Here is the
list of the most asked collections interview questions with answers.
o Arrays are always of fixed size, i.e., a user can not increase or decrease the
length of the array according to their requirement or at runtime, but In
Collection, size can be changed dynamically as per need.
o Arrays can only store homogeneous or similar type objects, but in Collection,
heterogeneous objects can be stored.
o Arrays cannot provide the ?ready-made? methods for user requirements as
sorting, searching, etc. but Collection includes readymade methods to use.
1|Page
Queue interface: Queue (java.util.Queue) interface defines queue data structure,
which stores the elements in the form FIFO (first in first out).
Syntax:
public interface Queue<E> extends Collection<E>
4. Dequeue interface: it is a double-ended-queue. It allows the insertion and
removal of elements from both ends. It implants the properties of both Stack and
queue so it can perform LIFO (Last in first out) stack and FIFO (first in first out) queue,
operations.
Syntax:
public interface Dequeue<E> extends Queue<E>
5. Map interface: A Map (java.util.Map) represents a key, value pair storage of
elements. Map interface does not implement the Collection interface. It can only
contain a unique key but can have duplicate elements. There are two interfaces which
implement Map in java that are Map interface and Sorted Map.
2|Page
The Iterator traverses the elements ListIterator traverses the elements in
1)
in the forward direction only. backward and forward directions both.
The Iterator can be used in List, Set,
2) ListIterator can be used in List only.
and Queue.
The Iterator can only perform ListIterator can perform ?add,? ?remove,?
3) remove operation while traversing and ?set? operation while traversing the
the collection. collection.
o The List can contain duplicate elements whereas Set includes unique items.
o The List is an ordered collection which maintains the insertion order whereas
Set is an unordered collection which does not preserve the insertion order.
o The List interface contains a single legacy class which is Vector class whereas
Set interface does not have any legacy class.
o The List interface can allow n number of null values whereas Set interface only
allows a single null value.
o HashSet contains only values whereas HashMap includes the entry (key,
value). HashSet can be iterated, but HashMap needs to convert into Set to be
iterated.
o HashSet implements Set interface whereas HashMap implements the Map
interface
o HashSet cannot have any duplicate value whereas HashMap can contain
duplicate values with unique keys.
o HashSet contains the only single number of null value whereas HashMap can
hold a single null key with n number of null values.
4|Page
14) What is the difference between Collection and Collections?
The differences between the Collection and Collections are given below.
1. import java.util.*;
2. import java.io.*;
3. public class Test {
4. public static void main(String[] args)throws Exception{
5. FileReader reader=new FileReader("db.properties");
6.
5|Page
7. Properties p=new Properties();
8. p.load(reader);
9.
10. System.out.println(p.getProperty("user"));
11. System.out.println(p.getProperty("password"));
12. }
13. }
Output
system
oracle
6|Page
o Generic confirms the stability of the code by making it bug detectable at
compile time.
o Separate Chaining
o Open Addressing
o Using HashSet: By using HashSet we can remove the duplicate element from
the ArrayList, but it will not then preserve the insertion order.
o Using LinkedHashSet: We can also maintain the insertion order by using
LinkedHashSet instead of HashSet.
The Process to remove duplicate elements from ArrayList using the LinkedHashSet:
8|Page
31) How to reverse ArrayList?
To reverse an ArrayList, we can use reverse() method of Collections class. Consider
the following example.
1. import java.util.ArrayList;
2. import java.util.Collection;
3. import java.util.Collections;
4. import java.util.Iterator;
5. import java.util.List;
6. public class ReverseArrayList {
7. public static void main(String[] args) {
8. List list = new ArrayList<>();
9. list.add(10);
10. list.add(50);
11. list.add(30);
12. Iterator i = list.iterator();
13. System.out.println("printing the list....");
14. while(i.hasNext())
15. {
16. System.out.println(i.next());
17. }
18. Iterator i2 = list.iterator();
19. Collections.reverse(list);
20. System.out.println("printing list in reverse order....");
21. while(i2.hasNext())
22. {
23. System.out.println(i2.next());
24. }
25. }
26. }
Output
printing the list....
10
50
30
printing list in reverse order....
30
50
10
9|Page
1. import java.util.ArrayList;
2. import java.util.Collection;
3. import java.util.Collections;
4. import java.util.Comparator;
5. import java.util.Iterator;
6. import java.util.List;
7.
8. public class ReverseArrayList {
9. public static void main(String[] args) {
10. List list = new ArrayList<>();
11. list.add(10);
12. list.add(50);
13. list.add(30);
14. list.add(60);
15. list.add(20);
16. list.add(90);
17.
18. Iterator i = list.iterator();
19. System.out.println("printing the list....");
20. while(i.hasNext())
21. {
22. System.out.println(i.next());
23. }
24.
25. Comparator cmp = Collections.reverseOrder();
26. Collections.sort(list,cmp);
27. System.out.println("printing list in descending order....");
28. Iterator i2 = list.iterator();
29. while(i2.hasNext())
30. {
31. System.out.println(i2.next());
32. }
33.
34. }
35. }
10 | P a g e
Output
printing the list....
10
50
30
60
20
90
printing list in descending order....
90
60
50
30
20
10
11 | P a g e