双列集合:
-----| Map 如果是实现了Map接口的集合类,具备的特点: 存储的数据都是以键值对的形式存在的,键不可重复,值可以重复。
----------| HashMap
----------| TreeMap
----------| Hashtable
Map接口的方法:
添加:
put(K key, V value)
putAll(Map<? extends K,? extends V> m)
删除
remove(Object key)
clear()
获取:
get(Object key)
size()
判断:
containsKey(Object key)
containsValue(Object value)
isEmpty()
首先创建:
Map<String, String> map = new HashMap<String, String>();
添加:
map.put("123", "abc");
map.put("456", "def");
map.put("345", "ghi");
map.put("789", "ghi");
map.put("222", "jkl");
System.out.println("返回值:" + map.put("222", "qwe"));
//没有存在该键,返回值为null,如果之前就存在其他键值,就返回该键之前对应的值 jkl
//map.put(null, null);这也是可以的
Map<String, String> map2 = new HashMap<String, String>();
map2.put("888","rty");
map2.put("666", "rty");
map.putAll(map2);
删除方法:
System.out.println("删除的数据是:"+map.remove("456"));//根据键删除值
map.clear(); //清除集合中的所有数据
获取元素:
System.out.println("根据指定的键值获取对应的值:" + map.get("456"));
System.out.println("获取Map集合键值对个数:" + map.size());
判断方法:
System.out.println("判断map集合是否包含指定的键:"+map.containsKey("222"));
System.out.println("判断map集合是否包含指定的值:"+map.containsValue("abc"));
System.out.println("判断map集合是否为空元素:"+map.isEmpty());