Java Collections sort() Method21 Mar 2025 | 3 min read The sort() method of Java Collections class is used to sort the elements presents in the specified list. There is two different types of Java sort() method which can be differentiated depending on its parameter. These are:
Java Collections sort(list) MethodThis method is used to sort the elements presents in the specified list of collection in ascending order. Java Collections sort(list, comp) MethodThis method is used to sorts the specified list according to the order introduced by the specified comparator. SyntaxFollowing is the declaration of sort() method: Parameter
ReturnsThe sort() method does not return anything. ExceptionsUnsupportedOperationException- This method thrown exception if the specified list or its list-iterator does not support the set operation. ClassCastException- This method thrown exception if the list contains elements that are not mutually comparable (for example, strings and integers). Example 1Output: Specified value before sort: [Java, Python, Android, One, Ruby, Node.js] Specified value after sort: [Android, Java, Node.js, One, Python, Ruby] Example 2Output: Value before sort: [Java, Python, Android, One, Ruby, Node.js] Sort Value according to specified comparator: [Ruby, Python, One, Node.js, Java, Android] Example 3Output: 101 201 230 Example 4Output: Data before sorted- 101 Java USA 103 Ruby China 102 Android India Data after sorted by rollno- 101 Java USA 102 Android India 103 Ruby China Next TopicJava Collections Class |
Java Collections Method The method of Java Collections class is used to get a synchronized (thread-safe) map backed by the specified map. Syntax Following is the declaration of method: public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m) Parameter Parameter Description Required/Optional m It is the map which will be wrapped in a synchronized map. Required Returns The...
2 min read
Java Collections Method The is a Java Collections class method which returns the maximum value for the given inputs. All elements in the collection must implement the Comparable interface. There is two different types of Java method which can be differentiated depending on...
3 min read
Java Collections Method The method of Java Collections class is used to rotate the elements in the specified list by a given distance. Syntax Following is the declaration of method: public static void rotate(List<?> list, int distance) Parameter Parameter Description Required/Optional list It is the list which will be rotated. Required distance It is...
3 min read
Java Collections Method The is a method of Java Collections class which adds all of the specified elements to the specified collection. The elements to be added may be specified individually or as an array. Syntax Following is the declaration of method: public static <T> boolean...
2 min read
Java Collections Method The method of Java Collections class is used to get a List that has no elements. These empty list are immutable in nature. The Collections. method in Java, part of the java.util.Collections class, returns an immutable empty List. This means that the...
5 min read
Java Collections Method The method of Java Collections class is used to get a synchronized (thread-safe) sorted set backed by the specified sorted set. Syntax Following is the declaration of method: public static <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s) Parameter Parameter Description Required/Optional s It is the sorted set which will be wrapped in...
3 min read
Java Collections Method The is a method of Java Collections class which is used to get the enumeration over the specified collection. Syntax Following is the declaration of method: public static <T> Eenumeration<T> enumeration(Collection<T> c) Parameter Parameter Description Required/Optional c It is a collections for which enumeration is to be returned. Required Returns The ...
3 min read
Java Collections Method The method of Java Collections class copies all of the elements from one list into another list. In this method, the size of the destination list must be greater than or equal to the size of the source list. Syntax Following is the...
3 min read
Java Collections Method The is a method of Java Collections class which returns a dynamically typesafe view of the specified collection. If an element inserted of the wrong type, it will result in an immediate ClassCastException. Syntax Following is the declaration of method: public static <E>...
2 min read
Java Collections Method The method of Java Collections class is used to replace all occurrences of one specified value in a list with the other specified value. Syntax Following is the declaration of method: public static <T> boolean replaceAll(List<T> list, T oldVal, T newVal) Parameter Parameter Description Required/Optional list It...
2 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India