java Collator类的具体用法场景

Collator 用来简化我们处理各种语言之间的差别性。Collator 主要处理:

  • 规范化的典型等效 字符
  • 多层次的比较

通过比较字符串 Unicode 字节的 code point 来进行Java字符串的比较。这将意味着在Unicode代码图表字符位置指定的排序权重,但事实并非如此。语言可能有完全相同的字符而不同的排序权重。

例如,你完全不懂德语,你可能希望 ß (\u00DF) 被当作 b 或者 B 来进行排序,但事实上是 ss,在该语言中 ß 的排序值高于正常的 s。

而多层次的比较指的是提供4种比较级别:基本字符、口音、case、标点符号。

需要注意的是 Collator 不支持标点符号。

来看看一些具体的关于多层次比较例子的代码:

[Java]  view plain copy
  1. System.out.println("a equals b -> " + (collator.compare("a""b")==0 ? "true":"false"));  
  2. System.out.println("a equals à -> " + (collator.compare("a""à")==0 ? "true":"false"));  
  3. 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 ,但是他们看起来确实完全相同的字符串:

[Java]  view plain copy
  1. collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);  
  2. String single = "abgaskr\u00FCmmerdichtung";  
  3. String combined = "abgaskr\u0075\u0308mmerdichtung";  
  4.   
  5. System.out.println("Single equals combined? " +   
  6.     (collator.compare(single, combined) == 0 ? "true""false"));  

等等,种子 Collator 类理解起来挺费劲,但当你需要处理各种不同语言时,你还真离不开它。

你可以从这里查看 Collator 类的 JavaDoc 文档。

[java]  view plain copy
  1. protected List<Map<String, Object>> getData(String prefix) {  
  2.         List<Map<String, Object>> myData = new ArrayList<Map<String, Object>>();  
  3.   
  4.         Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);  
  5.         mainIntent.addCategory("com.actionbarsherlock.sample.demos.EXAMPLE");  
  6.   
  7.         PackageManager pm = getPackageManager();  
  8.         List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);  
  9.   
  10.         if (null == list)  
  11.             return myData;  
  12.   
  13.         String[] prefixPath;  
  14.         String prefixWithSlash = prefix;  
  15.   
  16.         if (prefix.equals("")) {  
  17.             prefixPath = null;  
  18.         } else {  
  19.             prefixPath = prefix.split("/");  
  20.             prefixWithSlash = prefix + "/";  
  21.         }  
  22.   
  23.         int len = list.size();  
  24.   
  25.         Map<String, Boolean> entries = new HashMap<String, Boolean>();  
  26.   
  27.         for (int i = 0; i < len; i++) {  
  28.             ResolveInfo info = list.get(i);  
  29.             CharSequence labelSeq = info.loadLabel(pm);  
  30.             String label = labelSeq != null  
  31.                     ? labelSeq.toString()  
  32.                     : info.activityInfo.name;  
  33.   
  34.             if (prefixWithSlash.length() == 0 || label.startsWith(prefixWithSlash)) {  
  35.   
  36.                 String[] labelPath = label.split("/");  
  37.   
  38.                 String nextLabel = prefixPath == null ? labelPath[0] : labelPath[prefixPath.length];  
  39.   
  40.                 if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1) {  
  41.                     addItem(myData, nextLabel, activityIntent(  
  42.                             info.activityInfo.applicationInfo.packageName,  
  43.                             info.activityInfo.name));  
  44.                 } else {  
  45.                     if (entries.get(nextLabel) == null) {  
  46.                         addItem(myData, nextLabel, browseIntent(prefix.equals("") ? nextLabel : prefix + "/" + nextLabel));  
  47.                         entries.put(nextLabel, true);  
  48.                     }  
  49.                 }  
  50.             }  
  51.         }  
  52.   
  53.         Collections.sort(myData, sDisplayNameComparator);  
  54.   
  55.         return myData;  
  56.     }  
  57.   
  58.     private final static Comparator<Map<String, Object>> sDisplayNameComparator =  
  59.         new Comparator<Map<String, Object>>() {  
  60.         private final Collator   collator = Collator.getInstance();  
  61.   
  62.         public int compare(Map<String, Object> map1, Map<String, Object> map2) {  
  63.             return collator.compare(map1.get("title"), map2.get("title"));  
  64.         }  
  65.     };  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值