Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3
JAVA Interview Question
1. Collection:
Classes that Implement List
Since List is an interface, we cannot create objects from it. In order to use the functionalities of the List interface, we can use these classes: ArrayList LinkedList Vector Stack
In Java, we must import java.util.List package in order to use List.
// ArrayList implementation of List
List<String> list1 = new ArrayList<>();
// LinkedList implementation of List
List<String> list2 = new LinkedList<>(); Java ArrayList In Java, we use the ArrayList class to implement the functionality of resizable-arrays. It implements the List interface of the collections framework.
Java ArrayList Vs Array
In Java, we need to declare the size of an array before we can use it. Once the size of an array is declared, it's hard to change it. To handle this issue, we can use the ArrayList class. It allows us to create resizable arrays. Unlike arrays, arraylists can automatically adjust their capacity when we add or remove elements from them. Hence, arraylists are also known as dynamic arrays.
In Java, both ArrayList and Vector implements the List interface and provides the same functionalities. However, there exist some differences between them. The Vector class synchronizes each individual operation. This means whenever we want to perform some operation on vectors, the Vector class automatically applies a lock to that operation. However, in array lists, methods are not synchronized. Instead, it uses the Collections.synchronizedList() method that synchronizes the list as a whole.
What is the difference between Java ArrayList and LinkedList?
Some of the major differences between ArrayList and LinkedList in Java are:
ArrayList LinkedList
Implements List interface Implements List, Queue, and Deque interfaces.
Stores a single value. Stores 3 values: data, previous and next
address
Provides the functionality of a resizable
Provides the functionality of doubly-linked list array.
Java List vs. Set
Both the List interface and the Set interface inherit the Collection interface. However, there exists some difference between them. 1. Lists can include duplicate elements. However, sets cannot have duplicate elements. 2. Elements in lists are stored in some order. However, elements in sets are stored in groups like sets in mathematics.