Collection Framework Based Questions- 2



1. What's the main difference between a Vector and an ArrayList 
Java Vector class is internally synchronized and ArrayList is not. Hence, Vectors are thread-safe while ArrayLists are not. On the Contrary, ArrayLists are faster and Vectors are not. ArrayList has no default size while vector has a default size of 10.

2. What's the difference between a queue and a stack? 

Stacks works by last-in-first-out rule (LIFO), while queues use the first-in-first-out (FIFO) rule

3. what is a collection? 

Collection is a group of objects.

There are two fundamental types of collections they are Collection and Map.

Collections just hold a bunch of objects one after the other while Maps hold values in a key-value pair.

Collections Ex: ArrayList, Vector etc.
Map Ex: HashMap, Hashtable etc

4. What is the Collections API? 

The Collections API is a set of classes and interfaces that support operation on collections of objects. The API contains Interfaces, Implementations & Algorithm to help java programmer in everyday programming using the collections.

The purpose of the Collection API is to:

o Reduces programming efforts. - Increases program speed and quality.
o Allows interoperability among unrelated APIs.
o Reduces effort to learn and to use new APIs.
o Reduces effort to design new APIs.
o Encourages & Fosters software reuse.

Example of classes: HashSet, HashMap, ArrayList, LinkedList, TreeSet and TreeMap.
Example of interfaces: Collection, Set, List and Map.

5. What is the List interface? 

The List interface provides support for ordered collections of objects. Ex: ArrayList

6. What is the Vector class? 

The Vector class provides the capability to implement a growable array of objects that is synchronzied and thread-safe.

7. What is an Iterator interface?

The Iterator interface is used to step through the elements of a Collection one by one. It is an easier way to loop through the contents of a collection when you do not know, how many objects are present in it. Also, it is a cleaner way to access the contents of a collection when compared to using a for loop and accessing entries using get(index) method.

Remember that, when using Iterators they contain a snapshot of the collection at the time the Iterator was obtained; generally it is not advisable to modify the collection itself while traversing an Iterator.

8. What is the Map interface? 

A map is an object that stores associations between keys and values (key/value pairs). Given a key, you can find its value. Both keys and values are objects. The keys must be unique, but the values may be duplicated. Some maps can accept a null key and null values, others cannot.


9. What is the Collection interface? 

The Collection interface provides support for the implementation of a mathematical bag - an unordered collection of objects that may contain duplicates.

10. What is the Set interface? 

• The Set interface provides methods for accessing the elements of a finite mathematical set
• Sets do not allow duplicate elements
• Contains no methods other than those inherited from Collection
• It adds the restriction that duplicate elements are prohibited
• Two Set objects are equal if they contain the same elements


11. What are different types of collections? 

* A regular collection has no special order and does not reject duplicates
* A list is ordered and does not reject duplicates
* A set has no special order but rejects duplicates
* A map supports storing data as key-value pairs

12. Difference between Hashtable and HashMap? 

* Hashtable does not store null value, while HashMap does
* Hashtable is synchronized, while HashMap is not
* Hashtables are slower because of the synchronization overhead while HashMaps are faster

13.What are the main Implementations of the Set interface ? 

The main implementations of the List interface are as follows:
HashSet
TreeSet
LinkedHashSet
EnumSet

14.What is a HashSet ?

A HashSet is an unsorted, unordered Set. It uses the hashcode of the object being inserted to access the object. So, the more efficient your hashcode() implementation the better access performance you’ll get. Use this class when you want a collection with no duplicates and you don’t care about order when you iterate through it.

14.What is a TreeSet ?
TreeSet is a Set implementation that keeps the elements in sorted order. The elements are sorted according to the natural order of elements or by the comparator provided at creation time.

15.What is an EnumSet ?

An EnumSet is a specialized set for use with enum types, all of the elements in the EnumSet type that is specified, explicitly or implicitly, when the set is created.

16.Difference between HashSet and TreeSet ? 

HashSet does not store data in any Order while the TreeSet stores data in order. Also, you can elements of any type to the hash set while you can only add similar types of elements into a tree set.


17.What are the main Implementations of the Map interface ? 

The main implementations of the List interface are as follows:
HashMap
HashTable
TreeMap
EnumMap

18.What is a TreeMap ?

TreeMap actually implements the SortedMap interface which extends the Map interface. In a TreeMap the data will be sorted in ascending order of keys according to the natural order for the key's class, or by the comparator provided at creation time. TreeMap is based on the Red-Black tree data structure.

19.How do you decide when to use HashMap and when to use TreeMap ?

For inserting, deleting, and locating elements in a Map, the HashMap offers the best alternative. If, however, you need to traverse the keys in a sorted order, then TreeMap is your better alternative. Depending upon the size of your collection, it may be faster to add elements to a HashMap, then convert the map to a TreeMap for sorted key traversal.

20. Can I store multiple keys or values that are null in a HashMap? 

You can store only one key that has a value null. But, as long as your keys are unique, you can store as many nulls as you want in an HashMap.


No comments:

Post a Comment