Java Collections Questions

I)  http://www.javatpoint.com/java-collections-interview-questions

Few Questions from it:

1) What is the difference between Collection and Collections?

Collection is an interface whereas Collections is a class.

Collection interface provides normal functionality of data structure to List, Set and Queue. But, Collections class is to sort and synchronize collection elements.

Both are present in java.util package and  part of java collections framework.

2) What is the difference between Comparable and Comparator?

No. Comparable Comparator
1) Comparable provides only one sort of sequence. Comparator provides multiple sort of sequences.
2) It provides one method named compareTo(). It provides one method named compare().
3) It is found in java.lang package. it is found in java.util package.
4) If we implement Comparable interface, actual class is modified. Actual class is not modified.

 

3) What is the advantage of generic collection?

If we use generic class, we don’t need typecasting. It is typesafe and checked at compile time.

 

4) What is the difference between Iterator and ListIterator?

Iterator traverses the elements in forward direction only whereas ListIterator traverses the elements in forward and backward direction.

No. Iterator ListIterator
1) Iterator traverses the elements in forward direction only. ListIterator traverses the elements in backward and forward directions both.
2) Iterator can be used in List, Set and Queue. ListIterator can be used in List only.

 

II) http://javahungry.blogspot.com/2015/05/50-java-collections-interview-questions-and-answers.html

1) What is Collection ? What is a Collections Framework ? What are the benefits of Java Collections Framework ?

Collection : A collection (also called as container) is an object  that groups multiple elements into a single unit.

Collections Framework : Collections framework provides unified architecture for manipulating and representing collections.

Benefits of Collections Framework :

1. Improves program quality and speed
2. Increases the chances of reusability of software
3. Decreases programming effort.

2) What is the root interface in collection hierarchy ? 

Root interface in collection hierarchy is Collection interface . Few interviewer may argue that
Collection interface extends Iterable interface. So iterable should be the root interface. But you should reply iterable interface present in java.lang package not in java.util package.

3)  Which collection classes are synchronized or thread-safe ?

Stack, Properties , Vector and Hashtable can be used in multi threaded environment because they are synchronized classes (or thread-safe).

4) Name the core Collection  interfaces ?

The list of core collection interfaces are : the important ones
Important : Collection , Set , Queue , List , Map
Other interface also in the list :  SortedSet, SortedMap , Deque, ListIterator etc.


5) What is an iterator ?

Iterator is an interface . It is found in java.util package. It provides methods to iterate over any Collection.

6) What is the difference between Iterator and Enumeration ?

The main difference between Iterator and Enumeration is that Iterator has remove() method while Enumeration doesn’t.
Hence , using Iterator we can manipulate objects by adding and removing the objects from the collections. Enumeration behaves like a read only interface as it can only traverse the objects and fetch it.


7)
Which methods you need to override to use any object as key in HashMap ?

To use any object as key in HashMap , it needs to implement equals() and hashCode() method .
8) How to reverse the List in Collections ?

There is a built in reverse method in Collections class . reverse(List list) accepts list as parameter.

Collections.reverse(listobject);

9) How to convert the array of strings into the list ?

Arrays class of java.util package contains the method asList() which accepts the array as parameter.
So,

String[]  wordArray =  {“Love Yourself”  , “Alive is Awesome” , “Be in present”};
List wordList =  Arrays.asList(wordArray);

10) What is the difference between ArrayList and Vector ?

1). Vector is synchronized while ArrayList is not .
2). Vector is slow while ArrayList is fast .
3). Every time when needed, Vector increases the capacity twice of its initial size while ArrayList increases its ArraySize by 50%.11) What is the difference between HashMap and Hashtable ?

1) HashMap allows one null key and any number of null values while Hashtable does not allow null keys and null values.
2) HashMap is not synchronized or thread-safe while Hashtable is synchronized or thread-safe.

12) What is the difference between peek(),poll() and remove() method of the Queue interface ?

Both poll() and remove() method is used to remove head object of the Queue. The main difference lies when the Queue is empty().
If Queue is empty then poll() method will return null . While in similar case , remove() method will throw NoSuchElementException .
peek() method retrieves but does not remove the head of the Queue. If queue is empty then peek() method also returns null.

13) What is the difference between HashSet and TreeSet ?

1)  HashSet maintains the inserted elements in random order while TreeSet maintains elements in the sorted order
2) HashSet can store null object while TreeSet can not store null object.

14)  What is the difference between HashMap and ConcurrentHashMap ?

1) HashMap is not synchronized while ConcurrentHashMap is synchronized.
2) HashMap can have one null key and any number of null values while ConcurrentHashMap does not allow null keys and null values .

15) Arrange the following in the ascending order (performance):
HashMap , Hashtable , ConcurrentHashMap and Collections.SynchronizedMap Hashtable  <  Collections.SynchronizedMap  <  ConcurrentHashMap  <  HashMap

16) What is the difference between LinkedList and ArrayList in Java ?

1) LinkedList is the doubly linked list implementation of list interface , while , ArrayList is the resizable array implementation of list interface.
2) LinkedList can be traversed in the reverse direction using descendingIterator() method  provided by the Java Api developers , while , we need to implement our own method to traverse ArrayList in the reverse direction

17) Why Map interface does not extend the Collection interface in Java Collections Framework ?

Map interface is not compatible with the Collection interface.
Explanation : Since Map requires key as well as value , for example , if we want to add key-value pair then we will use put(Object key , Object value) . So there are two parameters required to add element to the HashMap object  . In Collection interface add(Object o) has only one parameter.
The other reasons are Map supports valueSet , keySet as well as other appropriate methods which have just different views from the Collection interface.

18)When to use ArrayList and when to use LinkedList in application?

ArrayList has constant time search operation O(1) .Hence, ArrayList is preferred when there are more get() or search operation .

Insertion , Deletion operations take constant time O(1) for LinkedList. Hence, LinkedList is preferred when there are more insertions or deletions involved in the application.

 

Post to look into : http://www.journaldev.com/1330/java-collections-interview-questions-and-answers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s