Chapter 17: java.util Part 1: The Collections Framework 477
Method Description
static int frequency(Collection<?>c, Objectobj) Counts the number of occurrences ofobjincand
returns the result.
static int indexOfSubList(List<?>list,
List<?>subList)
Searcheslistfor the first occurrence ofsubList.
Returns the index of the first match, or –1 if no
match is found.
static int lastIndexOfSubList(List<?>list,
List<?>subList)
Searches list for the last occurrence ofsubList.
Returns the index of the last match, or –1 if no
match is found.
static <T>
ArrayList<T> list(Enumeration<T>enum)
Returns anArrayListthat contains the elements
ofenum.
static <T> T max(Collection<? extends T>c,
Comparator<? super T>comp)
Returns the maximum element incas determined
bycomp.
static <T extends Object &
Comparable<? super T>>
T max(Collection<? extends T>c)
Returns the maximum element incas determined
by natural ordering. The collection need not be
sor ted.
static <T> T min(Collection<? extends T>c,
Comparator<? super T>comp)
Returns the minimum element incas determined
bycomp.The collection need not be sor ted.
static <T extends Object &
Comparable<? superT>>
T min(Collection<? extends T>c)
Returns the minimum element incas determined
by natural ordering.
static <T> List<T> nCopies(intnum, Tobj) Returnsnumcopies ofobjcontained in an immutable
list.nummust be greater than or equal to zero.
static <E> Set<E> newSetFromMap(Map<E, Boolean>m) Creates and returns a set backed by the map
specified bym, which must be empty at the time
this method is called. (Added by Java SE 6.)
static <T> boolean replaceAll(List<T>list,
Told, Tnew)
Replaces all occurrences ofoldwithnewinlist.
Returnstrueif at least one replacement occurred.
Returnsfalse, other wise.
static void reverse(List<T>list) Reverses the sequence inlist.
static <T> Comparator<T>
reverseOrder(Comparator<T>comp)
Returns a reverse comparator based on the one
passed incomp.That is, the returned comparator
reverses the outcome of a comparison that uses
comp.
static <T> Comparator<T> reverseOrder( ) Returns a reverse comparator, which is a
comparator that reverses the outcome of a
comparison between two elements.
static void rotate(List<T>list, intn) Rotateslistbynplaces to the right. To rotate left,
use a negative value forn.
static void shuffle(List<T>list, Randomr) Shuffles (i.e., randomizes) the elements inlistby
usingras a source of random numbers.
static void shuffle(List<T>list) Shuffles (i.e., randomizes) the elements inlist.
static <T> Set<T> singleton(Tobj) Returnsobjas an immutable set. This is an easy
way to conver t a single object into a set.
static <T> List<T> singletonList(Tobj) Returnsobjas an immutable list. This is an easy
way to conver t a single object into a list.
static <K, V> Map<K, V>
singletonMap(Kk, Vv)
Returns the key/value pairk/vas an immutable
map. This is an easy way to conver t a single key/
value pair into a map.
TABLE 17-14 The Algorithms Defined byCollections(continued)