"Here I am."
"I've been waiting for you here for a long time."
"I hope so. Let's carry on, then."
"I'm going to introduce you to present you a super-duper full-featured utility class for collections."
"I'm already shaking all over with excitement."
"Great. Then let's get started. Here are the methods of the Collections class:"
Code | Explanation |
---|---|
|
Adds the passed elements to the passed collection. This method can be conveniently called like this: Collections.addList (list, 10,11,12,13,14,15) |
|
Returns a «normal queue» made from a deque. |
|
Performs a binary search for the key element in the list. The list must be sorted. You can specify a comparator to compare the elements. |
|
Checks that all elements of collection c are of a certain type. Similar methods exist for List, Map, Set, and SortedSet. |
|
Copies the src list to the dest list. |
|
Checks that the collections do not contain common elements |
|
Fills list with the element obj |
|
Counts how many times object o exists in collection c |
|
Determines the index of the first occurrence of the target list within the source list. The principle is similar to String.indexOf("some string") |
|
Determines the index of the last occurrence of the target list within the source list. Similar to String.lastIndexOf("some string") |
|
Searches for the maximum number/value in a collection. How do we find the maximum of 6 numbers? Collections.max(Arrays.asList(51, 42, 33, 24, 15, 6)); |
|
Searches for the minimum value in a collection. |
|
Creates a dummy collection in which the o element appears n times. |
|
Replaces all oldVal elements in the list with newVal elements |
|
Reverses the list. |
|
Randomly shuffles the elements of the list. |
|
Returns an immutable list consisting of one passed element. Similar methods exist for Map, Set, and SortedSet. |
|
Sorts the list in ascending order. |
|
Swaps the elements of the list |
|
Wraps this collection in a synchronized counterpart. Similar methods exist for List, Map, Set, and SortedSet. |
"Whoa! Yeah, this is a whole arsenal, and I've hardly used any of it."
"Actually, this is where my lesson ends today."
"Take a look at these methods, and find the ones you are most likely to use."
"Or better yet, study them. They will be useful to you."
"Thank you, Rishi. I'll go study."
GO TO FULL VERSION