Sorting collection of String and StringBuffer in Java Last Updated : 09 Nov, 2020 Comments Improve Suggest changes Like Article Like Report Sorting a collection of objects can be done in two ways: By implementing Comparable (Natural Order) e.g. Strings are sorted ASCIIbetically. meaning B comes before A and 10 is before 2.By implementing Comparator (Custom order) and it can be in any order.Using Collections.sort() method of Collections utility class. Read more about Comparable and ComparatorFor sorted Collection we can use below collections: TreeSetTreeMap In case of String, sorting will be done automatically in natural order. Java // Java program to sort String objects using TreeSet import java.util.Set; import java.util.TreeSet; public class Test { public static void main(String[] args) { Set<String> str = new TreeSet<String>(); str.add("Sohan"); str.add("Raja"); str.add("Harish"); str.add("Ram"); System.out.println(str); } } Output:  [Harish, Raja, Ram, Sohan] Java // Java program to demonstrate that simple sorting // StringBuffer objects does work. import java.util.Set; import java.util.TreeSet; public class Test { public static void main(String[] args) { Set<StringBuffer> str = new TreeSet<>(); str.add(new StringBuffer("Sohan")); str.add(new StringBuffer("Raja")); str.add(new StringBuffer("Harish")); str.add(new StringBuffer("Ram")); System.out.println(str); } } Output[Harish, Raja, Ram, Sohan] String class implements Comparable interface whereas StringBuffer and StringBuilder classes do not implement Comparable interface. See below signatures of String, StringBuffer and StringBuilder classes: Java public final class String extends Object implements Serializable, Comparable, CharSequence Java public final class StringBuffer extends Object implements Serializable, CharSequence Java public final class StringBuilder extends Object implements Serializable, CharSequence There are many ways of sorting StringBuffer, StringBuilder classes. Some of the ways are given below: By implementing Comparator interfaceBy converting StringBuffer to String using StringBuffer.toString() method Java // Java program to demonstrate sorting // of StringBuffer objects using Comparator // interface. import java.util.Comparator; import java.util.Set; import java.util.TreeSet; public class Test implements Comparator<StringBuffer> { @Override public int compare(StringBuffer s1, StringBuffer s2) { return s1.toString().compareTo(s2.toString()); } public static void main(String[] args) { Set<StringBuffer> str = new TreeSet<>(new Test()); str.add(new StringBuffer("Sohan")); str.add(new StringBuffer("Raja")); str.add(new StringBuffer("Harish")); str.add(new StringBuffer("Ram")); System.out.println(str); } } Output: [Harish, Raja, Ram, Sohan] Comment More infoAdvertise with us Next Article Sorting collection of String and StringBuffer in Java S Sajid Ali Khan Improve Article Tags : Strings Tree Java DSA Java-Strings java-StringBuffer Java-String-Programs +3 More Practice Tags : JavaJava-StringsStringsTree Similar Reads equals() on String and StringBuffer objects in Java Consider the following codes in java: Java // This program prints false class GFG { public static void main(String[] args) { StringBuffer sb1 = new StringBuffer("GFG"); StringBuffer sb2 = new StringBuffer("GFG"); System.out.println(sb1.equals(sb2)); } } Output: false Java // This 1 min read How to sort an Array of Strings in Java Array Of StringsTo sort an array of strings in Java, we can use Arrays.sort() function. Java // A sample Java program to // sort an array of strings // in ascending and descending // orders using Arrays.sort(). import java.util.Arrays; import java.util.Collections; // Driver Class public class SortE 3 min read Swapping Pairs of Characters in a String in Java Given string str, the task is to write a Java program to swap the pairs of characters of a string. If the string contains an odd number of characters then the last character remains as it is. Examples: Input: str = âJavaâOutput: aJav Explanation: The given string contains even number of characters. 3 min read String vs StringBuilder vs StringBuffer in Java A string is a sequence of characters. In Java, String objects are immutable, which simply means once created, their values can not be changed. In Java, String, StringBuilder, and StringBuffer are used for handling strings. The main difference is:String: Immutable, meaning its value cannot be changed 6 min read Sort an array of strings according to string lengths We are given an array of strings, we need to sort the array in increasing order of string lengths.Examples: Input : {"GeeksforGeeeks", "I", "from", "am"}Output : I am from GeeksforGeeks Input : {"You", "are", "beautiful", "looking"}Output : You are looking beautiful A simple solution is to write our 10 min read Swapping Characters of a String in Java As we know that Object of String in Java are immutable (i.e. we cannot perform any changes once its created). To do modifications on string stored in a String object, we copy it to a character array, StringBuffer, etc and do modifications on the copy object.In this article we would go through some m 3 min read StringBuffer Class in Java The StringBuffer class in Java represents a sequence of characters that can be modified, which means we can change the content of the StringBuffer without creating a new object every time. It represents a mutable sequence of characters.Features of StringBuffer ClassThe key features of StringBuffer c 11 min read StringBuffer lastIndexOf() method in Java with Examples In StringBuffer class, there are two types of lastIndexOf() method depending upon the parameters passed to it. lastIndexOf(String str) The lastIndexOf(String str) method of StringBuffer class is the inbuilt method used to return the index within the String for last occurrence of passed substring as 4 min read StringBuffer setCharAt() method in Java with Examples The setCharAt() method of StringBuffer class sets the character at the position index to character which is the value passed as parameter to method. This method returns a new sequence which is identical to old sequence only difference is a new character ch is present at position index in new sequenc 2 min read Sort a String in Java (2 different ways) The string class doesn't have any method that directly sorts a string, but we can sort a string by applying other methods one after another. The string is a sequence of characters. In java, objects of String are immutable which means a constant and cannot be changed once created. Creating a String T 6 min read Like