Map获取值的两种方式:keyset()和使用Map.Entry

本文详细介绍了在Java中使用HashMap的两种常见方法来获取Map中的值。第一种方法是通过获取Map的keySet并遍历获取value;第二种方法是利用Map.Entry遍历获取键值对。文章提供了具体的代码示例,帮助读者理解两种方法的实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值