Java_Collection_Framework_Questions
Java_Collection_Framework_Questions
The two main ways to iterate the elements of a collection in Java are:
1. Using Iterator: The Iterator interface provides methods to iterate over any Collection. It
has methods like hasNext(), next(), and remove().
2. Using enhanced for loop (for-each loop): This is a more readable and compact way to
iterate over elements of arrays and collections.
How can we sort the elements of an object? What is the difference between
Comparable and Comparator interfaces?
To sort the elements of an object, you can use:
1. Comparable: The object itself implements the Comparable interface and overrides the
compareTo() method. This allows objects to be compared based on one natural ordering.
2. Comparator: A separate class implements the Comparator interface and overrides the
compare() method. This allows objects to be compared based on multiple orderings.