TreeMap中⽂排序,TreeMap倒序输出排列1、TreeMap集合倒序排列
import java.util.Comparator;
/**
* ⽐较算法的类,⽐较器
* @author Administrator
*
*/
public class MyCmp implements Comparator<Object> {
// 实现倒序
@Override
public int compare(Object o1, Object o2) {
int x = o2.toString()String());
return x;
}
}
import java.util.Iterator;
import java.util.TreeMap;
public class TestMap {
public static void main(String[] args) {
TreeMap<String,String> map = new TreeMap<String,String> (new MyCmp());
map.put("10001","张某");
map.put("10005","孙某");
map.put("10003","⽅某");
map.put("10004","谭某");
map.put("10002","李某");
Iterator<String> it = map.keySet().iterator();
while(it.hasNext()){
String key = it.next();
System.out.println(key+"="+(key));
}
}
}
2、TreeMap中⽂排序
CollationKey;
Collator;
import java.util.Comparator;
/**
* 处理中⽂排序
* @author Administrator
*
*/
public class MyCmp1 implements Comparator<Object> {
Collator collator = Instance();
/**
* 提供以与⾃然语⾔⽆关的⽅式来处理⽂本,⽇期,数字和消息的类和接⼝
* 获取当前默认语⾔环境的Collator
*/
@Override
public int compare(Object o1, Object o2) {
// TODO Auto-generated method stub
CollationKey key1 = String());
CollationKey key2 = String());
return key1pareTo(key2);
}
}
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
public class TestMap1 {
public static void main(String[] args) {
TreeMap<String,String> map = new TreeMap<String,String>(new MyCmp1());
map.put("艾伦", "1001");
java集合排序怎么实现map.put("过去", "1002");
map.put("以后", "1003");
map.put("明天", "1004");
map.put("未来", "1005");
Set<Entry<String,String>> set = Set(); Iterator<Entry<String,String>> it = set.iterator();
while(it.hasNext()){
Entry<String, String> e = it.next();
System.out.Key()+"="+e.getValue()); }
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论