Map获取值的两种方式:
1.根据map的keyset()方法来获取key的set集合,然后遍历map取得value的值;
2.使用Map.Entry来获取;
实现代码;
package comt.test06.mapsetget;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.junit.Test;
public class MapPut {
/*
* Map添加和获取值
* 方式一:根据map的keyset()方法来获取key的set集合,然后遍历map取得value的值、
*
* maps中的内容:{01=laoyi, 02=laoer, 03=laosan}
* 通过迭代Map,获得的值:key=01 value=laoyi
* 通过迭代Map,获得的值:key=02 value=laoer
* 通过迭代Map,获得的值:key=03 value=laosan
* */
@SuppressWarnings("rawtypes")
@Test
public void mapPut01(){
Map<String, String> maps = new HashMap<String, String>();
maps.put("01", "laoyi");
maps.put("02", "laoer");
maps.put("03", "laosan");
System.out.println("maps中的内容:" + maps);
Set set = maps.keySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()){
String key = (String) iterator.next();
String value = maps.get(key);
System.out.println("通过迭代Map,获得的值:key=" + key + " value="+value);
}
}
/*
* Map添加和获取值
* 方式二:使用Map.Entry来获取
*
* maps中的内容:{01=laoyi, 02=laoer, 03=laosan}
* 通过迭代Map,获得的值:key=01 value=laoyi
* 通过迭代Map,获得的值:key=02 value=laoer
* 通过迭代Map,获得的值:key=03 value=laosan
* */
@SuppressWarnings("rawtypes")
@Test
public void mapPut02(){
Map<String, String> maps = new HashMap<String, String>();
maps.put("01", "laoyi");
maps.put("02", "laoer");
maps.put("03", "laosan");
System.out.println("maps中的内容:" + maps);
Set set = maps.entrySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()){
Map.Entry entry = (Entry) iterator.next();
String key = (String) entry.getKey();
String value = (String) entry.getValue();
System.out.println("通过迭代Map,获得的值:key=" + key + " value="+value);
}
}
}
总结:
两种方式差异进行对比:
方式一:是直接获取到map的key值,在通过key获取value
Set set = maps.keySet();
…
while(iterator.hasNext()){
String key = (String) iterator.next();
String value = maps.get(key);
System.out.println(“通过迭代Map,获得的值:key=” + key + " value="+value);
}
源代码说明:
/**
* Returns a {@link Set} view of the keys contained in this map.
* The set is backed by the map, so changes to the map are
* reflected in the set, and vice-versa. If the map is modified
* while an iteration over the set is in progress (except through
* the iterator’s own remove operation), the results of
* the iteration are undefined. The set supports element removal,
* which removes the corresponding mapping from the map, via the
* Iterator.remove, Set.remove,
* removeAll, retainAll, and clear
* operations. It does not support the add or addAll
* operations.
*
* @return a set view of the keys contained in this map
*/
Set keySet();
方式二:是直接获取到map的key,value键值对
Set set = maps.entrySet();
…
while(iterator.hasNext()){
Map.Entry entry = (Entry) iterator.next();
String key = (String) entry.getKey();
String value = (String) entry.getValue();
System.out.println("通过迭代Map,获得的值:key=" + key + " value="+value);
}
源代码说明:
/**
* Returns a {@link Set} view of the mappings contained in this map.
* The set is backed by the map, so changes to the map are
* reflected in the set, and vice-versa. If the map is modified
* while an iteration over the set is in progress (except through
* the iterator’s own remove operation, or through the
* setValue operation on a map entry returned by the
* iterator) the results of the iteration are undefined. The set
* supports element removal, which removes the corresponding
* mapping from the map, via the Iterator.remove,
* Set.remove, removeAll, retainAll and
* clear operations. It does not support the
* add or addAll operations.
*
* @return a set view of the mappings contained in this map
*/
Set<Map.Entry<K, V>> entrySet();