在Java中对 String 和 StringBuffer 的集合进行排序
可以通过两种方式对对象集合进行排序:
- 通过实现 Comparable (Natural Order),例如字符串按 ASCII 顺序排序。意思是 B 在 A 之前,而 10 在 2 之前。
- 通过实现比较器(自定义顺序),它可以是任何顺序。
- 使用 Collections 实用程序类的 Collections.sort() 方法。
阅读更多关于 Comparable 和 Comparator
对于排序的集合,我们可以使用以下集合:
- 树集
- 树状图
在字符串的情况下,排序将按自然顺序自动完成。
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 str = new TreeSet();
str.add("Sohan");
str.add("Raja");
str.add("Harish");
str.add("Ram");
System.out.println(str);
}
}
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 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);
}
}
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
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 {
@Override public int compare(StringBuffer s1, StringBuffer s2)
{
return s1.toString().compareTo(s2.toString());
}
public static void main(String[] args)
{
Set 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);
}
}
输出:
[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 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);
}
}
输出
[Harish, Raja, Ram, Sohan]
String 类实现了 Comparable 接口,而 StringBuffer 和 StringBuilder 类不实现 Comparable 接口。请参阅下面的 String、StringBuffer 和 StringBuilder 类的签名:
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
StringBuffer、StringBuilder类的排序方式有很多种。下面给出一些方法:
- 通过实现 Comparator 接口
- 通过使用 StringBuffer.toString() 方法将 StringBuffer 转换为 String
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 {
@Override public int compare(StringBuffer s1, StringBuffer s2)
{
return s1.toString().compareTo(s2.toString());
}
public static void main(String[] args)
{
Set 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);
}
}
输出:
[Harish, Raja, Ram, Sohan]