標籤:
1 package cn.itcast_01; 2 3 import java.util.Collection; 4 import java.util.HashMap; 5 import java.util.Map; 6 import java.util.Set; 7 8 /* 9 * 擷取功能:10 * V get(Object key):根據鍵擷取值11 * Set<K> keySet():擷取集合中所有鍵的集合12 * Collection<V> values():擷取集合中所有值的集合13 */14 public class MapDemo2 {15 public static void main(String[] args) {16 // 建立集合對象17 Map<String, String> map = new HashMap<String, String>();18 19 // 建立元素並添加元素20 map.put("鄧超", "孫儷");21 map.put("黃曉明", "楊穎");22 map.put("周杰倫", "蔡依林");23 map.put("劉愷威", "楊冪");24 25 // V get(Object key):根據鍵擷取值26 System.out.println("get:" + map.get("周杰倫"));27 System.out.println("get:" + map.get("周傑")); // 返回null28 System.out.println("----------------------");29 30 // Set<K> keySet():擷取集合中所有鍵的集合31 Set<String> set = map.keySet();32 for (String key : set) {33 System.out.println(key);34 }35 System.out.println("----------------------");36 37 // Collection<V> values():擷取集合中所有值的集合38 Collection<String> con = map.values();39 for (String value : con) {40 System.out.println(value);41 }42 }43 }
Android(java)學習筆記103:Map集合的擷取功能