初始化方法
(1) HashMap<String, String> map = new HashMap<String, String>(); (2) HashMap<String, String> map = new HashMap<String, String>(){ { put(String, String); } };
import java.util.*; public class NewTips { public static void main(String[] args) { HashMap<String, Integer> data1 = new HashMap<>(); data1.put("china", 20); data1.put("python", 30); data1.put("c++", 40); System.out.println("显示data1: " + data1); HashMap<String, Integer> data2 = new HashMap<>(){ { put("chian", 22); put("python", 33); put("c++", 44); } }; System.out.println("显示data2: " + data1); } }
基本使用方法
(1) 插入键值对数据 public V put(K key, V value)
(2)根据键值获取键值对值数据 public V get(Object key)
(3)获取Map中键值对的个数 public int size()
(4)判断Map集合中是否包含键为key的键值对 public boolean containsKey(Object key)
(5)判断Map集合中是否包含值为value的键值对 boolean containsValue(Object value)
(6)判断Map集合中是否没有任何键值对 public boolean isEmpty()
(7)清空Map集合中所有的键值对 public void clear()
(8)根据键值删除Map中键值对 public V remove(Object key)
import java.util.HashMap; public class NewTips { public static void main(String[] args) { HashMap<String, Integer> data = new HashMap<String, Integer>(); // public V put(K key, V value) 插入键值对数据 System.out.print("显示put: "); data.put("china", 20); data.put("python", 30); data.put("c++", 40); System.out.println(data); // public V get(Object Key) 根据key获取value的值 System.out.print("显示get: "); System.out.println(data.get("china")); // public int size() 获取键值对个数 System.out.print("显示size: "); System.out.println(data.size()); // public boolean containsKey(Object key) 判断集合中是否包含键为key的键值对 System.out.print("显示containsKey: "); System.out.println(data.containsKey("china")); // boolean containsValue(Object value) 判断map集合中是否包含值为value值的键值对 System.out.print("显示containsValue: "); System.out.println(data.containsValue(20)); // public boolean isEmpty() 判断集合中是否没有任何的键值对 System.out.print("显示isEmpty: "); System.out.println(data.isEmpty()); // public V remove(Object Key) 根据键值删除map中键值对 System.out.print("显示remove: "); System.out.println(data.remove("python")); System.out.println(data); // public void clear() 清空map集合中所有的键值对 System.out.print("显示clear: "); data.clear(); System.out.println(data); } }
键值对遍历
(1)将Map中所有的键装到Set集合中返回 public Set<K> keySet(); Set<K> set = map. keySet()
(2)返回集合中所有的value的值的集合 public Collection<V> values(); Collection<V> c = map.values()
(3)将每个键值对封装到一个个Entry对象中,再把所有Entry的对象封装到Set集合中返回 public Set<Map.Entry<K,V>> entrtSet(); Set<Map.Entry<K,V>> entrys = map.entrySet()
import java.util.Collection; import java.util.HashMap; import java.util.Set; import java.util.Map; public class NewTips { public static void main(String[] args) { HashMap<String, Integer> data = new HashMap<>(); data.put("china", 20); data.put("python", 30); data.put("c++", 40); System.out.println("显示data值: "+data); System.out.println("----------------"); // public Set<K> keySet() 将map中所有的key装到Set集合中返回 // Set<type> set = map.keySet(); 用法 Set<String> keys = data.keySet(); System.out.println("显示所有keys: "+keys); for(String key : keys){ System.out.println("key:"+key+" value:"+data.get(key)); } System.out.println("----------------"); // public Collection<K> values() 返回集合中所有的Value值集合 // Collection<type> c = map.values() Collection<Integer> values = data.values(); System.out.println("显示所有value: "+values); for (int value : values){ System.out.println("value:"+value); } System.out.println("----------------"); // public Set<Map.Entry<K,V>> entrySet() // 将每个键值对封装到一个个Entry对象中,再将所有的Entry对象封装到set集合中 // Set<Map.Entry<K,V>> entrys = map.entrySet() Set<Map.Entry<String, Integer>> entries = data.entrySet(); for(Map.Entry<String, Integer> entry : entries) { System.out.println("键值对: "+entry); System.out.println("key: "+entry.getKey()+" value:"+entry.getValue()); } } }
数据遍历
(1)for-each 遍历entry (2)keySet 迭代器遍历 (3)entrySet 迭代器遍历 不使用泛型 (4)entrySet 迭代器遍历 使用泛型 (5)keySet 只获取key (6)values 只获取valus
import java.util.*; public class NewTips { public static void main(String[] args) { HashMap<String, Integer> data = new HashMap<>(); data.put("china", 20); data.put("python", 30); data.put("c++", 40); System.out.println("显示data值: " + data); // for-each System.out.println("方法一:-------for-each---------"); for(Map.Entry<String, Integer> entry : data.entrySet()){ String key = entry.getKey(); int val = entry.getValue(); System.out.println("key:"+key+" value:"+val); } // 效率低 System.out.println("方法二:--------keySet--------"); Iterator iter1 = data.keySet().iterator(); while (iter1.hasNext()){ Object key = iter1.next(); Object val = data.get(key); System.out.println("key:"+key+" value:"+val); } // 不使用泛型 System.out.println("方法三:-------entrySet-不使用泛型--------"); Iterator iter2 = data.entrySet().iterator(); while (iter2.hasNext()) { Map.Entry entry = (Map.Entry) iter2.next(); Object key = entry.getKey(); Object val = entry.getValue(); System.out.println("key:" + key + " value:" + val); } // 使用泛型 System.out.println("方法四:-------entrySet-使用泛型--------"); Iterator<Map.Entry<String, Integer>> iter3 = data.entrySet().iterator(); while (iter3.hasNext()) { Map.Entry<String, Integer> entry = iter3.next(); Object key = entry.getKey(); Object val = entry.getValue(); System.out.println("key:" + key + " value:" + val); } // 只获取key System.out.println("方法五:-------keySet---------"); for(String key : data.keySet()){ System.out.println("key: "+key); } // 只获取val System.out.println("方法六:-------values---------"); for(Integer value : data.values()){ System.out.println("value: "+value); } } }