Wednesday, July 3, 2024
HomeLanguagesJavaSorting collection of String and StringBuffer in Java

Sorting collection of String and StringBuffer in Java

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: 

  • TreeSet
  • TreeMap

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]

This article is contributed by Sajid Ali Khan and improvisation by Soumyaranjan Panda. If you like Lazyroar and would like to contribute, you can also write an article using contribute.neveropen.co.za or mail your article to contribute@neveropen.co.za. See your article appearing on the Lazyroar main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments