hashmap基本操作
HashMap<Integer,Integer> m=new HashMap<Integer,Integer>();
m.put(“No.1”,“Oliver”);//add data
m.put(“No.2”,“Kimiko”);
print(m.isEmpty());//false
print(m.size());//2
print(m.containsKey(“No.1”));//true
print(m.contansValue(“Oliver”));//true
map.entrySet();
Iterator iterator = map.entrySet().iterator();
while(iterator.hasNext()){
var entry = (Map.Entr<Integer,Integer>)iterator.next();
print("Key: "+entry.getKey()+“Value:”+entry.getValue());
}
HashMap和HashSet区别
HashMap * HashSet*
HashMap实现了Map接口 HashSet实现了Set接口
HashMap储存键值对 HashSet仅仅存储对象
使用put()方法将元素放入map中 使用add()方法将元素放入set中
HashMap中使用键对象来计算hashcode值 HashSet使用成员对象来计算hashcode值,对于两个对象来 说hashcode可能相同,所以equals()方法用来判断对象的相等性,如果两个对象不同的话,那么返回false
HashMap比较快,因为是使用唯一的键来获取对象 HashSet较HashMap来说比较慢
为什么要用unordered_map代替hash_map?
因为标准化的推进,unordered_map原来属于boost分支和std::tr1中,而hash_map属于非标准容器。
另外,使用之后,感觉速度和hash_map差不多,但是支持string做key,也可以使用复杂的对象作为key。
gxx需要添加编译选项:–std=gnu++0x或者–std=c++0x