Java Collections Class Methods
Collections Methods
The Collections
class (in java.util
) contains
several useful methods for working with collections like ArrayList
,
HashSet
, and HashMap
.
Method | Description | Return Type |
---|---|---|
sort() | Sorts the list in ascending order (according to natural ordering) | void |
binarySearch() | Searches for a key in a sorted list and returns its index (otherwise < 0 ) |
int |
reverse() | Reverses the order of elements in a list | void |
reverseOrder() | Returns a comparator that imposes the reverse of natural ordering | Comparator |
shuffle() | Randomly permutes the elements of a list | void |
swap() | Swaps the elements at the specified positions in a list | void |
frequency() | Returns the number of elements equal to the specified object | int |
max() | Returns the maximum element of a collection (according to natural ordering) | T |
min() | Returns the minimum element of a collection (according to natural ordering) | T |
disjoint() | Checks if two collections have no elements in common | boolean |
copy() | Copies all elements from one list into another | void |
fill() | Replaces all elements of the list with the specified object | void |
replaceAll() | Replaces all occurrences of one value with another in a list | boolean |
unmodifiableList() | Returns an unmodifiable (read-only) view of the specified list | List |
unmodifiableSet() | Returns an unmodifiable (read-only) view of the specified set | Set |
unmodifiableMap() | Returns an unmodifiable (read-only) view of the specified map | Map |