集合之TreeMap
2024-08-20 14:14:00
TreeMap
底层数据结构是二叉树
如何保证键的唯一: 利用存的特点
如何保证键的可排序: 利用取的特点 左跟右
在map中数据结构只对键有效
TreeMap 有Map的键值对的特性:还可以进行排序,默认自然排序
利用正则和TreeMap 实现一段英文的单词记录
String str = "Failure is probably the, fortification in your pole. " +
"It is like a peek your wallet as. the thief, when you are" +
" thinking how to spend several? hard-won lepta, when you " +
"are wondering whether ?new money, it has laid background." +
" Because of you, then at the heart of the most lax, alert," +
" and most " +
"low awareness, and left it godsend failed."; String s = "\\d+.\\d+|\\w+";//正则表达式
Pattern pattern=Pattern.compile(s);
Matcher ma=pattern.matcher(str); TreeMap<String, Integer> tree = new TreeMap<String,Integer>(); while(ma.find()){ String c = ma.group();//获取用正则取出来的单词 if(!tree.containsKey(c)){//去重和计数
tree.put(c, 1);
}else{
int num = tree.get(c)+1;
tree.put(c, num);
}
} //遍历:
Set<String> set = tree.keySet();
for (String key : set) {
System.out.println(key+"("+tree.get(key)+")");
} //TreeMap排序:
TreeMap<Person,String> map =new TreeMap<Person,String>(new java.util.Comparator<Person>(){//实现Comparator接口 @Override
public int compare(Person o1, Person o2) {//按实际要求重写该方法,必要时使用三门运算符
return -(o1.getHandsome()-o2.getHandsome());
} } );
最新文章
- boost库的使用
- 正确理解javascript当中的面向对象
- X240s安装Win7 以及如何启用ExpressCache
- freebsd|odoo - 为odoo报表 安装文泉译中文字体
- FatMouse&#39; Trade_贪心
- SQL调用系统存储过程整理
- thinkphp 访问其它控制器模板
- 内核中的 likely() 与 unlikely()
- HDU 1260 Tickets(简单dp)
- Automake
- linux的comm命令
- 隐藏nginx 版本号信息(转)
- Docker容器中开始.NETCore之路
- POJ2396 Budget [有源汇上下界可行流]
- HTML5之webSocket使用
- Pytorch中的自动求导函数backward()所需参数含义
- 【bzoj 3110】[Zjoi2013]K大数查询
- Hadoop之YARN思维导图
- node.js之爬虫
- PathUtil