双向map,通过value找到key,还有...
2016-03-09 15:00
671 查看
* 开发中,你有没有遇到过需要通过map中的value值获取相应的key的值呢?如果你的map中的key-value是1:1映射的,那好了,可以考虑使用org.apache.commons.collections.BidiMap.
下面演示它的几个小功能:
@Test
public void fun4() {
BidiMap map = new DualHashBidiMap();
map.put(“1”, “a”);
map.put(“2”, “b”);
map.put(3, “c”);
map.put(3, “cc”);//会覆盖上一行的值,因为这是1:1映射
map.put(3.14, Math.PI);
/*
* 再也不需要通过Map的Entry对象遍历了!!! 效率上去了.
*/
MapIterator it = map.mapIterator();
while (it.hasNext()) {
Object key = it.next();
Object value = it.getValue();
System.out.println(key + “->” + value);
}
//反向功能,通过value->key
System.out.println(map.getKey(“a”));
}
下面演示它的几个小功能:
@Test
public void fun4() {
BidiMap map = new DualHashBidiMap();
map.put(“1”, “a”);
map.put(“2”, “b”);
map.put(3, “c”);
map.put(3, “cc”);//会覆盖上一行的值,因为这是1:1映射
map.put(3.14, Math.PI);
/*
* 再也不需要通过Map的Entry对象遍历了!!! 效率上去了.
*/
MapIterator it = map.mapIterator();
while (it.hasNext()) {
Object key = it.next();
Object value = it.getValue();
System.out.println(key + “->” + value);
}
//反向功能,通过value->key
System.out.println(map.getKey(“a”));
}
相关文章推荐
- question about import google file
- UIBezierPath精讲
- UIAlertController
- BuiltWith
- 【bzoj1670】[Usaco2006 Oct]Building the Moat护城河的挖掘 凸包
- Android Volley完全解析(三),定制自己的Request
- HTML--11marquee标签
- com.mysql.jdbc.PacketTooBigException: Packet for query is too large (4200044 > 1048576). You can cha
- Django中远程服务器访问bad request
- 《iOS Human Interface Guidelines》——Scope Bar
- UICollectionView中layoutsubView的坑
- 1062. Talent and Virtue (25) 排序
- 《iOS Human Interface Guidelines》——Search Bar
- PHP中的break与continue
- Longest Consecutive Sequence
- 22.手势识别之UIPinchGestureRecognizer
- easyUI Combobox
- SurfaceView你所应该知道的事
- array_count_values() 函数
- UICollectionView cellForItemAtIndexPath 方法不走