Java multithreading interview questions Set 5
Categories: Java 8(JDK1.8)
Define FutureTask class in Java?
Java FutureTask class provides a base implementation of the Future interface. The result can only be obtained if the execution of one task is completed, and if the computation is not achieved then get method will be blocked. If the execution is completed, then it cannot be re-started and can't be canceled.
Syntax
public class FutureTask<V> extends Object implements RunnableFuture<V>
What is the Collection framework in Java?
Collection Framework is a combination of classes and interface, which is used to store and manipulate the data in the form of objects. It provides various classes such as ArrayList, Vector, Stack, and HashSet, etc. and interfaces such as List, Queue, Set, etc. for this purpose.
What are the main differences between array and collection?
Array and Collection are somewhat similar regarding storing the references of objects and manipulating the data, but they differ in many ways. The main differences between the array and Collection are defined below:
a) Arrays are always of fixed size, i.e., a user can not increase or decrease the length of the array according to their requirement or at runtime, but In Collection, size can be changed dynamically as per need.
b) Arrays can only store homogeneous or similar type objects, but in Collection, heterogeneous objects can be stored.
c) Arrays cannot provide the ?ready-made? methods for user requirements as sorting, searching, etc. but Collection includes readymade methods to use.
What is the difference between Set and Map?
The differences between the Set and Map are given below.
a) Set contains values only whereas Map contains key and values both.
b) Set contains unique values whereas Map can contain unique Keys with duplicate values.
c) Set holds a single number of null value whereas Map can include a single null key with n number of null values.
Explain various interfaces used in Collection framework?
Collection framework implements various interfaces, Collection interface and Map interface (java.util.Map) are the mainly used interfaces of Java Collection Framework. List of interfaces of Collection Framework is given below:
1. Collection interface: Collection (java.util.Collection) is the primary interface, and every collection must implement this interface.
Syntax:
public interface Collection<E>extends Iterable
Where <E> represents that this interface is of Generic type
2. List interface: List interface extends the Collection interface, and it is an ordered collection of objects. It contains duplicate elements. It also allows random access of elements.
Syntax:
public interface List<E> extends Collection<E>
3. Set interface: Set (java.util.Set) interface is a collection which cannot contain duplicate elements. It can only include inherited methods of Collection interface
Syntax:
public interface Set<E> extends Collection<E>
What is the difference between ArrayList and Vector?
No.ArrayListVector
1)ArrayList is not synchronized.Vector is synchronized.
2)ArrayList is not a legacy class.Vector is a legacy class.
3)ArrayList increases its size by 50% of the array size.Vector increases its size by doubling the array size.
4)ArrayList is not ?thread-safe? as it is not synchronized.Vector list is ?thread-safe? as it?s every method is synchronized.
What is the difference between ArrayList and LinkedList?
No.ArrayListLinkedList
1)ArrayList uses a dynamic array.LinkedList uses a doubly linked list.
2)ArrayList is not efficient for manipulation because too much is required.LinkedList is efficient for manipulation.
3)ArrayList is better to store and fetch data.LinkedList is better to manipulate data.
4)ArrayList provides random access.LinkedList does not provide random access.
5)ArrayList takes less memory overhead as it stores only objectLinkedList takes more memory overhead, as it stores the object as well as the address of that object.
What is the difference between Iterator and ListIterator?
Iterator traverses the elements in the forward direction only whereas ListIterator traverses the elements into forward and backward direction.
No.IteratorListIterator
1)The Iterator traverses the elements in the forward direction only.ListIterator traverses the elements in backward and forward directions both.
2)The Iterator can be used in List, Set, and Queue.ListIterator can be used in List only.
3)The Iterator can only perform remove operation while traversing the collection.ListIterator can perform ?add,? ?remove,? and ?set? operation while traversing the collection.
What is the difference between Iterator and Enumeration?
No.IteratorEnumeration
1)The Iterator can traverse legacy and non-legacy elements.Enumeration can traverse only legacy elements.
2)The Iterator is fail-fast.Enumeration is not fail-fast.
3)The Iterator is slower than Enumeration.Enumeration is faster than Iterator.
4)The Iterator can perform remove operation while traversing the collection.The Enumeration can perform only traverse operation on the collection.
What is the difference between List and Set?
The List and Set both extend the collection interface. However, there are some differences between the both which are listed below.
a) The List can contain duplicate elements whereas Set includes unique items.
b) The List is an ordered collection which maintains the insertion order whereas Set is an unordered collection which does not preserve the insertion order.
c) The List interface contains a single legacy class which is Vector class whereas Set interface does not have any legacy class.
d) The List interface can allow n number of null values whereas Set interface only allows a single null value.
What is the difference between HashSet and TreeSet?
The HashSet and TreeSet, both classes, implement Set interface. The differences between the both are listed below.
a) HashSet maintains no order whereas TreeSet maintains ascending order.
b) HashSet impended by hash table whereas TreeSet implemented by a Tree structure.
c) HashSet performs faster than TreeSet.
d) HashSet is backed by HashMap whereas TreeSet is backed by TreeMap.