java4all@1986 java. Powered by Blogger.

>> Saturday, June 25, 2011

54.Difference between ArrayList and Vector ?
A)
.ArrayList vs    Vector
ArrayList is NOT synchronized by default.     Vector List is synchronized by default.
ArrayList can use only Iterator to access the elements.     Vector list can use Iterator and Enumeration Interface to access the elements.
The ArrayList increases its array size by 50 percent if it runs out of room.     A Vector defaults to doubling the size of its array if it runs out of room
ArrayList has no default size.     While vector has a default size of 10.

55.How to obtain Array from an ArrayList ?

Array can be obtained from an ArrayList using toArray() method on ArrayList.

    List arrayList = new ArrayList();
      arrayList.add(…

    Object  a[] = arrayList.toArray();


56.Why insertion and deletion in ArrayList is slow compared to LinkedList ?
A). 
   ArrayList internally uses and array to store the elements, when that array gets filled by inserting elements a new array of roughly 1.5 times the size of the original array is created and all the data of old array is copied to new array.
    During deletion, all elements present in the array after the deleted elements have to be moved one step back to fill the space created by deletion. In linked list data is stored in nodes that have reference to the previous node and the next node so adding element is simple as creating the node an updating the next pointer on the last node and the previous pointer on the new node. Deletion in linked list is fast because it involves only updating the next pointer in the node before the deleted node and updating the previous pointer in the node after the deleted node.


57.Why are Iterators returned by ArrayList called Fail Fast ?
A).
Because, if list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

58.How do you decide when to use ArrayList and When to use LinkedList?
A)
.If you need to support random access, without inserting or removing elements from any place other than the end, then ArrayList offers the optimal collection. If, however, you need to frequently add and remove elements from the middle of the list and only access the list elements sequentially, then LinkedList offers the better implementation.

59.What is the Set interface ?
 A).
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

60.What are the main Implementations of the Set interface ?
A).
The main implementations of the List interface are as follows:

    HashSet
    TreeSet
    LinkedHashSet
    EnumSet

61.What is a HashSet ?
A).
A HashSet is an unsorted, unordered Set.
    It uses the hashcode of the object being inserted (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.

62.What is a TreeSet ?
A).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.

63.What is an EnumSet ?
A)
.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.

64.Difference between HashSet and TreeSet ?
A)
.HashSet vs    TreeSet
HashSet is under set interface i.e. it  does not guarantee for either sorted order or sequence order.     TreeSet is under set i.e. it provides elements in a sorted  order (acceding order).
We can add any type of elements to hash set.     We can add only similar types
of elements to tree set.


65.What is a Map ?
A)
.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.


66.What are the main Implementations of the Map interface ?
A).
The main implementations of the List interface are as follows:

    HashMap
    HashTable
    TreeMap
    EnumMap


67.What is a TreeMap ?
A).
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.

68.How do you decide when to use HashMap and when to use TreeMap ?
A)
.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.

69.Difference between HashMap and Hashtable ?
A).HashMap
vs     Hashtable
HashMap lets you have null values as well as one null key.     HashTable  does not allows null values as key and value.
The iterator in the HashMap is fail-safe (If you change the map while iterating, you’ll know).     The enumerator for the Hashtable is not fail-safe.
HashMap is unsynchronized.     Hashtable is synchronized.

Note: Only one NULL is allowed as a key in HashMap. HashMap does not allow multiple keys to be NULL. Nevertheless, it can have multiple NULL values.


70.How does a Hashtable internally maintain the key-value pairs?
A).
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.

0 comments:

Post a Comment

FaceBook Login

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

Total Pageviews

STATCOUNTER

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP