Sorting collection of String and StringBuffer in Java


In order to sort in Java as we know we can use either Comparable or Comparator interfaces in which we could also define our custom logic to sort.One of the approach to sort is to add the entity in TreeSet or TreeMap which would sort the entries as internally they also uses the comparable interface.

Now String class in Java internally implements comparable interface so whenever we add string to a tree set or map it uses comparable logic of string class and sort the input entry strings.But String buffer do not have the implementation of comparable interface so when we tried to add string buffer entries in tree set or map it would throw an exception as it do no find the implementation of comparable interface.

In order to sort string buffer either we could implement the comparator interface and define own custom logic of sorting or we could first change our string buffer into string and then add string entries to tree set or map collection.

Example

Live Demo

import java.util.TreeSet;
public class StringBufferSort {
   public static void main(String[] args) {
      TreeSet<String> tset = new TreeSet<>();
      tset.add("Brown");
      tset.add("Yellow");
      tset.add("Red");
      tset.add("Grey");
      tset.add("White");
      System.out.println(tset);
      TreeSet<StringBuffer> tset1 = new TreeSet<>();	
      tset1.add(new StringBuffer("Brown"));
      tset1.add(new StringBuffer("Yellow"));
      tset1.add(new StringBuffer("Red"));
      tset1.add(new StringBuffer("Grey"));
      tset1.add(new StringBuffer("White"));
      System.out.println(tset1);
   }
}

Output

[Brown, Grey, Red, White, Yellow]
Exception in thread "main" java.lang.ClassCastException: java.lang.StringBuffer cannot be cast to java.lang.Comparable
	at java.util.TreeMap.compare(TreeMap.java:1188)
	at java.util.TreeMap.put(TreeMap.java:531)
	at java.util.TreeSet.add(TreeSet.java:255)
	at StringBufferSort.main(StringBufferSort.java:18)

Example

Live Demo

import java.util.Comparator;
import java.util.TreeSet;
public class StringBufferSort implements Comparator<StringBuffer>{
   @Override
   public int compare(StringBuffer strB1, StringBuffer strB2) {
      return strB1.toString().compareTo(strB2.toString());
   }
   public static void main(String[] args) {
      TreeSet<String> tset = new TreeSet<>();
      tset.add("Brown");
      tset.add("Yellow");
      tset.add("Red");
      tset.add("Grey");
      tset.add("White");
      System.out.println(tset);
      TreeSet<StringBuffer> tset1 = new TreeSet<>(new StringBufferSort());	
      tset1.add(new StringBuffer("Brown"));
      tset1.add(new StringBuffer("Yellow"));
      tset1.add(new StringBuffer("Red"));
      tset1.add(new StringBuffer("Grey"));
      tset1.add(new StringBuffer("White"));
      System.out.println(tset1);
   }
}

Output

[Brown, Grey, Red, White, Yellow]
[Brown, Grey, Red, White, Yellow]

Updated on: 30-Jul-2019

506 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements