java?Map集合中取鍵和值的4種方式舉例
1、使用Map的keySet()方法獲取鍵集合,再使用forEach循環(huán)遍歷鍵集合,通過Map的get()方法獲取對(duì)應(yīng)的值。例如:
Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("orange", 3);
// 獲取鍵集合,遍歷鍵集合,通過get()方法獲取對(duì)應(yīng)的值
Set<String> keySet = map.keySet();
for (String key : keySet) {
Integer value = map.get(key);
System.out.println("key:" + key + ",value:" + value);
}
2、使用Map的values()方法獲取值集合,再使用forEach循環(huán)遍歷值集合。例如:
Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("orange", 3);
// 獲取值集合,遍歷值集合
Collection<Integer> values = map.values();
for (Integer value : values) {
System.out.println("value:" + value);
}
3、使用Map的entrySet()方法獲取鍵值對(duì)集合,再使用forEach循環(huán)遍歷鍵值對(duì)集合,通過Entry的getKey()方法獲取鍵,通過Entry的getValue()方法獲取值。例如:
Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("orange", 3);
// 獲取鍵值對(duì)集合,遍歷鍵值對(duì)集合,通過Entry的getKey()方法獲取鍵,通過Entry的getValue()方法獲取值
Set<Entry<String, Integer>> entrySet = map.entrySet();
for (Entry<String, Integer> entry : entrySet) {
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println("key:" + key + ",value:" + value);
}
4、使用Java8的Stream流獲取鍵值對(duì)集合,通過map方法獲取鍵或值的流。例如:
Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("orange", 3);
// 獲取鍵流,遍歷鍵流
map.keySet().stream().forEach(key -> System.out.println("key:" + key));
// 獲取值流,遍歷值流
map.values().stream().forEach(value -> System.out.println("value:" + value));
// 獲取鍵值對(duì)流,遍歷鍵值對(duì)流,通過Entry的getKey()方法獲取鍵,通過Entry的getValue()方法獲取值
map.entrySet().stream().forEach(entry -> System.out.println("key:" + entry.getKey() + ",value:" + entry.getValue()));附:遍歷獲取Map集合中的鍵和值
寫在前面:前段時(shí)間找工作,遇到一道面試題,要求遍歷取出Map集合的鍵和值,沒有要求使用多種方法,做完面試題就想著整理一下,讓自己多多積累。
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Base2 {
/**
*
* @param args
*/
public static void main(String[] args) {
Map<String, Object> map = new HashMap<>();
map.put("1", "張三");
map.put("2", "李四");
map.put("3", "王五");
//方法一:使用keySet()方法將key放入set中
Set<String> set = map.keySet();//先使用set得到map的所有key
Iterator<String> it = set.iterator();//構(gòu)造set的迭代器
while (it.hasNext()) {
String key = it.next();
Object value = map.get(key);
System.out.println("key" + key + "value" + value);
}
//方法二 : 用增強(qiáng)for循環(huán)來代替 iterator
Set<String> set1 = map.keySet();
for (String key1 : set1) {
Object value1 = map.get(key1);
System.out.println("key" + key1 + "value" + value1);
//方法三 :將map的鍵值對(duì)用entrySet方法取出,再放入到set當(dāng)中
Set<Map.Entry<String, Object>> map1 = map.entrySet();
for (Map.Entry<String, Object> maps :
map1) {
String key2 = maps.getKey();
Object Value2 = maps.getValue();
System.out.println("key" + key2 + "value" + Value2);
}
//方法四
Iterator iterator = map.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry maps = ( Map.Entry)iterator.next();
Object key = maps.getKey();
Object value = maps.getValue();
System.out.println("key" + key + "value" + value);
}
}
}}總結(jié)
到此這篇關(guān)于java Map集合中取鍵和值的4種方式的文章就介紹到這了,更多相關(guān)java Map集合取鍵和值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot中整合Minio文件存儲(chǔ)的安裝部署過程
這篇文章主要介紹了SpringBoot整合Minio文件存儲(chǔ)的相關(guān)知識(shí),詳細(xì)介紹了Minio安裝部署過程,需要的朋友可以參考下2022-04-04
SpringMVC核心原理與前后端數(shù)據(jù)交互機(jī)制詳解(推薦)
文章詳細(xì)介紹了SpringMVC的核心原理及前后端數(shù)據(jù)交互機(jī)制,包括SpringMVC的概述、架構(gòu)、工作流程、請(qǐng)求參數(shù)綁定、JSON數(shù)據(jù)處理、數(shù)據(jù)驗(yàn)證、異常處理以及最佳實(shí)踐,感興趣的朋友跟隨小編一起看看吧2025-11-11
如何使用Gradle實(shí)現(xiàn)類似Maven的profiles功能
這篇文章主要介紹了如何使用Gradle實(shí)現(xiàn)類似Maven的profiles功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-06-06
Spring中使用Hystrix實(shí)現(xiàn)熔斷詳解
這篇文章主要介紹了Java中使用Hystrix實(shí)現(xiàn)熔斷詳解,對(duì)于第一個(gè)問題,查看hystrix源碼可以看到,如果有緩存配置是優(yōu)先使用的緩存的,因此如果配置更新,必須要更新緩存,不能使用Hystrix.reset()方法來更新緩存,需要的朋友可以參考下2023-12-12
Java使用Spire.XLS for Java實(shí)現(xiàn)設(shè)置Excel行高列寬
在日常的數(shù)據(jù)處理和報(bào)表生成過程中,Excel 是最常見的文件格式之一,本文將介紹如何通過 Java 使用 Spire.XLS for Java 設(shè)置和調(diào)整 Excel 的行高與列寬,從而實(shí)現(xiàn)高效且美觀的報(bào)表輸出2025-08-08
我總結(jié)的幾種@Transactional失效原因說明
這篇文章主要是我總結(jié)的幾種@Transactional失效原因說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
Java使用ShardingSphere實(shí)現(xiàn)數(shù)據(jù)庫分片的策略指南
隨著業(yè)務(wù)數(shù)據(jù)量的爆炸式增長(zhǎng),數(shù)據(jù)庫分片作為解決大數(shù)據(jù)量存儲(chǔ)和查詢性能問題的核心技術(shù),已成為現(xiàn)代分布式系統(tǒng)架構(gòu)的重要組成部分,ShardingSphere作為一套開源的分布式數(shù)據(jù)庫解決方案,提供了強(qiáng)大的數(shù)據(jù)分片功能,本文將深入探討ShardingSphere的核心分片策略2025-08-08

