Thursday, November 29, 2012

Collections questions

Differences between Iterator and Enumeration?
Iterator duplicate functionality of Enumeration with one addition of remove() method and both provide navigation functionally on objects of Collection.

Difference between Set and List in Java?
In Set the order of the elements in not saved.Set doesn't allowed duplicate while List does and List maintains insertion order while Set does not. Set uses the method equals() internally, not ==.

How do you Sort objects on collection?
The class Collections has functionality for sorting collections. There are two most used methods for this:
Collections.sort() - called without Comparator. It uses Comparable interface in Java. The order is the default , which is specified by CompareTo method;
Collections.sort(Comparator) will sort objects based on compare() method of Comparator. See Sorting in Java using Comparator and Comparable for more details.


No comments:

Post a Comment