Static Methods for Lists and Collections

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, revers…


This content originally appeared on DEV Community and was authored by Paul Ngugi

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.

Image description

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<String> list = Arrays.asList("red", "green", "blue");
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<String> list = Arrays.asList("yellow", "red", "green", "blue");
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<Integer> list1 = Arrays.asList(2, 4, 7, 10, 11, 45, 50, 59, 60, 66);
System.out.println("(1) Index: " + Collections.binarySearch(list1, 7));
System.out.println("(2) Index: " + Collections.binarySearch(list1, 9));

List<String> list2 = Arrays.asList("blue", "green", "red");
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<String> list = Arrays.asList("yellow", "red", "green", "blue");
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<String> list = Arrays.asList("yellow", "red", "green", "blue");
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<String> list1 = Arrays.asList("yellow", "red", "green", "blue");
List<String> list2 = Arrays.asList("yellow", "red", "green", "blue");
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<String> list1 = Arrays.asList("yellow", "red", "green", "blue");
List<String> list2 = Arrays.asList("white", "black");
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<GregorianCalendar> list1 = Collections.nCopies(5, new GregorianCalendar(2005, 0, 1));

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<String> list = Arrays.asList("red", "green", "blue");
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<String> collection = Arrays.asList("red", "green", "blue");
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<String> collection1 = Arrays.asList("red", "cyan");
Collection<String> collection2 = Arrays.asList("red", "blue");
Collection<String> collection3 = Arrays.asList("pink", "tan");
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<String> collection = Arrays.asList("red", "cyan", "red");
System.out.println(Collections.frequency(collection, "red"));


This content originally appeared on DEV Community and was authored by Paul Ngugi


Print Share Comment Cite Upload Translate Updates
APA

Paul Ngugi | Sciencx (2024-07-09T21:12:05+00:00) Static Methods for Lists and Collections. Retrieved from https://www.scien.cx/2024/07/09/static-methods-for-lists-and-collections/

MLA
" » Static Methods for Lists and Collections." Paul Ngugi | Sciencx - Tuesday July 9, 2024, https://www.scien.cx/2024/07/09/static-methods-for-lists-and-collections/
HARVARD
Paul Ngugi | Sciencx Tuesday July 9, 2024 » Static Methods for Lists and Collections., viewed ,<https://www.scien.cx/2024/07/09/static-methods-for-lists-and-collections/>
VANCOUVER
Paul Ngugi | Sciencx - » Static Methods for Lists and Collections. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/09/static-methods-for-lists-and-collections/
CHICAGO
" » Static Methods for Lists and Collections." Paul Ngugi | Sciencx - Accessed . https://www.scien.cx/2024/07/09/static-methods-for-lists-and-collections/
IEEE
" » Static Methods for Lists and Collections." Paul Ngugi | Sciencx [Online]. Available: https://www.scien.cx/2024/07/09/static-methods-for-lists-and-collections/. [Accessed: ]
rf:citation
» Static Methods for Lists and Collections | Paul Ngugi | Sciencx | https://www.scien.cx/2024/07/09/static-methods-for-lists-and-collections/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.