Java Collection Framework Question and Answers

13. What is the difference between Comparable and Comparator?
  Comparable:
  • Comparable provides only one sort of sequence.
  • It provides one method named compareTo().
  • It is found in java.lang.package.
  • If we implement Comparable interface, actual class is modified.
Comparator:
  • Comparator provides multiple sorts of sequences.
  • It provides one method named compare().
  • It is found in java.util.package.
  • Actual class is not modified.
 
Your Name Your Email-ID
Your Answer
14. What is an iterator?
 
  • Iterator interfaces provides methods to iterate over any Collection.
  • We can get iterator instance from a Collection using iterator() method.
  • Iterator takes the place of Enumeration in the Java Collections Framework.
  • Iterators allow the caller to remove elements from the underlying collections during the iteration.
  • Java Collection iterator provides a generic way for traversal through the elements of a collection and implements Iterator Design Pattern.
 
Your Name Your Email-ID
Your Answer
15. What is the dictionary class?
 
  • The Dictionary class is an abstract class.
  • The class maps keys to values.
  • The classes such as Hash Tables are the sub classes of the abstract class Dictionary.
  • The key and values are objects.
  • The key and value are non null objects.
  • The Dictionary Class provides the capability to store key value pairs
 
Your Name Your Email-ID
Your Answer
16. What is the difference between enumeration and iterator interface?
 
  • Enumeration is twice as fast as Iterator and uses very less memory.
  • Enumeration is very basic and fits to basic needs.
  • But Iterator is much safer as compared to Enumeration because it always denies other threads to modify the collection objects which is being iterated by it.
  • Iterator takes the place of Enumeration in the Java Collections Framework.
  • Iterators allow the caller to remove elements from the underlying collection that is not possible with Enumeration.
  • Iterator method names have been improved to make its functionality clear.
 
Your Name Your Email-ID
Your Answer
12345 Page 4 of 5