May

Collections in Java Interview Questions
- Shom Das
- 17th May, 2022
- 496 Followers
Collections in Java Interview Questions
Collections in Java Interview Questions
1) What is Collection in Java?
A Collection in Java is a group of objects that are represented as a single unit. The group of objects is stored as a single unit where you can manipulate the objects.
You can do various operations on this structure.
Such as:- searching, sorting, insertion, manipulation, and deletion.
The collection class is implemented from the java.util.Collection interface and java.util.Map interface.
2) What is garbage collection in Java?
The Garbage Collection in Java is a memory management process. In C/C++, the programmer is responsible for clearing the unused memory used by the objects. But in Java, the programmer need not take care of this unused memory as the garbage collector process automatically takes care of that process. This process mainly destroys the unused objects in the heap memory.
The garbage collection is an automatic process and is implemented in the JVM.
3) What is collection framework in java?
The collection framework in Java is used to store data and process it efficiently. It is nothing but a collection of interfaces and classes.
The collection interface is subdivided into the list, set, map, sorted map, sorted set, and enumeration. These interfaces present a unified architecture for representing and manipulating a collection of data. This framework has high-performance in implementing fundamental collections such as dynamic arrays, linked lists, trees, and hashtables efficiently.
4) What is collection interface in java?
The collection interfaces are the abstract data types that are used to represent collections. It allows the objects in the collections to be manipulated independently of the details of their representation.
The interface is generally presented at the top of the hierarchy in object-oriented languages.
5) What is list in java collections?
A list is one of the interfaces in the Java collection framework. It stores an ordered collection of data and objects. Duplicate values can also be stored in the list interface. The insertion and removal of elements in the list are based on the index.
The list interface has various default methods for manipulating data in it. Some of the important methods used in the list are added, addAll, clear, get, isEmpty, etc.
// Example of list declaration public interface List extends Collection
6) What is collection api in java?
The collection API (Application Programming Interface) provides the interfaces for structuring and arranging the data.
The interfaces provided by the API are Collection, List, Map, and Set. The interfaces of Collection API are present in Java. util package.
7) What is concurrent collections in java?
Concurrent collections are different from the traditional collections in a way that it is synchronized in nature. Traditional collections such as Array, HashMap are not synchronized and cannot leverage multiple threads to perform actions simultaneously.
So developers can use the java.util.concurrent package to write concurrent collections to leverage multi-threading. Concurrent collections also improve the performance of the applications.
8) What are generic collection in java?
Generics Collection is to enforce the type-safety of the objects. Without using generics, we can store any type of object in the collection. But generic enforces the collection to store a specific type of collection. It ensures the type-safety of the objects thus reducing bugs at the compile time. Generics also removes the need to typecast an object.
9) How to sort collection in Java?
The sort collections method is used to sort the collection in Java. This method is defined in the java.util.Collections class and it sorts the collection specified in the ascending order.
We can also specify a comparator to order the elements according to it.
//syntax sort(List list) //it sorts the list in the ascending order.
10) What Java Stream collect() Method do?
The stream collect() method is used to receive the elements from the stream and store it as a collection. The stream collect method also takes care of synchronization when used with a parallel stream. It can be considered as a terminal operation.
11) List types of collections in java?
Java collections are divided into interfaces and classes. The interfaces provided by the collection are the set, list, queue, and dequeue.
The classes provided by the collections are
- ArrayList
- Vector
- LinkedList
- PriorityQueue
- HashSet
- LinkedHashSet
- TreeSet
12) How to convert java collection to array?
The toArray() method is used to convert Java collection into an Array. This method is defined in the java.util.Collection interface.
//definition T[] toArray(T[] a)
The above function is called with the java collection and it returns an array with the elements of the collection.
13) What are thread safe collections in java?
The concurrent collections are called the thread-safe collections in Java. This collection is called as thread-safe because they are synchronized in nature. So, the concurrent collections can leverage the multi-thread and increase the performance of the application.
Leave A Comment :
Valid name is required.
Valid name is required.
Valid email id is required.