public class
HashMap<K,V> extends AbstractMap<K,V> implements
Map<K,V>, Cloneable, Serializable
public class
TreeMap<K,V>
extends AbstractMap<K,V>
implements
SortedMap<K,V>, Cloneable, java.io.Serializable
Obviously,TreeMap store elements sorted.But for the chinese word ,the default
Comparator don't do it rightly,so you must write your
Comparator .this is an example from web:
importjava.text.CollationKey;
import java.text.Collator;
import java.util.Comparator;
/**
*@author www.inspiresky.com
*
*/
publicclass CollatorComparator implements Comparator {
Collator collator = Collator.getInstance();
publicint compare(Object element1, Object element2) {
CollationKey key1 = collator.getCollationKey(element1.toString());
CollationKey key2 = collator.getCollationKey(element2.toString());
return key1.compareTo(key2);
}
}
to use this CollatorComparator ,use TreeMap constructor
Mothod :
public TreeMap(Comparator<? super K> c) {
this.comparator = c;
}
posted on 2006-10-30 16:32
R.Zeus 阅读(325)
评论(0) 编辑 收藏 所属分类:
J2SE