TreeMap

本文提供了两个Java示例,展示了如何使用TreeMap进行排序。第一个示例通过自定义比较器按账户所有者的姓氏进行排序;第二个示例则介绍了如何通过CollatorComparator类解决中文排序的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[quote]/*

比较函数类TComp比较两个包含姓和名的字符串。它首先比较姓,具体是这样做的,首先寻找每一个字符串中最后一个空格的下标,然后比较从这个位置开始的每一个元素的子字符串。当两个字符串中姓完全相等时,再比较两个名。这样就形成了先按姓排序,在姓相同的情况下再按名字进行排序的树型映射。

*/
//Use a comparator to sort accounts by last name.
import java.util.*;
//Compare last whole words in two strings.
class TComp implements Comparator{
public int compare(Object a,Object b){
int i,j,k;
String strA,strB;
strA = (String)a;
strB = (String)b;
//find index of beginning of last name
i = strA.lastIndexOf(' ');
j = strB.lastIndexOf(' ');
k = strA.substring(i).compareTo(strB.substring(j));
if(k==0) //last name match,check entire name
return strA.compareTo(strB);
else
return k;
}
//no need to override equals
}
public class TreeMapDemo2{
public static void main(String[] args)
{
//Create a tree map.
TreeMap tm = new TreeMap(new TComp());
//Put elements to the map.
tm.put("Sue Yuan",new Double(17.15));
tm.put("Jiahui Sheng",new Double(78777));
tm.put("Huajiang Chen",new Double(12345.77));
tm.put("Magic Ya",new Double(-99.10));
tm.put("Quanbing Chen",new Double(100.00));
//Get a set of the entries.
Set set = tm.entrySet();
//Get an iterator.
Iterator i = set.iterator();
//Display elements.
while(i.hasNext()){
Map.Entry me = (Map.Entry)i.next();
System.out.println(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println();
//Deposit 1000 into Jiahui Sheng's account
double balance = ((Double)tm.get("Jiahui Sheng")).doubleValue();
tm.put("Jiahui Sheng",new Double(balance + 1000));
System.out.println("Jiahui Sheng's new balance : " + tm.get("Jiahui Sheng"));
}
}

[/quote]

[quote]
public class TreeSort {   

public static void main(String[] args) {

CollatorComparator comparator = new CollatorComparator();
TreeMap<String, Object> map = new TreeMap<String, Object>(comparator);

for (int i = 0; i < 10; i++) {
String s = "" + (int) (Math.random() * 1000);
map.put(s, s);
}
map.put("abcd", "abcd");
map.put("Abc", "Abc");
map.put("bbb", "bbb");
map.put("BBBB", "BBBB");
map.put("北京", "北京");
map.put("中国", "中国");
map.put("上海", "上海");
map.put("厦门", "厦门");
map.put("香港", "香港");
map.put("碑海", "碑海");
Collection col = map.values();
Iterator it = col.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}

// Set set=map.keySet();

Iterator its = map.entrySet().iterator();
while (its.hasNext()) {
// entry的输出结果如key0=value0等
Map.Entry entry = (Map.Entry) its.next();
Object key = entry.getKey();
Object value = entry.getValue();
System.out.println(entry);
// System.out.println(key);
// System.out.println(value);
}
List list = Collections.synchronizedList(new LinkedList());

String s="((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)";

System.out.println("122.068.2.0".matches(s));

}
}

/**
* Java中之所以出现偏差,主要是compare方法的问题,所以这里自己实现Comparator接口,
* 而国际化的问题,使用Collator类来解决。这里先解决中文问题,代码如下:
*
*
*
*/
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); 加负号将逆序
return key1.compareTo(key2);
}

}
[/quote]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值