首页 > 其他 > 详细

Map的排序问题

时间:2014-02-15 16:28:20      阅读:389      评论:0      收藏:0      [点我收藏+]

map有序无序?如果说有序, 这个顺序是怎么定义的? 安装put的先后顺序吗? 还是被put元素的内容呢?

 

经观察,应该是后者,跟put先后顺序无关, 跟内部实现有关(可能是hash排序的, 非大小排序)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static void main(String[] args) {
 
// asfd();
// TODO Auto-generated method stub
Map<String, String> map = new ConcurrentHashMap<String, String>();
//   map.put("10.167.240.78", "aa");
//   map.put("10.167.240.61", "bb");
//   map.put("10.167.240.46", "dd");
//   map.put("10.167.240.54", "cc");
 
map.put("205", "b");
map.put("101", "b");
map.put("201", "b");
 
for (int i = 10; i > 0; i--) {
// map.put(i+"", Math.random()+"");
}
 
System.out.println(map);
 
}

  

 


打印 :{201=b, 101=b, 205=b}


如果是
map.put("205", "b");
map.put("101", "b");
map.put("201", "b");
(把205改成25)
则打印:
{201=b, 101=b, 25=b}

 

 

而且发现ConcurrentHashMap和HashMap的表现也不同 —— 内部的hash算法还有细节上的区别吗?

Map的排序问题

原文:http://www.cnblogs.com/FlyAway2013/p/3550431.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!