Welcome to Java4u

A Single Place for all Java Resources

Looking for something?

Subscribe to this blog!

Receive the latest posts by email.

.Just enter your email below if you want to subscribe!

Email

Tuesday, August 31, 2010

Collections FAQs

Q51. What is TreeMap?

 TreeMap can be used to store a group of objects as key-value pairs where all the entries are arranged according to some sorting order of keys.
  • The underlying data structure is RED-BLACK Tree
  • Duplicates keys are not allowed but values can be duplicated.
  • Insertion order is not preserved because insertion is based on some sorting order
  • If we are depending on Natural sorting order then keys should be homogeneous(violation leads to ClassCastException)  but values need not homogeneous
  • In case of customized sorting order we can insert  heterogeneous keys and values
  • For empty TreeMap as first entry with null values are allowed but after inserting that entry if we are trying to insert any other entry we will get NullPointerException
  • For non empty TreeMap if we are trying to insert null keys we will get NullPointerException
  • There are no restrictions for null values.
  Q52. What is Hashtable
     
     Hashtable is a legacy Map and can be used to store objects as key value pairs.
  • The underlying data sturucture is Hashtabe
  • Duplicates keys are not allowed but duplicate values are allowed
  • null insertion is not possible for both keys and values
  • all methods are synchronized
  • insertion order is not preserved because it is  based on hashcode  of keys
  • heterogeneous Objects are allowed for both keys and values
  • introduced in 1.0 version it is legacy class
Q53. What is PriorityQueue?

It represents a data structure to hold group of individual objects prior to processing based on some priority .it can be natural sorting order and it can be customized sorting order described by Comparator.
It is the implementation class of Queue interface.
  • Insertion order is not preserved because here insertion is done based on some sorting order
  • Duplicates are not allowed
  • null insertion is not possible even as first element also
  •  If we are depending on natural sorting order Objects should be homogeneous  violation leads to ClassCastException
  •  If we are depending on customized  sorting order Objects can be heterogeneous also.
Q54. What is Arrays class?
  • It is utility class for arrays.
  • It defines several utility methods for arrays like sorting an array or searching an element in array
  • present in java.util package
Q55. We are planning to do an indexed search in a list of objects. Which of the two Java collections should you use: ArrayList or LinkedList?

ArrayList

Q56. Why ArrayList is faster than Vector?  

All methods present in the Vector are synchronized  and hence  any method can be executed by only one thread at a time. It slows down the execution.
But in ArrayList,  no method is synchronized and hence multiple thread are allowed execute simultaneously which speed up the execution.

<Pre 1 2 3 4 5 6

0 comments: