序(2)
时间:2010-12-14
你是在对集合关键字进行排序,而不是实际的字符串。这不仅提供固定的不区分大小写的排序,而且它是跨语种的排序。换句话说,如果你对西班牙文和非西班牙文的混合词进行排序,词ma?ana (tomorrow)将排在mantra的前面。如果你不使用Collector,ma?ana将排在mantra的后面。
下面这个程序对一个列表进行不同类型的排序(缺省的、区分大小写的、区分语种的):
import java.awt.BorderLayout;
import java.awt.Container;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
public class SortIt {
public static class CollatorComparator
implements Comparator {
Collator collator = Collator.getInstance();
public int compare(Object element1,
Object element2) {
CollationKey key1 = collator.getCollationKey(
element1.toString());
CollationKey key2 = collator.getCollationKey(
element2.toString());
return key1.compareTo(key2);
}
}
public static class CaseInsensitiveComparator
implements Comparator {
public int compare(Object element1,
Object element2) {
String lower1 = element1.toString().
toLowerCase();
String lower2 = element2.toString().
toLowerCase();
return lower1.compareTo(lower2);
}
}
public static void main(String args[]) {
String words[] =
{"man", "Man", "Woman", "woman",
"Manana", "manana", "ma?ana", "Ma?ana",
"Mantra", "mantra", "mantel", "Mantel"
};
// Create frame to display sortings
JFrame frame = new JFrame("Sorting");
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
JTextArea textArea = new JTextArea();
JScrollPane pane = new JScrollPane(textArea);
contentPane.add(pane, BorderLayout.CENTER);
// Create buffer for output
StringWriter buffer = new StringWriter();
PrintWriter out = new PrintWriter(buffer);
// Create initial list to sort
List list = new ArrayList(Arrays.asList(words));
out.println("Original list:");
out.println(list);
out.println();
// Perform default sort
Collections.sort(list);
out.println("Default sorting:");
out.println(list);
out.println();
// Reset list
list = new ArrayList(Arrays.asList(words));
// Perform case insensitive sort
Comparator comp = new CaseInsensitiveComparator();
Collections.sort(list, comp);
out.println("Case insensitive sorting:");
out.println(list);
out.println();
// Reset list
list = new ArrayList(Arrays.asList(words));
// Perform collation sort
comp = new CollatorComparator();
Collections.sort(list, comp);
out.println("Collator sorting:");
out.println(list);
out.println();
// Fill text area and display
textArea.setText(buffer.toString());
frame.pack();
frame.show();
}
}
如果你的主要问题是顺序访问,可能列表不是你的好的数据结构选择。只要你的集合没有重复,你可以在树(TreeSet)中保存你的元素(提供或不提供Comparator)。这样,元素将总是排序形式的。 |