集合框架06
一、Map接口
1 public class Demo01 { 2 /* 3 * Map接口中的常用方法 4 * 使用Map接口的實(shí)現(xiàn)類HashMap 5 */ 6 public static void main(String[] args) { 7 //function(); 8 function_2(); 9 } 10 /* 11 * 將鍵值對(duì)存儲(chǔ)到集合中 12 * V put(K,V) K作為鍵的對(duì)象,V作為值得對(duì)象 13 * 存儲(chǔ)的是重復(fù)的鍵,會(huì)覆蓋值 14 * 返回值一般是空 15 */ 16 public static void function(){ 17 Map<String,Integer> map = new HashMap<String,Integer>(); 18 map.put("a", 1); 19 map.put("b", 2); 20 map.put("c", 3); 21 22 System.out.println(map); 23 } 24 /* 25 * 通過鍵,獲取值 26 * V get(K) 27 * 如果集合沒有指定的鍵,返回NULL 28 */ 29 public static void function_1(){ 30 Map<String,Integer> map = new HashMap<String,Integer>(); 31 map.put("a", 1); 32 map.put("b", 2); 33 map.put("c", 3); 34 System.out.println(map); 35 36 System.out.println(map.get("a")); 37 System.out.println(map.get("v")); 38 } 39 /* 40 * 移除集合中的鍵值對(duì),返回被移除的值 41 * V remove(K) 42 */ 43 public static void function_2(){ 44 Map<String,Integer> map = new HashMap<String,Integer>(); 45 map.put("a", 1); 46 map.put("b", 2); 47 map.put("c", 3); 48 System.out.println(map); 49 50 Integer i = map.remove("a"); 51 System.out.println(i); 52 System.out.println(map); 53 } 54 } 1 public class Demo02 { 2 /* 3 * Map集合的遍歷 4 * 利用鍵獲取值 5 * Map接口中定義方法keySet 6 * 所有的鍵,存儲(chǔ)到Set集合 7 */ 8 public static void main(String[] args) { 9 /* 10 * 1.調(diào)用map集合的方法KeySet,所有的鍵存儲(chǔ)到Set集合中 11 * 2.遍歷Set集合,獲取出Set集合中所有的元素(map中的鍵) 12 * 3.調(diào)用map集合方法get,通過鍵獲取值 13 */ 14 Map<String,Integer> map = new HashMap<String,Integer>(); 15 map.put("a1", 11); 16 map.put("a2", 12); 17 map.put("a3", 13); 18 map.put("a4", 14); 19 map.put("a5", 15); 20 21 Set<String> set = map.keySet(); 22 System.out.println(set.getClass()); //java.util.HashMap$KeySet HashMap的內(nèi)部類KeySet 23 Iterator<String> it = set.iterator(); 24 while(it.hasNext()){ 25 //it.next返回的是Map中的鍵 26 String key = it.next(); 27 Integer value = map.get(key); 28 System.out.println(key+":"+value); 29 } 30 System.out.println("==========="); 31 for(String key:set){ 32 Integer value = map.get(key); 33 System.out.println(key+":"+value); 34 } 35 } 36 } 1 public class Demo03 { 2 /* 3 * Map集合獲取方法 4 * entrySet方法,鍵值對(duì)映射關(guān)系 5 * 實(shí)現(xiàn)步驟: 6 * 1.調(diào)用map集合方法entrySet()將集合中的映射關(guān)系對(duì)象,存儲(chǔ)到Set集合 7 * Set<Entry<K,V>> 8 * 2.迭代Set集合 9 * 3.獲取出的Set集合的元素,是映射關(guān)系 10 * 4.通過映射關(guān)系對(duì)象方法getKet,getValue獲取鍵值對(duì) 11 */ 12 public static void main(String[] args) { 13 Map<Integer,String> map = new HashMap<Integer,String>(); 14 map.put(11, "a1"); 15 map.put(12, "a2"); 16 map.put(13, "a3"); 17 map.put(14, "a4"); 18 map.put(15, "a5"); 19 20 Set<Map.Entry<Integer,String>> set = map.entrySet(); 21 Iterator <Map.Entry<Integer,String>> it = set.iterator(); 22 while(it.hasNext()){ 23 //it.next 獲取的是Map.Entry對(duì)象 24 Map.Entry<Integer, String> entry = it.next(); 25 Integer key = entry.getKey(); 26 String value = entry.getValue(); 27 System.out.println(key+":"+value); 28 } 29 System.out.println("======="); 30 for(Map.Entry<Integer, String> entry:map.entrySet()){//增強(qiáng)for不能直接遍歷Map 31 System.out.println(entry.getKey()+":"+entry.getValue()); 32 } 33 } 34 }?LinkedHashMap
1 public class Demo04 { 2 /* 3 * LinkedHashMap繼承HashMap 4 * 保證迭代順序 5 */ 6 public static void main(String[] args) { 7 LinkedHashMap<String,String> link = new LinkedHashMap<String,String>(); 8 link.put("11", "a1"); 9 link.put("12", "a2"); 10 link.put("13", "a3"); 11 link.put("14", "a4"); 12 13 for(String key:link.keySet()){ 14 System.out.println(key+":"+link.get(key)); 15 } 16 } 17 }HashTable
1 /* 2 * Map接口實(shí)現(xiàn)類 Hashtable 3 * 底層數(shù)據(jù)結(jié)果哈希表,特點(diǎn)和HashMap是一樣的 4 * Hashtable 線程安全集合,運(yùn)行速度慢 5 * HashMap 線程不安全的集合,運(yùn)行速度快 6 * 7 * Hashtable命運(yùn)和Vector是一樣的,從JDK1.2開始,被更先進(jìn)的HashMap取代 8 * 9 * HashMap 允許存儲(chǔ)null值,null鍵 10 * Hashtable 不允許存儲(chǔ)null值,null鍵 11 * 12 * Hashtable他的孩子,子類 Properties 依然活躍在開發(fā)舞臺(tái) 13 */ 14 public class HashtableDemo { 15 public static void main(String[] args) { 16 Map<String,String> map = new Hashtable<String,String>(); 17 map.put(null, null); 18 System.out.println(map); 19 } 20 }?
轉(zhuǎn)載于:https://www.cnblogs.com/Nelsoner/p/6691160.html
總結(jié)
- 上一篇: select自定义箭头问题 。。。和一行
- 下一篇: MyEclipse设置文件编码