Containers

We are hosting this site in another domain. Checkout

http://www.techpitcher.com/java/container.html


1. What do you know about Containers?
There are two types of container library available in java .
i. Collection: Collection category only holds one item in each location. Which includes List and Set. ArrayList is type of list and HashSet is type of Set.
The name is misleading, because entire container libraries are often called “collections”.
a. List (Interface)
Order is most important feature of List
List allows insertion and deletion of element in the middle.We can traverse List in both directions.
1.ArrayList:
ArrayList allows random access of elements.
It is slow when inserting and removing elements from the middle.
2. LinkedList :
Provides optimal sequential access.
Inexpensive insertions and deletions from the middle of the List.
Relatively slow for sequential access.
b. Set (Interface)
Element in set are unique.
Set doesn’t guarantee that it will maintain any order.
1. HashSet:
Provides fast lookup
ii. Map: The Map holds key-value pairs, rather like a mini database. We can lookup on the basis of key.
1. HashMap:
Implementation based on Hash table.
Provides constant time performance for inserting and location pairs.
-----------------------------------------------------------------------------
2. Difference between HashTable and HashMap?
The HashTable is almost equal to HashMap except following points.
  1. HashTable is synchronized, while HashMap is not.
  2. HashTable doesn’t permit null as key while in HashMap it is allowed.
  3. HashTable guarantee that order of the table will remain constant over time while in HashMap it is not.
-----------------------------------------------------------------------------

3. What are the differences between Vector and ArrayList?
The two classes are very similar, however there are few differences, which can give one a clear idea about their usages.Vector is synchronized. Content in vector is thread-safe. ArrayList is not synchronized hence its content is not thread-safe. Synchronization is not at free of cost. It affects vector’s performance. So if we don’t require thread-safe data, we should use ArrayList.
Internally, both the ArrayList and vector hold data in Array. When we add a new element into ArrayList or Vector, and the object will need to expand its internal array if it runs out of memory. Vector doubles the size of Array by default, while the ArrayList increases its size by 50 percent. Vector does have slight advantage since they allow to reset increment size.Addition and deletion operation in both classes takes same time.
Addition at the end of container can be performed in constant time O(1). Deletion and Addition are more expansive when the element is added/removed from middle, it takes O(n-i). Here n is number of elements in the container and i is index of the element.
-----------------------------------------------------------------------------

You can also find links related to these topics on ads section provided by Google.
Please provide your comment to make this forum better.

No comments: