- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
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
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]
- Related Articles
- Difference between string and StringBuffer in Java.
- Java String, StringBuffer and StringBuilder Tutorial.
- Difference Between String and StringBuffer Class in Java
- Difference between String class and StringBuffer class in Java
- How can we replace specific part of String and StringBuffer in Java?
- Differences between String and StringBuffer
- Difference between String and StringBuffer.
- Looping MongoDB collection for sorting
- Java program to insert a String into a Substring of StringBuffer
- Constructors of StringBuffer class in Java.
- Methods of StringBuffer class in Java.
- Why should we use a StringBuffer instead of a String in Java?\n
- Difference between StringBuilder and StringBuffer in Java
- What is the difference between a String object and a StringBuffer object in java?
- What is the difference between String, StringBuffer and StringBuilder classes in Java explain briefly?
