標籤:檔案內容 內容 property void htable equals extends eth div
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();//表示拿出來了所有的key23 ---> Collection values() 返回此映射中包含的值的 Collection 視圖。24 例子:Collection collection = map.values();//表示拿出來了所有的value25 ---> 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 V37 例子: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 Properties44 45 46 47 48 49 (擴充):public class Hashtable<K,V>extends Dictionary<K,V>50 implements Map<K,V>, Cloneable, Serializable51 (HashMap和HashTable)52 Hashtable:古老的,安全執行緒的 不允許null作為K、V 53 HashMap : 線程不安全 允許null54 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 hehe66 password 12367 Code-----:68 Properties pro=new Properties();69 ---> pro.load(new FileInputStream(new File("d:/config.properties")));70 ---> String name=pro.getProperty("userName"); //通過K得到V71 String pwd=pro.getProperty("password");72 System.out.println("使用者名稱 :"+name+" 密碼:"+pwd);73 Result-----:74 使用者名稱 :hehe 密碼:12375 76 77 78 */79 public class Map_Method {80 81 }
Java基礎筆記——map