Map接口和Collection有一定区别
1.保存形式:
key->value的方式保存
例:张三 15953111111
2.常用子类
HashMap:无序存放,key不允许重复
HashTable:无序存放,key不允许重复
在以Map存放数据的时候,key都是唯一的
3.常用方法
put(Object key,Object value) 存入元素
boolean containsKey(Object key) 判断key是否存在
boolean containsValue(Object value) 判断value是否存在
Set<E> keySet() 返回所有的key到Set集合
Map<String, String> map = new HashMap<String, String>();
map.put("key1","000");
map.put("key2","111");
map.put("key3","222");
map.put("key4","333");
map.put("key5","444");
map.put("key6","555");
Set<String> set = map.keySet();
Iterator<String> iter = set.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
key1
key2
key5
key6
key3
key4
在这里是无序存放的
Collection values() 返回所有的value到Collection
Collection<String> collection = map.values();
Iterator<String> iter = collection.iterator();
while(iter.hasNext()){
System.out.println(iter.next());
}
转载于:https://blog.51cto.com/11317783/1774922