The java.util.Collections class provides a number of methods that operate on collections (in most cases, lists). We will describe two such methods, sort and reverse,
in this section. The sort method sorts a list into the natural ascending order of its elements. Suppose we have created the following list:
List c = new ArrayList(); c.add("Smith");
c.add(''Jones");
c.add("Smith");
c.add("Brown");
c.add("Able");
then the statement
Collections.sort(c);
will sort the List c as follows:
Able Brown Jones Smith Smith
Like SortedSets, the sort method uses the String class implementation of the Comparable interface compareTo method to sort the Strings in lexicographic order. Similarly, if the collection contains Dates, then the Date class implementation of the Comparable interface compareTo method is used to sort the Dates in chronological order.
The reverse method reverses the order of elements in a list. For example, the statement
Collections.reverse(c);
will reverse the List c as follows:
Able Brown Smith Jones Smith
The Collections Class
Tags
# Collections
# The Collections Class
Share This
The Collections Class
Labels:
Collections,
The Collections Class
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment