(一).参考文档:http://cmsblogs.com/?p=176
(二).Map接口
- Map中的元素都是成对出现的,它提供了键(key)到值(vaules)的映射,键决定了元素的存储位置。
- 检索Map中的元素需要通过指定key值,key可以是任意数据类型,但key必须唯一,否则新添加的值会取代已有的值。
- http://www.java2s.com/Tutorials/Java/Java_Collection/0180__Java_Map.htm
(三)Map接口的实现类HashMap
定义:https://baike.so.com/doc/869853-919684.html
HashMap的实现原理:http://blog.csdn.net/vking_wang/article/details/14166593
http://www.importnew.com/7099.html
代码演示:http://www.cnblogs.com/xwdreamer/archive/2012/05/14/2499339.html
1.Key值相同的结果,被后面一个值覆盖
package com.Collection;
import java.util.HashMap;
public class HashMapDemo {
public static void main(String[] args){
HashMap<Integer,String> studentMap = new HashMap<>();
studentMap.put(1,"李四");
studentMap.put(1, "凯耐");
System.out.println(studentMap.get(1));
}
}
2.Key&Values都可以为空值
package com.Collection;
import java.util.HashMap;
public class HashMapDemo {
public static void main(String[] args){
HashMap<Integer,String> studentMap = new HashMap<>();
studentMap.put(1,"李四");
studentMap.put(null, "凯耐");
studentMap.put(1, null);
System.out.println(studentMap.get(1));
}
}
3.删除条目
package com.Collection;
import java.util.HashMap;
public class HashMapDemo {
public static void main(String[] args){
HashMap<Integer,String> studentMap = new HashMap<>();
studentMap.put(1,"李四");
studentMap.put(2, "凯耐");
studentMap.put(3, null);
//通过键值删除条目
System.out.println(studentMap.remove(3));
System.out.println("studentMap"+studentMap);
}
}
清除所有条目使用 Clear()方法
4.在HashMap中检索键、值和键值对
package com.Collection;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class HashMapDemo {
public static void main(String[] args){
HashMap<Integer,String> studentMap = new HashMap<>();
studentMap.put(1,"张三");
studentMap.put(2, "李四");
//1.获取Set对象,Set对象保存的数据为整型
Set<Integer> keys = studentMap.keySet();
//2.获取HashMap的值集合
Collection<String> values = studentMap.values();
//3.获取Map.Entry object
Set<Entry<Integer, String>> entries = studentMap.entrySet();
//4.打印所有的Key值
for(Integer key:keys)
System.out.println(key);
//5.打印所有的值
for(String value:values)
System.out.println(value);
//打印key-value键值对
for(Map.Entry entry : entries)
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
控制台打印结果:
1
2
张三
李四
1 : 张三
2 : 李四
(四)java中Map遍历的四种方法
在java中所有的map都实现了Map接口,因此所有的Map(如HashMap, TreeMap, LinkedHashMap, Hashtable等)都可以用以下的方式去遍历。
方法一:在for循环中使用entries实现Map的遍历:
/**
* 最常见也是大多数情况下用的最多的,一般在键值对都需要使用
*/
Map <String,String>map = new HashMap<String,String>();
map.put("熊大", "棕色");
map.put("熊二", "黄色");
for(Map.Entry<String, String> entry : map.entrySet()){
String mapKey = entry.getKey();
String mapValue = entry.getValue();
System.out.println(mapKey+":"+mapValue);
}
方法二:在for循环中遍历key或者values,一般适用于只需要map中的key或者value时使用,在性能上比使用entrySet较好;
Map <String,String>map = new HashMap<String,String>();
map.put("熊大", "棕色");
map.put("熊二", "黄色");
//key
for(String key : map.keySet()){
System.out.println(key);
}
//value
for(String value : map.values()){
System.out.println(value);
}
方法三:通过Iterator遍历;
Iterator<Entry<String, String>> entries = map.entrySet().iterator();
while(entries.hasNext()){
Entry<String, String> entry = entries.next();
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key+":"+value);
}
方法四:通过键找值遍历,这种方式的效率比较低,因为本身从键取值是耗时的操作;
for(String key : map.keySet()){
String value = map.get(key);
System.out.println(key+":"+value);
}