1 package map_show;
2 /*
3 * Map--properties
4 */
5
6
7
8 /*
9 * Map接口:具有映射关系“key-value对”的集合 ---类似于高中的“函数”
10 *
11 * ------Map 方法:
12 *
13 添加、删除操作:
14 ---> Object put(Object key,Object value) 将指定的值与此映射中的指定键关联
15 void putAll(Map t)
16 例子:map.put("AA", 11);
17 ---> Object remove(Object key)
18 void clear()
19
20 元视图操作的方法:
21 ---> Set keySet() 返回此映射中包含的键的 Set 视图。
22 例子:Set keySet = map.keySet();//表示拿出来了所有的key
23 ---> Collection values() 返回此映射中包含的值的 Collection 视图。
24 例子:Collection collection = map.values();//表示拿出来了所有的value
25 ---> Set entrySet() 返回此映射中包含的映射关系的 Set 视图。
26 例子:Set entrySet = map.entrySet();//表示拿出来了所有的映射对:AA=11 说明:主要是K,可以通过map.get(k),来得到V
27
28 元素查询的操作:
29 ---> Object get(Object key)
30 boolean containsKey(Object key)
31 boolean containsValue(Object value)
32 ---> int size()
33 boolean isEmpty()
34 boolean equals(Object obj)
35
36 Map.Entry接口里面有getKey() getValue() 取得对应的K V
37 例子:Itarator iter=map.entrySet().itarator();
38 ---> Map.Entry entry = (Entry) iter.next();
39 ---> System.out.println(entry.getKey()+"--->"+entry.getValue());
40
41
42
43 Map接口的实现类 HashMap TreeMap Properties
44
45
46
47
48
49 (扩展):public class Hashtable<K,V>extends Dictionary<K,V>
50 implements Map<K,V>, Cloneable, Serializable
51 (HashMap和HashTable)
52 Hashtable:古老的,线程安全的 不允许null作为K、V
53 HashMap : 线程不安全 允许null
54
55
56 Properties类:
57 继承于Hashtable,所以有K/V都是字符串;主要用在属性文件或配置文件
58 说明:
59 1、由于属性文件里的 key、value 都是字符串类型,所以 Properties 里的 key 和 value 都是字符串类型
60 2、存取数据时,建议使用setProperty(String key,String value)方法和getProperty(String key)方法
61 例子:
62 有一个文件:
63 文件全名:config.properties:
64 文件内容:
65 userName hehe
66 password 123
67 Code-----:
68 Properties pro=new Properties();
69 ---> pro.load(new FileInputStream(new File("d:/config.properties")));
70 ---> String name=pro.getProperty("userName"); //通过K得到V
71 String pwd=pro.getProperty("password");
72 System.out.println("用户名 :"+name+" 密码:"+pwd);
73 Result-----:
74 用户名 :hehe 密码:123
75
76
77
78 */
79 public class Map_Method {
80
81 }