Collator 用来简化我们处理各种语言之间的差别性。Collator 主要处理:
- 规范化的典型等效 字符
- 多层次的比较
通过比较字符串 Unicode 字节的 code point 来进行Java字符串的比较。这将意味着在Unicode代码图表字符位置指定的排序权重,但事实并非如此。语言可能有完全相同的字符而不同的排序权重。
例如,你完全不懂德语,你可能希望 ß (\u00DF) 被当作 b 或者 B 来进行排序,但事实上是 ss,在该语言中 ß 的排序值高于正常的 s。
而多层次的比较指的是提供4种比较级别:基本字符、口音、case、标点符号。
需要注意的是 Collator 不支持标点符号。
来看看一些具体的关于多层次比较例子的代码:
- System.out.println("a equals b -> " + (collator.compare("a", "b")==0 ? "true":"false"));
- System.out.println("a equals à -> " + (collator.compare("a", "à")==0 ? "true":"false"));
- System.out.println("A equals a -> " + (collator.compare("a", "A")==0 ? "true":"false"));
当 collator.setStrength(Collator.PRIMARY):
a equals b -> false
a equals à -> true
A equals a -> true
当 collator.setStrength(Collator.SECONDARY):
a equals b -> false
a equals à -> false
A equals a -> true
当 collator.setStrength(Collator.TERTIARY):
a equals b -> false
a equals à -> false
A equals a -> false
还有下面这段代码,尽管第一行输出的是 false ,但是他们看起来确实完全相同的字符串:
- collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
- String single = "abgaskr\u00FCmmerdichtung";
- String combined = "abgaskr\u0075\u0308mmerdichtung";
- System.out.println("Single equals combined? " +
- (collator.compare(single, combined) == 0 ? "true": "false"));
等等,种子 Collator 类理解起来挺费劲,但当你需要处理各种不同语言时,你还真离不开它。
你可以从这里查看 Collator 类的 JavaDoc 文档。
- protected List<Map<String, Object>> getData(String prefix) {
- List<Map<String, Object>> myData = new ArrayList<Map<String, Object>>();
- Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
- mainIntent.addCategory("com.actionbarsherlock.sample.demos.EXAMPLE");
- PackageManager pm = getPackageManager();
- List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);
- if (null == list)
- return myData;
- String[] prefixPath;
- String prefixWithSlash = prefix;
- if (prefix.equals("")) {
- prefixPath = null;
- } else {
- prefixPath = prefix.split("/");
- prefixWithSlash = prefix + "/";
- }
- int len = list.size();
- Map<String, Boolean> entries = new HashMap<String, Boolean>();
- for (int i = 0; i < len; i++) {
- ResolveInfo info = list.get(i);
- CharSequence labelSeq = info.loadLabel(pm);
- String label = labelSeq != null
- ? labelSeq.toString()
- : info.activityInfo.name;
- if (prefixWithSlash.length() == 0 || label.startsWith(prefixWithSlash)) {
- String[] labelPath = label.split("/");
- String nextLabel = prefixPath == null ? labelPath[0] : labelPath[prefixPath.length];
- if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1) {
- addItem(myData, nextLabel, activityIntent(
- info.activityInfo.applicationInfo.packageName,
- info.activityInfo.name));
- } else {
- if (entries.get(nextLabel) == null) {
- addItem(myData, nextLabel, browseIntent(prefix.equals("") ? nextLabel : prefix + "/" + nextLabel));
- entries.put(nextLabel, true);
- }
- }
- }
- }
- Collections.sort(myData, sDisplayNameComparator);
- return myData;
- }
- private final static Comparator<Map<String, Object>> sDisplayNameComparator =
- new Comparator<Map<String, Object>>() {
- private final Collator collator = Collator.getInstance();
- public int compare(Map<String, Object> map1, Map<String, Object> map2) {
- return collator.compare(map1.get("title"), map2.get("title"));
- }
- };