Java Reference
In-Depth Information
Table 11.3
Useful Static Methods of the Collections Class
Method
Description
binarySearch(list, value) Searches a sorted list for a given element
value and returns its index
copy(destinationList, sourceList) Copies all elements from the source list to the
destination list
fill(list, value) Replaces every element in the given list with
the given value
max(list) Returns the element with the highest value
min(list) Returns the element with the lowest value
replaceAll(list, oldValue, newValue) Replaces all occurrences of the old value with
the new value
Reverses the order of the elements in the
given list
reverse(list)
Shifts each element to the right by the given
number of indexes, moving the final element
to the front
rotate(list, distance)
Rearranges the elements into random order
shuffle(list)
Rearranges the elements into sorted (nonde-
creasing) order
sort(list)
Switches the element values at the given two
indexes
swap(list, index1, index2)
method even more general by having it accept a parameter of type Collection
rather than List , since every collection has an iterator method.
The java.util package has a class called Collections that contains several
useful methods related to all collections. (Note that this class is not the same as the
Collection interface that many collection classes implement.) The Collections
class contains static methods that operate on lists. These methods' headers specify
parameters of type List rather than LinkedList or ArrayList . The methods perform
common tasks on lists, such as sorting, shuffling, and searching. Table 11.3 presents a
short list of useful methods from the Collections class that operate on lists.
Notice that these methods are static, so they must be called by writing the word
Collections followed by a dot and the method's name. For example, if you had a
LinkedList variable called list and you wanted to reverse the list's contents,
you'd write
Collections.reverse(list);
In addition to List , there are several other interfaces, such as Queue , Set , and
Map , representing ADTs in the Java Collections Framework. We'll explore several of
them in this chapter.
 
Search WWH ::




Custom Search