Java基础知识强化之集合框架笔记51:Map集合之Map集合的功能概述与测试

1. Map集合的功能概述

(1)添加功能
 V put(K key,V value)添加元素。这个其实还有另一个功能?先不告诉你,等会讲
 如果键是第一次存储,就直接存储元素,返回null
 如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值


(2)删除功能
 void clear()移除所有的键值对元素
 V remove(Object key):根据键删除键值对元素,并把值返回


(3)判断功能
 boolean containsKey(Object key):判断集合是否包含指定的键
 boolean containsValue(Object value)判断集合是否包含指定的值
 boolean isEmpty():判断集合是否为空


(4)获取功能
 Set<Map.Entry<K,V>> entrySet()???
 V get(Object key)根据键获取值
 Set<K> keySet()获取集合中所有键的集合
 Collection<V> values()获取集合中所有值的集合


(5)长度功能
 int size():返回集合中的键值对的对数

2. 基本功能测试:

 1 package cn.itcast_01;
 2 
 3 import java.util.HashMap;
 4 import java.util.Map;
 5 
 6 public class MapDemo {
 7     public static void main(String[] args) {
 8         // 创建集合对象
 9         Map<String, String> map = new HashMap<String, String>();
10 
11         // 添加元素
12         // V put(K key,V value):添加元素。这个其实还有另一个功能?先不告诉你,等会讲
13         // System.out.println("put:" + map.put("文章", "马伊俐"));
14         // System.out.println("put:" + map.put("文章", "姚笛"));
15 
16         map.put("邓超", "孙俪");
17         map.put("黄晓明", "杨颖");
18         map.put("周杰伦", "蔡依林");
19         map.put("刘恺威", "杨幂");
20 
21         // void clear():移除所有的键值对元素
22         // map.clear();
23 
24         // V remove(Object key):根据键删除键值对元素,并把值返回
25         // System.out.println("remove:" + map.remove("黄晓明"));
26         // System.out.println("remove:" + map.remove("黄晓波"));
27 
28         // boolean containsKey(Object key):判断集合是否包含指定的键
29         // System.out.println("containsKey:" + map.containsKey("黄晓明"));
30         // System.out.println("containsKey:" + map.containsKey("黄晓波"));
31 
32         // boolean isEmpty():判断集合是否为空
33         // System.out.println("isEmpty:"+map.isEmpty());
34         
35         //int size():返回集合中的键值对的对数
36         System.out.println("size:"+map.size());
37 
38         // 输出集合名称
39         System.out.println("map:" + map);
40     }
41 }

3. 获取功能测试:

 1 package cn.itcast_01;
 2 
 3 import java.util.Collection;
 4 import java.util.HashMap;
 5 import java.util.Map;
 6 import java.util.Set;
 7 
 8 /*
 9  * 获取功能:
10  * V get(Object key):根据键获取值
11  * Set<K> keySet():获取集合中所有键的集合
12  * Collection<V> values():获取集合中所有值的集合
13  */
14 public class MapDemo2 {
15     public static void main(String[] args) {
16         // 创建集合对象
17         Map<String, String> map = new HashMap<String, String>();
18 
19         // 创建元素并添加元素
20         map.put("邓超", "孙俪");
21         map.put("黄晓明", "杨颖");
22         map.put("周杰伦", "蔡依林");
23         map.put("刘恺威", "杨幂");
24 
25         // V get(Object key):根据键获取值
26         System.out.println("get:" + map.get("周杰伦"));
27         System.out.println("get:" + map.get("周杰")); // 返回null
28         System.out.println("----------------------");
29 
30         // Set<K> keySet():获取集合中所有键的集合
31         Set<String> set = map.keySet();
32         for (String key : set) {
33             System.out.println(key);
34         }
35         System.out.println("----------------------");
36 
37         // Collection<V> values():获取集合中所有值的集合
38         Collection<String> con = map.values();
39         for (String value : con) {
40             System.out.println(value);
41         }
42     }
43 }

运行效果,如下:

原文地址:https://www.cnblogs.com/hebao0514/p/4864890.html