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());
} } );

  

最新文章

  1. boost库的使用
  2. 正确理解javascript当中的面向对象
  3. X240s安装Win7 以及如何启用ExpressCache
  4. freebsd|odoo - 为odoo报表 安装文泉译中文字体
  5. FatMouse&#39; Trade_贪心
  6. SQL调用系统存储过程整理
  7. thinkphp 访问其它控制器模板
  8. 内核中的 likely() 与 unlikely()
  9. HDU 1260 Tickets(简单dp)
  10. Automake
  11. linux的comm命令
  12. 隐藏nginx 版本号信息(转)
  13. Docker容器中开始.NETCore之路
  14. POJ2396 Budget [有源汇上下界可行流]
  15. HTML5之webSocket使用
  16. Pytorch中的自动求导函数backward()所需参数含义
  17. 【bzoj 3110】[Zjoi2013]K大数查询
  18. Hadoop之YARN思维导图
  19. node.js之爬虫
  20. PathUtil

热门文章

  1. Python文件夹与文件的操作 ZZ
  2. 记账本app(一)
  3. 高逼格UILabel的闪烁动画效果
  4. iptables:no config file
  5. Redis 4.0+安装及配置
  6. postgres if ,when及判断表是否存在的sql编写
  7. gpu 显卡 本质
  8. sysctl.conf学习和调优
  9. php签到
  10. php函数:call_user_func