1) Explain about Java Collections API?
Java Collections Framework provides a set of interfaces and classes that support operations on a collections of objects.
Java Collections Framework provides a set of interfaces and classes that support operations on a collections of objects.
2) 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.
3) What is the List interface?
The List interface provides support for ordered collections of objects.
4) What is the difference between set and list?
Set stores elements in an unordered way but does not contain duplicate elements, where as List stores elements in an ordered way but may contain duplicate elements.
5) What is map interface in a java?
Map is
an object that maps keys to values. A map cannot contain duplicate
keys: Each key can map to at most one value. This Map permits null value
6) What is the difference between Map and Hashmap?
Map is Interface and Hashmap is class that implements that
7) What is the difference between a HashMap and a Hashtable in Java?
- Hashtable is synchronized, whereas HashMap is not. This makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.
- Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values.
- One of HashMap's subclasses is LinkedHashMap, so in the event that you'd want predictable iteration order (which is insertion order by default), you could easily swap out the HashMap for aLinkedHashMap. This wouldn't be as easy if you were using Hashtable.
8) What is a vector in Java?
Vector implements a dynamic array. It is similar to ArrayList, but with two differences: Vector is synchronized, and it contains many legacy methods that are not part of the collections framework.
9) What is ArrayList In java?
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.
10) Difference between Vector and ArrayList?
Vector
& ArrayList both classes are implemented using dynamically
resizable arrays, providing fast random access and fast traversal.
ArrayList and Vector class both implement the List interface.
1) 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.
2) 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.
11) What is an Iterator interface?
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.
12) what is Enumeration in java?
An
enumeration is an object that generates elements one at a time, used
for passing through a collection, usually of unknown size. The traversing of elements can only be done once per creation.
13) What is difference between Iterator and Enumeration?
Both
Iterator and Enumeration are used to traverse Collection objects, in a
sequential fashion. Enumeration can be applied to Vector and HashTable.
Iterator can be used with most of the Collection objects.
The
main difference between the two is that Iterator is fail-safe. i.e, If
you are using an iterator to go through a collection you can be sure of
no concurrent modifications in the underlying collection which may
happen in multi-threaded environments.
14) What is the Properties class?
The
properties class is a subclass of Hashtable that can be read from or
written to a stream. It also provides the capability to specify a set of
default values to be used.
15) What is difference between HashMap and HashSet?
HashSet : HashSet
does not allow duplicate values. It provides add method rather put
method. You also use its contain method to check whether the object is
already available in HashSet. HashSet can be used where you want to
maintain a unique list.
HashMap : It allows null for both key and value. It is unsynchronized. So come up with better performance
16 ) What is the Difference between the Iterator and ListIterator?
Iterator :
Iterator takes the place of Enumeration in the Java collections
framework. One can traverse throughr the the collection with the help of
iterator in forward direction only and Iterators allow the caller to
remove elements from the underlying collection during the iteration with
well-defined semantics
ListIterator: An
iterator for lists that allows one to traverse the list in either
direction.modify the list during iteration, and obtain the iterator's
current position in the list. A ListIterator has no current element. its
cursor position always lies between the element that would be returned
by a call to previous() and the element that would be returned by a call
to next(). In a list of length n, there are n+1 valid index values,
from 0 to n, inclusive.
17) How to Make a Map or List as Thread-Safe or Synchronized Collection?
Collections.synchronizedMap(new HashMap());
Collections.synchronizedList(List<T> list)
18)
What method should the key class of Hashmap override?
The methods to override are equals() and hashCode().
19) How to convert a string array to arraylist?
new ArrayList(Arrays.asList(myArray));
20) What is TreeSet ?
TreeSet -
It is the implementation of SortedSet interface.This implementation
provides guaranteed log(n) time cost for the basic operations (add,
remove and contains). The class is not synchronized.
No comments:
Post a Comment