0
点赞
收藏
分享

微信扫一扫

Java学习Map之Map集合的获取功能

M4Y 2022-06-07 阅读 70

方法名

说明

V get(Object key)

根据键获取值

Set<K> KeySet()

获取所有键的集合

Collection<V>values()

获取所有值的集合

Set<Map.Entry<K,V>>entrySet()

获取所有键值对对象的集合

package com.ithema_24;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/*
V get(Object key)根据键获取值
Set<K> KeySet()获取所有键的集合
Collection<V>values()获取所有值的集合
Set<Map.Entry<K,V>>entrySet()获取所有键值对对象的集合
*/
public class MapDemo03 {
public static void main(String[] args) {
//创建集合对象
Map<String,String> map = new HashMap<String,String>();
//添加元素
map.put("李薇","王昱翔");
map.put("王顺","王亚争");
map.put("李林","张盼");

//V get(Object key)根据键获取值
// System.out.println(map.get("李薇"));
// System.out.println(map.get("王顺"));

//Set<K> KeySet()获取所有键的集合
// Set<String> keyset = map.keySet();
// for(String key:keyset){
// System.out.println(key);
// }
//Collection<V>values()获取所有值的集合
Collection<String> values = map.values();
for(String value:values){
System.out.println(value);
}
//Set<Map.Entry<K,V>>entrySet()获取所有键值对对象的集合
Set<Map.Entry<String,String>> entryset = map.entrySet();
for(Map.Entry<String,String> entry:entryset){
System.out.println(entry.getKey() + "=" + entry.getValue());
}

}
}

举报

相关推荐

0 条评论