Java Interview Questions and Answers on Collection - PLM Developer

Collections are one of the widely used concepts in java. As a developer, I use java collection concepts while writing the code every day. Collections play a vital role in application development. The primary use of java collections is to hold and process the data and perform many operations like searching and sorting. So, Collection is an essential topic for any java coding interview. This post is dedicated to frequently asked java collection interview questions and answers.

 

Key Points:

  • List maintains insertion order & allows duplicates.
  • Set removes the duplicates & doesn't maintain any order.
  • Map maintains the data in key-value pairs so that we can get any value using its key. This feature makes retrieving (search) element O(1) time.


Q. What is the collection framework in java?

The collection framework is a combination of classes and interfaces used to store and maintain the data in objects. Collection framework offers various classes like ArrayList, Vector, HashSet, Stack, and interfaces like List, Set, Queue, etc.


Q. What is the difference between Array and Collection?

An array stores homogenous elements.

Collection stores heterogeneous elements.

Arrays are fixed in size.

The Collection grows dynamically when the size of the collection object is full.

Arrays don't provide any methods to do operations on data.

The Collection provides readymade methods to do operations on data like searching and sorting.


Q. what is the difference between List and Set?

The list is an ordered collection of elements. It preserves the insertion order and allows duplicate elements.

Set is a collection that doesn't preserve the insertion order and doesn't allow duplicates.


Q. What is the difference between ArrayList and Vector?

  • ArrayList is not synchronized.
  • Vector is synchronized
  • ArrayList is not thread-safe so, multiple threads can operate concurrently.
  • Vector is thread-safe so, multiple threads can't operate concurrently.
  • ArrayList increases its size by 50% when the list is full.
  • Vector doubles its size when it is full.
  • ArrayList is not a legacy class.
  • Vector is a legacy class.


Q. What is the difference between HashMap and TreeMap?

HashMap doesn't follow any order, but TreeMap stores the elements in ascending order.

HashMap can have a null key with multiple null values, whereas TreeMap cannot have the null key but can have multiple null values.


Q. What is the difference between HashSet and TreeSet?

Elements in HashSet are not ordered, but TreeSet stores the elements in ascending order by default.

Operations 

HashSet gives better performance while doing operations like search, insert and delete takes constant time on average, whereas TreeSet is slower than HashSet.

 

See Also:

👉Java Interview Questions & Answers 1

👉Java Interview Questions & Answers 2

👉Java Interview Questions & Answers 3

 

Post a Comment

0 Comments