Collection中的集合,元素是孤立存在的,向集合中存储元素采用一个个元素的方式存储
相反Map中的集合,元素是成对存在的。每个元素由键与值两部分组成
Map<K,V> 集合特点:不能包含重复的键,值可以重复;每个键只能对应一个值
Map的常用子类:
HashMap:存储数据采用的哈希表结构,元素的存取顺序不能保证一致。
要保证键的唯一、不重复,需要重写键的hashCode()方法、equals()方法(原因:与Set集合相同)
LinkedHashMap:存储数据采用的哈希表结构+链表结构。通过链表结构可以保证元素的存取顺序一致;
LinkedHashMap在拥有HashMap的基础上能保证元素存取有序
Map常用方法:
public V put(K key, V value) 把指定的键与指定的值添加到Map集合中
HashMap<Integer, String> hm = new HashMap();
hm.put(1, "小张");
hm.put(2, "小王");
hm.put(3, "小李");
System.out.println(hm); //输出: {1=老王, 2=小王, 3=小张}
注意:使用put方法时接受返回值时,若指定的键(key)在集合中没有,则没有这个键对应的值,返回null,并把指定的键值添加到集合中
若指定的键(key)在集合中存在,则返回值为集合中键对应的值(该值为替换前的值),并把指定键所对应的值,替换成指定的新值
public V remove(Object key) 把指定的键所对应的键值对元素在Map集合中删除,返回被删除元素的值
HashMap<Integer, String> hm = new HashMap();
hm.put(1, "小张");
hm.put(2, "小王");
hm.put(3, "小李");
hm.remove(2);
System.out.println(hm); //输出:{1=老王, 3=小张}
public boolean boolean isEmpty() 判断集合是否为空,返回boolean值
HashMap<Integer, String> hm = new HashMap();
hm.put(1, "老王");
hm.put(2, "小王");
hm.put(3, "小张");
boolean b = hm.isEmpty();
System.out.println(b); //输出:false
public void clear() 删除集合中所有键值对
HashMap<Integer, String> hm = new HashMap();
hm.put(1, "老王");
hm.put(2, "小王");
hm.put(3, "小张");
hm.clear();
boolean b = hm.isEmpty();
System.out.println(b); //输出:true
public boolean containsKey(Object key) 判断是否包含指定的key,返回boolean值
HashMap<Integer, String> hm = new HashMap();
hm.put(1, "老王");
hm.put(2, "小王");
hm.put(3, "小张");
boolean b1 = hm.containsKey(1);
boolean b2 = hm.containsKey(4);
System.out.println(b1); //输出:true
System.out.println(b2); //输出:false
public int size() 获得键值对个数
HashMap<Integer, String> hm = new HashMap();
hm.put(1, "老王");
hm.put(2, "小王");
hm.put(3, "小张");
int i = hm.size();
System.out.println(i); //输出:3
public V get(Object key) 根据指定的键,在Map集合中获取对应的值
HashMap<Integer, String> hm = new HashMap();
hm.put(1, "老王");
hm.put(2, "小王");
hm.put(3, "小张");
String str = hm.get(2);
System.out.println(str); //输出:"小王"
public Set<K> keySet() 获取Map集合中所有的键,存储到Set集合中
HashMap<Integer, String> hm = new HashMap();
hm.put(1, "老王");
hm.put(2, "小王");
hm.put(3, "小张");
Set<Integer> s = hm.keySet();
System.out.println(s); //输出:[1, 2, 3]
public Set<Map.Entry<K,V>> entrySet() 获取到Map集合中所有的键值对对象的集合(Set集合)
HashMap<Integer, String> hm = new HashMap();
hm.put(1, "老王");
hm.put(2, "小王");
hm.put(3, "小张");
Set<Entry<Integer, String>> entryset = hm.entrySet();
System.out.println(entryset); //输出:[1=老王, 2=小王, 3=小张]
Entry键值对对象:
Map 中存放的是两种对象,一种称为key(键),一种称为value(值)
它们在在Map 中是一一对应关系,这一对对象又称做Map 中的一个Entry(项)
Entry 将键值对的对应关系封装成了对象,即键值对对象
Entry提供了两个获取值和键的方法:
public K getKey() 获取Entry对象中的键。
HashMap<Integer, String> hm = new HashMap();
hm.put(1, "老王");
hm.put(2, "小王");
hm.put(3, "小张");
Set<Entry<Integer, String>> entryset = hm.entrySet();
for (Entry<Integer, String> entry : entryset) { //使用增强for将entryset集合中的所有键值对遍历出来
Integer in = entry.getKey(); //用键值对对象的getKey()获取键
System.out.println(in);
}
public V getValue() 获取Entry对象中的值。
HashMap<Integer, String> hm = new HashMap();
hm.put(1, "老王");
hm.put(2, "小王");
hm.put(3, "小张");
Set<Entry<Integer, String>> entryset = hm.entrySet();
for (Entry<Integer, String> entry : entryset) {
String str = entry.getValue(); //使用键值对的getValue()方法获取值
System.out.println(str);
}
遍历Map集合方式一:
使用Map集合的keySet()方法获取集合中所有键,到Set集合中保存
并使用增强for(也可以使用Iterator迭代器)遍历所有的键,再用Map集合的get()方法
根据遍历的所有键获取值Map集合的所有值,从而完成遍历
HashMap<Integer, String> hm = new HashMap();
hm.put(1, "老王");
hm.put(2, "小王");
hm.put(3, "小张");
Set<Integer> keyset = hm.keySet();
for (Integer key : keyset) {
String value = hm.get(key);
System.out.println(key + "..." + value);
}
遍历Map集合方式二:
使用Map集合的entrySet()方法将所有键值对对象保存在Set集合中
使用增强for(使用Iterator也可以)遍历Set集合中的所有键值对对象
使用entry对象的getKey(),getValue()方法获取所有键与值,从而完成遍历
HashMap<Integer, String> hm = new HashMap();
hm.put(1, "老王");
hm.put(2, "小王");
hm.put(3, "小张");
Set<Entry<Integer, String>> entryset = hm.entrySet();
for (Entry<Integer, String> entry : entryset) {
Integer key = entry.getKey();
String value = entry.getValue();
System.out.println(key + "=" + value);
}