Collections in Java
- 1) What is Collection in Java?
- 2) What is garbage collection in Java?
- 3) What is collection framework in java?
- 4) What is collection interface in java?
- 5) What is list in java collections?
- 6) What is collection api in java?
- 7) What is concurrent collections in java?
- 8) What are generic collection in java?
- 9) How to sort collection in Java?
- 10) What Java Stream collect() Method do?
- 11) List types of collections in java?
- 12) How to convert java collection to array?
- 13) What are thread safe collections in java?
Below are the list of Best Collections in Java and Answers
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.
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.
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.
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.
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
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.
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.
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.
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.
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.
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
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.
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.
Related Interview Questions
Kotlin Interview Questions
Core Java interview questions
Vaadin interview questions
Scala interview questions
Maven interview questions
OpenXava Interview questions
Java Play interview Questions
Groovy interview questions
Hibernate Interview Questions
Java Grails Interview Questions
Apache Ant Interview questions
Apache Kafka Interview Questions
Gradle Interview Questions
JSF Interview Questions
JSP Interview Questions
JUnit Interview Questions
Spring interview questions
Struts interview questions
Spring Boot Interview questions
Servlet interview questions
JDBC interview questions
Subscribe Our NewsLetter
Never Miss an Articles from us.
- Most Common Interview Questions
- Python Flask Interview Questions
- NoSQL interview questions
- JQuery Interview Questions
- C programming interview questions
- AngularJS Interview Questions
- Node JS Interview Questions with Express
- Core Java interview questions
- HTML Interview Questions
- Laravel interview questions
- Wordpress Interview Questions
- PHP Interview Questions