The Collections class contains static methods to perform common operations in a collection and a list. The section introduced several static methods in the Collections class for array lists. The Collections class contains the sort, binarySearch, reverse, shuffle, copy, and fill methods for lists, and max, min, disjoint, and frequency methods for collections, as shown in Figure below.
You can sort the comparable elements in a list in its natural order with the compareTo method in the Comparable interface. You may also specify a comparator to sort elements. For example, the following code sorts strings in a list.
List
Collections.sort(list);
System.out.println(list);
The output is [blue, green, red].
The preceding code sorts a list in ascending order. To sort it in descending order, you can simply use the Collections.reverseOrder() method to return a Comparator object that orders the elements in reverse of their natural order. For example, the following code sorts a list of strings in descending order.
List
Collections.sort(list, Collections.reverseOrder());
System.out.println(list);
The output is [yellow, red, green, blue].
You can use the binarySearch method to search for a key in a list. To use this method, the list must be sorted in increasing order. If the key is not in the list, the method returns -(insertion point +1). Recall that the insertion point is where the item would fall in the list if it were present. For example, the following code searches the keys in a list of integers and a list of strings.
List
System.out.println("(1) Index: " + Collections.binarySearch(list1, 7));
System.out.println("(2) Index: " + Collections.binarySearch(list1, 9));
List
System.out.println("(3) Index: " +
Collections.binarySearch(list2, "red"));
System.out.println("(4) Index: " +
Collections.binarySearch(list2, "cyan"));
The output of the preceding code is:
(1) Index: 2
(2) Index: -4
(3) Index: 2
(4) Index: -2
You can use the reverse method to reverse the elements in a list. For example, the following code displays [blue, green, red, yellow].
List
Collections.reverse(list);
System.out.println(list);
You can use the shuffle(List) method to randomly reorder the elements in a list. For example, the following code shuffles the elements in list.
List
Collections.shuffle(list);
System.out.println(list);
You can also use the shuffle(List, Random) method to randomly reorder the elements in a list with a specified Random object. Using a specified Random object is useful to generate a list with identical sequences of elements for the same original list. For example, the following code shuffles the elements in list.
List
List
Collections.shuffle(list1, new Random(20));
Collections.shuffle(list2, new Random(20));
System.out.println(list1);
System.out.println(list2);
You will see that list1 and list2 have the same sequence of elements before and after the shuffling.
You can use the copy(det, src) method to copy all the elements from a source list to a destination list on the same index. The destination list must be as long as the source list. If it is longer, the remaining elements in the source list are not affected. For example, the following
code copies list2 to list1.
List
List
Collections.copy(list1, list2);
System.out.println(list1);
The output for list1 is [white, black, green, blue]. The copy method performs a shallow copy: only the references of the elements from the source list are copied.
You can use the nCopies(int n, Object o) method to create an immutable list that consists of n copies of the specified object. For example, the following code creates a list with five Calendar objects.
List
The list created from the nCopies method is immutable, so you cannot add, remove, or update elements in the list. All the elements have the same references.
You can use the fill(List list, Object o) method to replace all the elements in the list with the specified element. For example, the following code displays [black, black, black].
List
Collections.fill(list, "black");
System.out.println(list);
You can use the max and min methods for finding the maximum and minimum elements in a collection. The elements must be comparable using the Comparable interface or the Comparator interface. For example, the following code displays the largest and smallest strings in a collection.
Collection
System.out.println(Collections.max(collection));
System.out.println(Collections.min(collection));
The disjoint(collection1, collection2) method returns true if the two collections have no elements in common. For example, in the following code, disjoint(collection1, collection2) returns false, but disjoint(collection1, collection3) returns true.
Collection
Collection
Collection
System.out.println(Collections.disjoint(collection1, collection2));
System.out.println(Collections.disjoint(collection1, collection3));
The frequency(collection, element) method finds the number of occurrences of the element in the collection. For example, frequency(collection, "red") returns 2 in the following code.
Collection
System.out.println(Collections.frequency(collection, "red"));
The above is the detailed content of Static Methods for Lists and Collections. For more information, please follow other related articles on the PHP Chinese website!