Top Java Collection Interview Questions and Answers In 2021

In Java, questions on collections are most common amongst the interviewers. In this blog, we would be covering frequently asked java collection interview questions for experienced candidates and freshers to crack their following interview. If you are new to Java, then you should first check out What Java is and the resources to learn it.

Generic Java Collection Interview Questions

Let us explore some generic java collection interview questions : 

1. Which interface is part of the collection framework but does not extend the Collection interface? Why? 

The Map interface is a part but does not extend the Collection interface. This is because they are not compatible. Collection requires an object as a parameter, but the map takes a key-value pair.

2. Which method of the Collections class is used to return an integer hash code? 

hashCode() does the needful.

3. Name the class of Java collection framework that is used when you do not need duplicates?

Set is used to avoid duplicate values.

4. Why does Collection only extend Iterable and not Cloneable and Serializable?

This is because Serializable and Cloneable need not be extended because of their very specialized behavior.

5. What is enumeration?

Enumeration is the traversal of legacy elements only on the collection. It is not fail-fast. More so, it is an interface that defines the methods in which you can obtain one element at a time in a collection.

Full Stack Java Developer Course

The Gateway to Master Web DevelopmentExplore Course
Full Stack Java Developer Course

6. Can you sort a collection in Java? How?

Yes. It can be simply done by calling Collections.sort() or Collections.reverseOrder() method.

7. Define Java Generics. Note that one of the most asked topics for java collection interview questions is generics.

It is a crucial Java feature added to remove the risk of ClassCastException at runtime and provide compile-time code safety.

8. List down methods to make collection thread-safe. 

 All these methods take collection as parameter and return thread safe and synchronized collection :

  • Collections.synchronizedList(list);
  • Collections.synchronizedSet(set);
  • Collections.synchronizedMap(map);

9. Name the data structure that keeps the lowest or highest element at the head for accessing and removing it in constant time. 

PriorityQueue in Java is the mentioned data structure. It provides the facility of the queue but not in the First In First Out manner but rather elements are ordered based on custom comparator or as per their natural ordering.

10. What is meant by fail-safe? 

The opposite of fail-fast. It means that they will not fail if the underlying collection on which iteration takes place is modified.

List – Java Collection Interview Questions

Here are some java collection interview questions on the List interface:

1. Give a difference between LinkedList and ArrayList in Java?

Only ArrayList allows searching via index.

2. Mention the main classes implementing the List interface.

LinkedList- elements cannot be accessed via index

ArrayList- elements can be accessed with an index

Vector- Similar to array and can be indexed

Stack- Last In First Out method data structure.

3. What is a stack? Give  one of its advantages. 

Stack is a collection operation that works on the Last In First Out principle. It helps to manage data in a LIFO method that is not possible by Array or LinkedList.

4. Is there a vector in Java? Explain.

Yes. In Java, a vector is similar to an array. The elements can be accessed using the index value. It can contain legacy methods even if they are not part of the Collection framework.

5. Is it possible to convert an array to LinkedList? Which types of the array are convertible.

Yes, it is possible by the Array.aslist() method. Any array (String, int, char) can be converted.

6. Mention the features of an ArrayList. (Questions on ArrayList are also commonly asked as java collection interview questions)

ArrayList permits is an implementation of List Interface that contains duplicate values and is an ordered collection. The size can be increased dynamically.

7. What is the critical difference between a list and a set?

Set does not allow duplicates and is an unordered collection that allows only one null element. Whereas duplicates are allowed in the list, which is an ordered collection. The list also allows null elements.

8. Mention the linked lists supported by Java.

Java supports only two types of linked lists:

  1. Singly Linked List: Each node of the list stores data and reference to the next node,
  2. Doubly Linked List: One node references two nodes- previous and next.

9. How is an ArrayList different from a vector?

The ArrayList methods are not synchronized, and when resized, its size increases by half and not double as in the case of the vector.

10. How to create a linked list and a vector in Java?

Linkedlist obj = new Linkedlist();

Vector obj = new Vector(size,increment);

Set – Java Collection Interview Questions

Let us see some java collection interview questions on Sets:

1. Name the three general-purpose Set implementations provided by Java.

The three implementations of the Set are:

  1. HashSet
  2. TreeSet
  3. LinkedHashSet

2. Why is only one null key allowed in HashSet?

As there is no null check in the add method, so one null key is allowed. However, it can’t be duplicated.

3. How does the HashSet class store elements?

It is an instance of HashMap and uses a hashing technique to store information as a hash code.

4. Name a set interface apart from the general-purpose set implementations. Explain.

EnumSet is also a class implementing the Set interface. It can be used with enum types, not synchronized, and cannot have null keys.

5. What does the Collections.emptySet() return?

This method returns a serializable set that is empty and immutable.

6. HashSet features are very commonly asked as java collection interview questions.

How is it different from a HashMap?

HashSet is an unordered collection of unique elements, and HashMap is a collection of key-pair values.

FREE Java Certification Training

Learn A-Z of Java like never beforeEnroll Now
FREE Java Certification Training

7. What do you know about a SortedSet?

It is an interface present in Collection framework that a TreeSet implements. This set provides total ordering on its elements as per their natural ordering.

8. What is a LinkedHashSet?

It is a subclass of the HashSet class that implements the set interface. It maintains a doubly-linked list across it all elements and is a well-ordered version of HashSet.

9. Name two methods of the HashSet class.

boolean add(Object o)

Adds the given element to the set if not present already

boolean contains(Object o):

Checks if the specified element is present in the set.

10. Differentiate between Map and Set.

Set extends the collection interface, whereas the map does not. The map can have duplicate values and multiple null values.

In this final section, let’s see java collection interview questions related to map interface:

1. List down main classes implementing the Map interface.

  1. Hashtable
  2. HashMap
  3. IdentityHashMap
  4. EnumMap
  5. LinkedHashMap

2. Give one use of the Map interface in Java.

It is an ideal tool for association mapping using key-value pairs such as in a dictionary.

3. What is a WeakHashMap?

An implementation of the map interface, which includes only weak references to the keys. This allows garbage collection when the key is no longer referenced outside of the Map.

4. Name the three key-value pair views.

The Map interface provides three views, and all of them can be easily navigated by using iterators:

  1. Value set view
  2. Key set view
  3. Entry set view

5. Another most common java collections interview question is hashmap! Explain briefly how hashmap works.

It works on the principle of hashing. A map is an object that maps keys to values. It uses an inner class Entry to store such structure.

6. Why is ConcurrentHashMap not implemented even though it has a higher performance rate than a HashMap?

This is because it doesn’t perform well in a multithreaded environment.

7. Which class is considered the best candidate for HashMap keys? Why?

Immutable class as the object must return the same hashcode() again and again.

8. When should a TreeMap be used?

When you need to maintain the order of the keys, use a particular form of HashMap-TreeMap.

9. Tell about Linkedhashmap in brief.

An implementation of the Map interface enables developers to allow more than one null value and one null key.

10. Give an example of HashMap.

import java.util.HashMap;

import java.util.Map;

public class Main{

  public static void main(String[] args){

Map<String, String> obj = new HashMap<String, String>();

obj.put("Name", "Toyota");

obj.put("HP", "440");

obj.put("Wheels", "4-wheeler");

obj.put("Cost", "1650000");

    System.out.println("Elements:");

System.out.println(obj);

  }

}

Get a firm foundation in Java, the most commonly used programming language in software development with the Java Certification Training Course.

Conclusion

This concludes java collection interview questions, and now you are ready to face your interview! We have covered different types of java collection interview questions that are frequently asked in an interview. If you are looking to upskill your  Java skills, check out this comprehensive course by Simplilearn. Alternatively, if you want to make a career in mobile and software development, you can also check out Java full-stack development course here. Start preparing for your next interview now!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.