Sorting collection of String and StringBuffer in Java
Last Updated :
09 Nov, 2020
Sorting a collection of objects can be done in two ways:
Read more about Comparable and Comparator
For sorted Collection we can use below collections:
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 interface
- By 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]
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