Friday 24 May 2013

JAVA INTERVIEW QUESTIONS - Set interface


1.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
  • 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
2.What are the main Implementations of the Set interface ?
The main implementations of the List interface are as follows:
  • HashSet
  • TreeSet
  • LinkedHashSet
  • EnumSet
3.What is a HashSet ?
  • 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.
4.What is a TreeSet ?
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.

5.What is an EnumSet ?
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.

6.Difference between HashSet and TreeSet ?
HashSetTreeSet
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.

JAVA INTERVIEW QUESTIONS - Map interface











JAVA INTERVIEW QUESTIONS - Iterator



EmoticonEmoticon