标签:
-------------------|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()
import java.util.*; public class ex12 { public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>();//为什么改成int会报错 map.put("Tom", "1"); map.put("Jack", "2"); map.put("Yoo", "3"); map.put("Peter", "4"); map.put("LiLi", "5"); System.out.println(map); System.out.println(); Map<String, String> map2 = new HashMap<String,String>(); map2.put("AAAAA","1"); map2.put("PPPPP","6"); map.putAll(map2);//把map2 的所有添加到map 集合中 System.out.println(map);//注意该行输出的结果 System.out.println(); System.out.println("remove(Object key)=====" + map2.remove("AAAAA"));//注意返回值 System.out.println("remove(Object key)=====" + map2.remove("BBBBB")); System.out.println(); // System.out.println("clear()=======" + map2.clear());//报错 map2.clear(); System.out.println(map2);//清空所有 System.out.println(); System.out.println(map.get("PPPPP")); System.out.println(map.size()); System.out.println(); System.out.println(map.isEmpty()); } }
标签:
原文地址:http://www.cnblogs.com/lifehrx/p/5802240.html