Heim  >  Artikel  >  Java  >  Java-Sammlungen umgekehrt

Java-Sammlungen umgekehrt

王林
王林Original
2024-08-30 15:46:371168Durchsuche

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.

How Does Java Collections Reverse Method Works?

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:

  • Java reverse () method is present in the Java.util package as part of the collection as a data structure in Java.
  • the reverse() method is used for reversing the elements present in the list, which means it is mainly used for making the order of elements all the way, starting from the right side to the left.
  • It follows the order to ease the search and implementation of the elements in a specified way.
  • the reverse() method is a method that is used as a static method for accessing the elements through its class name, thereby making the class with data structure properly.
  • If the scenario comes where the class object present within the method is not proper, it will definitely throw an error. Other than that, if it is correct, then the accessibility is quite easy.
  • Java reverse method() supports unwanted exceptions also, which might arise if there is something unwanted present within the class object, and will clearly acknowledge at the time of reversing the elements present in the list with an order.
  • It throws an exception of UnsupportedOperationException, which gives a list for the un-support operation of any set.
  • Coming to the return type of the method, it returns void, which means nothing.
  • The compatibility version for the proper working of the reverse method () includes Java 1.5 version and above, making it completely suitable.
  • Suppose there is a string buffer class in case there is no possibility to perform the reverse() method directly on the string class; some manipulations need to be performed to bring in reverse method() into the picture with the String class. First, it is required to convert the input string into String builder, followed by which input string needs to be appended into the string buffer using the append method. Once it is achieved, all the characters will be present with the reverse string last of the element.
  • Thus it can be said that the reverse() method is supported as default in the string builder class but not in the string class as a whole.

Examples of Java Collections reverse.

Given below are the examples of Java Collections reverse:

Example #1

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:

Java-Sammlungen umgekehrt

Example #2

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:

Java-Sammlungen umgekehrt

Example #3

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:

Java-Sammlungen umgekehrt

Example #4

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:

Java-Sammlungen umgekehrt

Example #5

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-Sammlungen umgekehrt

Conclusion

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.

Das obige ist der detaillierte Inhalt vonJava-Sammlungen umgekehrt. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:Java-SammlungskarteNächster Artikel:Java-Sammlungskarte