您的位置:首页 > 产品设计 > UI/UE

Java Collections Interview Questions

2015-05-05 21:34 246 查看
In this post we will see some of the most asked Java Collections related interview questions with answers.

Q: What is the Collections API?

The Collections API provides set of classes and interfaces that helps to operate on collections of objects.

Q: What is the List, Vector, Iterator, Map, Set interface?

List interface provides support for ordered collections of objects.

Vector class provides capability to implement a growable array of objects.

Iterator interface is used to step through elements of a Collection .

Map interface replaces JDK 1.1 Dictionary class and is used associate keys with values.

Set interface provides methods for accessing elements of a finite mathematical set. Sets do not allow duplicate elements

Q: What is the typical use of HashTable?

If your program want to store a key value pair, you can use HashTable.

Q: Can I store values in HashTable where the value is already exist?

Yes, you can store value to the HashTable using key. If the value already exist for that key then it will replace the value with the new value.

Q: What is the difference between the size and capacity of a Vector?

The size is the number of elements actually stored in the vector, while capacity is the maximum number of elements it can be stored at a given instance of time.

Q: What is difference between ArrayList and vector?

Synchronization – ArrayList is not thread-safe whereas Vector is thread-safe. In Vector class each method like
add()
,
get(int
i)
is surrounded with a synchronized block and thus making Vector class thread-safe.

Data growth – Internally, both the ArrayList and Vector hold onto their contents using an Array. When an element is inserted into an ArrayList or a Vector, the
object will need to expand its internal array if it runs out of room. A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent.

Q: What is difference between HashMap and HashTable?

Both collections implements Map. Both collections store value as key-value pairs. The key differences between the two are

Hashmap is not synchronized in nature but HashTable is.

Another difference is that iterator in the HashMap is fail-safe while the enumerator for the HashTable isn’t. Fail-safe – if the HashTable is structurally modified at any time after the iterator is created, in any way except through the iterator’s own remove
method, the iterator will throw a
ConcurrentModificationException


HashMap permits null values and only one null key, while HashTable doesn’t allow key or value as null.

Q: When to use ArrayList or LinkedList ?

For the ArrayList, doing random lookup using
get()
is fast, but for LinkedList,
it’s slow. It’s slow because there’s no efficient way to index into the middle of a linked list.

When removing elements, using ArrayList is slow. This is because all remaining elements in the underlying array of Object instances must be shifted down for each remove operation. But here LinkedList is fast, because deletion can be done simply by changing
a couple of links.

So an ArrayList works best for cases where you’re doing random access on the list, and a LinkedList works better if you’re doing a lot of editing in the middle of the list.

Q: Which design pattern Iterator follows?

Iterator follows behavioral design pattern. I allows you to navigate through a collection of data using a common interface without knowing about the underlying implementation. The benefits of Iterator are about their strength to provide a common interface for
iterating through collections without bothering about underlying implementation.

Example – The class java.util.Enumeration : It represents and abstract means of iterating over a collection of elements in some sequential order without the client having to know the representation of the collection being iterated over. It can be used to provide
a uniform interface for traversing collections of all kinds.

Q: Can a Vector or ArrayList contain heterogenous objects?

Yes a Vector can contain heterogenous objects. Because a Vector stores everything in terms of Object.

Q: What is an enumeration?

It is a construct which collection classes return when you request a collection of all the objects stored in the collection. It allows sequential access to all the elements stored in the collection.

Q: Where will you use Vector and where will you use ArrayList?

Vector is synchronized while ArrayList is not. Thus whenever there is a possibility of multiple threads accessing the same instance, one should use Vector. While if not multiple threads are going to access the same instance then use ArrayList. Non synchronized
data structure will give better performance than the synchronized one.

Q: What is HashMap and Map?

Map is Interface and HashMap is class that implements this interface.

Q: What is the difference between Iterator and ListIterator?

Iterator enables you to cycle through a collection in the forward direction only, for obtaining or removing elements

ListIterator extends Iterator, allow bidirectional traversal of list and the modification of elements

Q: Difference between HashMap and HashTable? Can we make hashmap synchronized?

HashMap is not synchronized while HashTable is. HashMap can be synchronized by
Map m = Collections.synchronizeMap(hashMap);

Q: What is the difference between Set and List?

A Set stores elements in an unordered way and does not contain duplicate elements, whereas a list stores elements in an ordered way but may contain duplicate elements.

Q: What is an Iterator interface? Is Iterator a Class or Interface? What is its use?

The Iterator is an interface, used to traverse through the elements of a Collection. It is not advisable to modify the collection itself while traversing an Iterator.

Q: How can we access elements of a collection?

We can access the elements of a collection using the following ways:

Every collection object has
get(index)
method to get the element of the object.
This method will return Object.

Collection provide Enumeration or Iterator object so that we can get the objects of a collection one by one.

Q: What’s the difference between a queue and a stack?

Stack is a data structure that is based on last-in-first-out rule (LIFO), while queues are based on First-in-first-out (FIFO) rule.

Q: How can we use HashSet in collection interface?

This class implements the set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits
the Null element. This class offers constant time performance for the basic operations (add, remove, contains and size), assuming the hash function disperses the elements properly among the buckets.

Q: What are differences between Enumeration, ArrayList, HashTable and Collections and Collection?

Enumeration: It is series of elements. It can be use to enumerate through the elements of a vector, keys or values of a hashtable. You can not remove elements from Enumeration.

ArrayList: It is resizable array implementation. Belongs to ‘List’ group in collection. It permits all elements, including null. It is not thread -safe.

Hashtable: It maps key to value. You can use non-null value for key or value. It is part of group Map in collection.

Collections: It implements Polymorphic algorithms which operate on collections.

Collection: It is the root interface in the collection hierarchy.

Q: What is difference between array and ArrayList?

An ArrayList is resizable, whereas, an array is not. ArrayList is a part of the Collection Framework. We can store any type of objects, and we can deal with only objects. It is growable. Array is collection of similar data items. We can have array of primitives
or objects. It is of fixed size. We can have multi dimensional arrays.

Array: can store primitive ArrayList: Stores object only

Array: fix size ArrayList: resizable

Array: can have multi dimensional

Array: lang ArrayList: Collection framework

Q: Can you limit the initial capacity of Vector in java?

Yes you can limit the initial capacity. We can construct an empty vector with specified initial capacity

public vector(int size)

Q: What method should the key class of HashMap override?

The methods to override are
equals()
and
hashCode()
.

Q: What is the difference between Enumeration and Iterator?

The functionality of Enumeration interface is duplicated by the Iterator interface. Iterator has a
remove()
method
while Enumeration doesn’t. Enumeration acts as Read-only interface, because it has the methods only to traverse and fetch the objects, whereas using Iterator we can manipulate the objects also like adding and removing the objects. So Enumeration is used when
ever we want to make Collection objects as Read-only.

Q: What goes wrong if the HashMap key has same hashCode value?

It leads to ‘Collision’ wherein all the values are stored in same bucket. Hence, the searching time increases quad radically.

Q: How will you remove duplicate element from a List?

Add the List elements to Set. Duplicates will be removed.

Q: Which java.util classes and interfaces support event handling?

The EventObject class and the EventListener interface support event processing.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: