Java collection plays a very important role in Java, where its main role is to reverse the order of the number of elements that are present in the form of a list. Java collection for sorting and reversing the elements supports the util method, which is further used to make the reverse method work with some help and making. Java collections reverse method throws an exception in case that throws some UnsupportedOperationException whether it exists or not. It doesn’t matter as it is without support for any operation of the set. Reversing a list and then making the elements in a proper reversed order is important.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax of Java Collections reverse.
There exists a proper syntax for the Java Collections Reverse () method that plays an important role as part of the Reverse class, as represented below:
public static <K> ComparatorOrigin<K> reverseOrder()
Declaration of the reverse method is done in this format where the Java collections class is used to get a comparatorOrigin which will be used in order to bring all the elements within a list in a specific order, i.e. reverse order.
public static <L> ComparatorOrigin<L> reverseOrder (Comparator<L> comp_0)
Declaration of the reverse order method is used to make the comparator impose this in natural order with a collection of the object that has reverse comparator ordering in a specific manner. Comp_0 acts as an optional argument in the list.
Java collections include many methods but again depend upon the requirement similar to the reverse method (), having significance for each.
Java collections reverse method works as follows:
Given below are the examples of Java Collections reverse:
This example shows the list of flowers defined in a particular fashion where the original flower comes out to be like this, and the reverse order after applying the reverse() method comes out to be like this, as shown in the output below.
Code:
import java.util.*; class Revrse_Order_Exmpl { public static void main(String[] args) { List<String> mylist_0 = new ArrayList<String>(); mylist_0.add("Rose_flower"); mylist_0.add("Jasmine_Flower"); mylist_0.add("Lily_Flower"); mylist_0.add("Velvet_Flower"); System.out.println("Outcome_of_original_list:- " + mylist_0); Collections.reverse(mylist_0); System.out.println("Outcome_of_Modified List:- /n " + mylist_0); } }
Output:
This program demonstrates the reverse () method functionality with the array of integers where they get reversed once they apply the reverse() method on it, as shown in the output.
Code:
import java.util.*; class Revrse_Order_Exmpl_2 { public static void main(String[] args) { Integer arr_5[] = {34, -10, 60, -70, 12, 16}; System.out.println("Actual_defined_array : " +Arrays.toString(arr_5)); Collections.reverse(Arrays.asList(arr_5)); System.out.println("Modified_defined_Array : " + "\n"+Arrays.toString(arr_5)); } }
Output:
This program demonstrates the reverse method () to be used with the list of integers, which reverses the list of elements as shown in the output.
Code:
import java.util.*; class Revrse_Order_Exmpl_3 { public static void main(String[] args) { List<Integer> list_0 = new ArrayList<Integer>(); list_0.add(10); list_0.add(9); list_0.add(8); list_0.add(5); System.out.println("Actual_list_before:- " + list_0); Collections.reverse(list_0); System.out.println("Modified_list_after:- " + list_0); } }
Output:
This program displays the inbuild reverse() method present as part of the StringBuilder class but not as a string buffer class, due to which this needs conversion to append it into a string call, as shown in the output below.
Code:
import java.util.*; class Revrse_Order_Exmpl_Strng { public static void main(String[] args) { String input_0 = "Weather_seems_nice"; StringBuilder input_a = new StringBuilder(); input_a.append(input_0); input_a.reverse(); System.out.println(input_a); } }
Output:
This program displays the string buffer with a string that applies a reverse() method and displays the following as shown in the output.
Code:
import java.util.*; class Revrse_Order_Exmpl_Strng_buff { public static void main(String[] args) { StringBuffer strng_bf = new StringBuffer("World_is_in_misery."); System.out.println("Print_the_string_bbuffer = " + strng_bf); strng_bf.reverse(); System.out.println("Strng_buffer_after_revversal = " + strng_bf); } }
Output:
Java collection reference has many advantages and plays a pivotal role in any java data structure. It helps make the entire collection framework handle many strings, characters, and integers, which helps in further implementation and results look as desired by the developers and users. It helps in making the entire Java useful.
The above is the detailed content of Java Collections reverse. For more information, please follow other related articles on the PHP Chinese website!