Hello guys, Java Collection and Generic are a very important topic for Java Interviews. They also present some of the hardest questions to a programmer when it comes to interviews, especially Generics.

It’s not easy to first understand what a particular piece of code doing with those question marks and other signs and then the pressure of interviews also makes it hard to answer complex usage of Generics.

But, with proper preparation and paying attention to both Java Collection and Generic, you can solve that hurdle. If you are looking for Java job but haven’t done well in the interviews you have given so far then you have come to the right place.

In this article, I have shared a lot of Java interview questions on various topics and difficulty levels.

There are Java questions for beginners as well as expert programmers. They are theoretical questions based upon Java programming concepts as well as coding and data structure algorithms questions for programmers, and this article is only going to make that collection even more valuable.

In this article, I am going to share some of the frequently asked Java Collection and Generic questions from Interviews. These are the questions you have often seen on a telephonic round of Java interview as well as on face-to-face interviews.

It’s useful for both beginners having 2 to 3 years of experience as well as experienced Java programmers with 5 to 6 years of experience.

This list has a collection of questions which has both easy and tough questions in it but the most important thing is that most of the questions have already been asked on interviews. I am sure you might have also seen it in your interviews.

knowing the answers to these questions will not only help you to crack your Java interview but also understand Java Generics and Collection topic in-depth, which will eventually help you to write better Java programmers and code.

Btw, if you are new to Java or want to solidify your Java knowledge then you should check out a comprehensive course like The Complete Java Masterclass before attempting to solve these questions. It will help you immensely by filling gaps in your knowledge and going back and forth. It’s also the most up-to-date course and covers every new feature introduced in new Java releases

50+ Java Collection and Generic Interview Questions

Without wasting any more of your time, here is my list of 50+ Java interview questions on Collection and Generics.

If you have done some work in Java +then you should know the answer to these questions but if you don’t you can always see the answer.

Instead of writing answers here, I have linked them to relevant posts so that you can try to solve the problem by yourself here and if you need you can get an in-depth discussion on individual posts to learn the topic in depth.

1) What is the Java Collection Framework and How do you choose different collections? (answer)

Here is the diagram which answers this question:

storing multiple objects

2) What are Generics in Java? (answer)

hint: Java feature to ensure type safety at compile time.

3) Which are your favorites classes from Java Collection Framework? (answer)

hint: Collection, ListSetMapArrayListVectorLinkedListHashMap, etc

4) When do you use Set, List, and Map in Java? (answer)

hint — use set when you don’t need duplicates, use List when you need order with duplicates, and use Map when you need to store key-value pair.

5) Which Sorted Collection have you used? (answer)

hint — TreeSet is one example of a sorted Collection

6) How HashSet works in Java? (answer)

hint — same as HashMap, using hashing and equals() and hashCode() method. HashSet is actually backed by HashMap where keys are elements you store in HashSet and values are always null.

**7) Which two methods you should override for an Object to be used as a key in hash-based Collections? **(answer)

hint — equals and hashcode

**8) Can you use HashMap in a concurrent application? **(answer)

hint — Yes, but only if you are reading from the HashMap and its initialized by a single thread, otherwise no.

**9) What is the difference between HashMap and Hashtable in Java? **(answer)

hint — HashMap is fast but not threadsafe, Hashtable is slow but thread-safe

** 10) What is the difference between synchronized and concurrent Collection in Java? (answer)**

11) How ConcurrentHashMap works in Java? (answer)

partitions map into segments and lock them individually instead of locking the whole map.

12) What is PriorityQueue in Java? (answer)

A data structure that always keeps the highest or lowest element at the head so that you can access or remove it in constant time.

**13) What is type-erasure in Generics? **(answer)

Its a part of Java compiler which removes all type related information after compilation from Java so that the generated code is the same as before Generics.

** 14) What is the difference between ArrayList and Vector in Java? (answer)**

hint — ArrayList is not synchronized hence fast, Vector is synchronized hence slow

15) What is the difference between LinkedList and ArrayList in Java? (answer)

hint — ArrayList is backed by array while LinkedList is backed by a linked list which means search with index is only possible in ArrayList.

** 16) What is the difference between Hashtable and ConcurrentHashMap in Java? (answer)**

hint — ConcurrentHashMap is a new concurrent class with better scalability as only a portion of the map called segment is locked while Hashtable is an old class where the whole map is Locke for synchronization. Seet **Java Collections: Fundamentals **course for more details.

java collection framework

17) What is the difference between LinkedHashSet and TreeSet in Java? (answer)

hint — TreeSetis a sorted set where elements are stored in their natural or custom sorting order depending upon comparable and comparator while LinkedHashSet is just an ordered collection that maintains insertion order.

#java #interview #java interview #interview-questions

Top 50 Java Collections Interview Questions
3.35 GEEK