1) What is collection?

A collection is an object that represents group of objects.

2) What are the differences between array & collection?

Array

Collection

An array is a collection of similar data elements.

A collection is an object that represents group of objects.

Array fixed in size.

The size of collection can increase & it can decrease dynamically as needed.

It supports homogeneous elements only.

It supports both homogeneous & heterogeneous elements.

3) What are the advantages of collections framework?

1) Reduces programming effort.

2) Increases programming speed & quality.

3) Allows interoperability among unrelated APIs.

4) What is the difference between Collection & Collections?

Collection is an interface & it is a root interface for List, Set & Queue where as Collections is a utility class & it contains several useful methods that are used to perform operations on collection elements.

5) What are the differences between List & Set?

List

Set

It allows duplicate elements.

It does not allow duplicate elements.

It maintains sequences

It does not maintain sequences.

List implementations are ArrayList, LinkedList & Vector.

Set implementations are HashSet, LinkedHashSet & TreeSet.


6) What are the differences between Set & Map?

Set

Map

It contains elements.

It contains key/value pairs.

It does not allow duplicate elements.

It does not allow duplicate keys & it allows duplicate values.

It is an index based collection.

It is a key based collection.

Set implementations are HashSet, LinkedHashSet & TreeSet.

Map implementations are HashMap, LinkedHashMap & TreeMap.

7) What are the differences between Queue & Deque?

Queue

Deque

It is a single ended linear collection.

It is a double ended linear collection.

It is a First In First Out(FIFO) list.

It is not a First In First Out list but it can be used to implement as First In First Out list & Last In First Out list.

It allows insertion at rear end only.

It allows insertion at both the ends(front end & rear end).

It allows deletion at front end only.

It allows deletion at both the ends(front end & rear end).

8) What is the default initial capacity of ArrayList?

The default initial capacity of ArrayList is 10.

9) What are the differences between ArrayList & LinkedList?

ArrayList

LinkedList

It is an array representation of List implementation class.

It is a linked representation of List implementation class.

It is an implementation of linear list data structure.

It is an implementation of double linked list data structure.

It occupies less memory.

It occupies more memory because data stored in nodes.

In ArrayList, insertion & deletion requires shuffling of data.

In LinkedList, it does not require shuffling of data.

The default initial capacity of ArrayList is 10.

The default initial capacity of LinkedList is 0.

10) What is CopyOnWriteArrayList?

CopyOnWriteArrayList is an enhancement of ArrayList. It is a thread safe variant of ArrayList & it is introduced in JDK 1.5 version in 2004.

11) What are the differences between HashSet & LinkedHashSet?

HashSet

LinkedHashSet

It is an implementation of hashing technique with array representation.

It is an implementation of hashing technique with linked representation.

It occupies less memory.

It occupies more memory because data stored in nodes.

HashSet is an unordered set.

LinkedHashSet is an ordered set.

In HashSet, insertion order is not preserved.

In LinkedHashSet, insertion order is preserved.

12) What is the default initial capacity of HashSet?

The default initial capacity of HashSet is 16.

13) What is the default load factor of HashSet?

The default load factor of HashSet is 75%.

14) What is the default initial capacity of TreeSet?

The default initial capacity of TreeSet is 0.

15) What are the differences between HashSet & TreeSet?

HashSet

TreeSet

It uses hashing technique.

It uses red black tree(similar to binary search tree).

It is an unordered set.

It is a sorted set.

It allows one null.

It does not allow null.

The default initial capacity of HashSet is 16.

The default initial capacity of TreeSet is 0.

16) What are the differences between HashSet & HashMap?

HashSet

HashMap

It contains elements.

It contains key/value pairs.

It does not allow duplicate elements.

It does not allow duplicate keys & it allows duplicate values.

It allows one null.

It allows one null key & many null values.

It implements Set interface.

It implements Map interface.

It is an index based collection.

It is a key based collection.

17) What is the main difference between HashMap, LinkedHashMap & TreeMap?

HashMap is an unordered map, LinkedHashMap is an ordered map & TreeMap is a sorted map.

18) What is IdentityHashMap?

IdentityHashMap uses == operator to compare keys where as HashMap uses equals() method to compare keys.

19) What is WeakHashMap?

WeakHashMap allows to remove values when its key is no longer in use because it uses weak reference where as HashMap uses strong reference.

20) What is ConcurrentHashMap?

ConcurrentHashMap allows multiple threads to read & write concurrently without corrupting the data & it does not allow null keys & null values.

21) What is the default initial capacity of PriorityQueue?

The default initial capacity of PriorityQueue is 11.

22) What is the default initial capacity of ArrayDeque?

The default initial capacity of ArrayDeque is 16.

23) What is PriorityQueue?

A PriorityQueue is a queue in which elements are processed based on their priority. In PriorityQueue always smallest element comes out first because it uses minimum heap tree.

24) What is BlockingQueue?

A BlockingQueue is a thread safe queue in which if a thread tries to take an element from an empty queue, it is blocked until an element is available & if a thread tries to add an element to a full queue, it is blocked until space available.

25) What is ArrayDeque?

ArrayDeque is a resizable array implementation of the Deque interface & it allows both insertion & deletion at both the ends(front end & rear end).

26) What are the differences between Iterator & ListIterator?

Iterator

ListIterator

It allows to iterate elements of any collection.

It allows to iterate elements of ArrayList & LinkedList only.

It supports forward direction only.

It supports both forward & backward directions.

It allows only remove operation while iterating elements.

It allows add, remove & set(modify) operations while iterating elements.

27) What is the difference between Fail-Fast & Fail-Safe?

Fail-Fast

Fail-Safe

It throws ConcurrentModification Exception  if a collection is modified while iterating.

It does not throw exception if a collection is modified while iterating.

It uses original collection to traverse.

It uses copy of original collection to traverse.

It does not require extra memory.

It requires extra memory to clone.

Examples are HashMap, HashSet, ArrayList & Vector.

Examples are CopyOnWriteArrayList & ConcurrentHashMap.

28) What is Map.Entry?

Entry is an inner interface of Map interface & it allows working with both key and value of a Map simultaneously while iterating.

29) What are the differences between Comparator & Comparable?

Comparator

Comparable

It is in java.util package.

It is in java.lang package

It contains  compare(T t1, T t2) method to compare the elements.

It contains compareTo(T t) method to compare the elements.

Here comparison logic can be in any other class also.

Here comparison logic must be present in the same class.

Here multiple sorting sequences are possible.

Here only one sorting sequence is possible.

30) What are legacy collections?

JDK 1.0 & 1.1 version classes & interfaces are called legacy collections. Examples: Stack, Vector, Hashtable, Properties, Random, Enumeration & StringTokenizer.

31) What are the differences between Enumeration & Iterator?

Enumeration

Iterator

It is a legacy collection interface.

It is a collections framework interface.

It does not allow other operations while iterating elements.

It allows remove operation while iterating elements.

32) What are the differences between Stack & Queue?

Stack

Queue

It is a legacy collection class.

It is a collections framework interface.

It is a Last In First Out(LIFO) list.

It is a First In First Out(FIFO) list.

It allows both insertion & deletion at top end only.

It allows insertion at rear end & deletion at front end only.

33) What are the differences between Vector & ArrayList?

Vector

ArrayList

It is a legacy collection class.

It is a collections framework class.

Methods of Vector class are synchronized.

Methods of ArrayList class are not synchronized.

34) What are the differences between Hashtable & HashMap?

Hashtable

HashMap

It is a legacy collection class.

It is a collections framework class.

Methods of Hashtable are synchronized.

Methods of HashMap are not synchronized.

It does not allow null keys & null values.

It allows one null key & multiple null values.

35) What is Properties class?

The Properties class represents a persistent set of properties. Here properties can be saved to a stream or loaded from a stream.

No comments:

Post a Comment