Map也称为双列集合,Collection集合称为单列集合。
其实map集合中存储的就是键值对。
map集合中必须保证键的唯一性。
Map常用的子类:
|--Hashtable :内部结构是哈希表,是同步的。不允许null作为键,null作为值。
|--Hashtable|--Properties:用来存储键值对型的配置文件的信息,可以和IO技术相结合。
|--HashMap : 内部结构是哈希表,不是同步的。允许null作为键,null作为值。
|--TreeMap : 内部结构是二叉树,不是同步的。可以对Map集合中的键进行排序。
常用方法:
1,添加。
value put(key,value):返回前一个和key关联的值,如果没有返回null.
2,删除。
void clear():清空map集合。 value remove(key):根据指定的key翻出这个键值对。
3,判断。
boolean containsKey(key) boolean containsValue(value) boolean isEmpty();
4,获取。
value get(key):通过键获取值,如果没有该键返回null。当然可以通过返回null,来判断是否包含指定键。
int size(): 获取键值对的个数。
5, Set<k> keyset();
返回的是Map中Key 但是变成了collection集合中的Set
然后在通过collection的迭代器遍历(Map本身是不具备迭代器)
Set a =map.keySet();
Iterator<Integer> it = a.iterator();
while(it.hasNext()){
int key = it.next();
System.out.println(key+":"+map.get(key));
6,Set<Map.Entry<K,V>> entrySet()
返回的是Map中K和V 其他同上(5)
Set<Map.Entry<Integer, String>> b = map.entrySet();
Iterator<Map.Entry<Integer, String>> it = b.iterator();
while(it.hasNext()){
Map.Entry<Integer, String> me = it.next();
Integer key = me.getKey();
String value = me.getValue();
System.out.println(key+"::"+value);
7,Collection<V> values(); 取集合中所有的V 同上collection中的迭代