1. List of methods
Remember that Java's creators wrote a whole helper class called Arrays
for our convenience when working with arrays?
They did the same thing for collections. Java has a java.util.Collections
class that has a lot of methods that are useful for working with collections. Here are just the most interesting ones:
Methods | Description |
---|---|
|
Adds the elements e1 , e2 , e3 , ... to the colls collection |
|
Replaces all the elements in the passed list with obj |
|
Returns a list of n copies of the obj object |
|
Replaces all instances of oldVal with newVal in the list list |
|
Copies all elements from the src list to the dest list |
|
Reverses the list. |
|
Sorts the list in ascending order |
|
Cyclically shifts the elements of the list list by n elements |
|
Randomly shuffles the elements in the list |
|
Finds the minimum element in the colls collection |
|
Finds the maximum element in the colls collection |
|
Determines how many times the obj element occurs in the colls collection |
|
Searches for key in a sorted list and returns the corresponding index. |
|
Returns true if the collections have no elements in common |
Many of these methods don't use the ArrayList
, HashSet
and HashMap
classes per se, but with the corresponding interfaces: Collection<T>
, List<T>
, Map<K, V>
.
This is not a problem: if a method accepts a List<T>
, you can always pass it an ArrayList<Integer>
, but the assignment operator does not work in the opposite direction.
2. Creating and modifying collections
Collections.addAll(Collection<T> colls, T e1, T e2, T e3, ...)
method
The addAll()
method adds the elements e1
, e2
, e3
, ... to the colls
collection Any number of elements can be passed.
Code | Console output |
---|---|
|
|
Collections.fill(List<T> list, T obj)
method
The fill()
method replaces all the elements of the list
collection with the obj
element.
Code | Console output |
---|---|
|
|
Collections.nCopies(int n, T obj)
method
The nCopies()
method returns a list of n
copies of the obj
element. Note that the returned list is immutable, which means you cannot change it! You can only use it to read values:
Code | Description |
---|---|
|
Create an immutable list of 5 Hello stringsCreate a mutable list and fill it with the values from the immutableList list.Console output:
|
Collections.replaceAll (List<T> list, T oldValue, T newValue)
method
The replaceAll()
method replaces all the elements in the list
collection equal to oldValue
with newValue
.
Code | Console output |
---|---|
|
|
Collections.copy (List<T> dest, List<T> src)
method
The copy()
method copies all the elements of the src
collection into the dest
collection.
If the dest
collection starts out longer than the src
collection, then the remaining elements of the dest
collection will remain intact.
dest
collection must be at least as long as the src
collection (otherwise, an IndexOutOfBoundsException
will be thrown).
Code | Console output |
---|---|
|
|
3. Order of the elements
Collections.reverse(List<T> list)
method
The reverse()
method reverses the order of the elements of the passed list.
Code | Console output |
---|---|
|
|
Collections.sort(List<T> list)
method
The sort()
method sorts the passed list in ascending order.
Code | Console output |
---|---|
|
|
Collections.rotate(List<T> list, int distance)
method
The rotate()
method cyclically shifts the elements of the passed list by distance
positions forward.
Code | Console output |
---|---|
|
|
Collections.shuffle(List<T> list)
method
The shuffle()
method randomly shuffles all the elements of the passed list. The result is different every time.
Code | Console output |
---|---|
|
|
4. Finding elements in collections
Collections.min(Collection<T> colls)
method
The min()
method returns the minimum element in the collection.
Code | Console output |
---|---|
|
|
Collections.max(Collection<T> colls)
method
The max()
method returns the maximum element in the collection.
Code | Console output |
---|---|
|
|
Collections.frequency(Collection<T> colls, T obj)
method
The frequency()
method counts the number of times the obj
element occurs in the colls
collection
Code | Console output |
---|---|
|
|
Collections.binarySearch(Collection<T> colls, T obj)
method
The binarySearch()
method searches for the obj
element in the colls
collection. Returns the index of the found element. Returns a negative number if the element is not found.
binarySearch()
method, the collection must be sorted (use Collections.sort()
).
Code | Console output |
---|---|
|
|
Collections.disjoint(Collection<T> coll1, Collection<T> coll2)
method
The disjoint()
method returns true
if the passed collections do not have any elements in common.
Code | Console output |
---|---|
|
|
GO TO FULL VERSION