Java中的Map

Map5.1Map集合的概述和使用5.1.15.2Map集合的基本功能5.3 Map集合的获取功能5.4Map集合的遍历(方式1:将键获取到一个集合,再将键的集合遍历获取对应的值)5.5 Map集合的遍历(方式2)5.1Map集合的概述和使用Interface Map<K,V>K:Map集合中key的类型V:Map集合中value的类型All Known Implementing Classes:AbstractMap, Attributes, AuthProvider, Concu
摘要由CSDN通过智能技术生成

5.1Map集合的概述和使用

Interface Map<K,V>
K:Map集合中key的类型
V:Map集合中value的类型
All Known Implementing Classes:
AbstractMap, Attributes, AuthProvider, ConcurrentHashMap, ConcurrentSkipListMap, EnumMap, HashMap, Hashtable, Headers, IdentityHashMap, LinkedHashMap, PrinterStateReasons, Properties, Provider, RenderingHints, SimpleBindings, TabularDataSupport, TreeMap, UIDefaults, WeakHashMap

public interface Map<K,V> {}
1.Interface Map<K,V>
K:Map集合中key的类型
V:Map集合中value的类型

2.将键映射到值得对象,Map不能包含重复的键,每个键可以映射到最多一个值

3.举例:学生的学号和姓名
student1 “anna”
student2 “tom”
student3 “we”

创建Map结合的对象:
因为Map是接口,不能直接创建对象,所以我们使用多态的方式来创建.在这里我们可以创建实现类HashMap的对象

5.1.1HashMap的put方法

举例:学生的学号和姓名
student1 “anna”
student2 “tom”
student3 “we”

HashMap的元素的添加我们使用put方法,注意如果key一次出现时是添加此元素,如果key第二次出现时,是修改元素.

import java.util.HashMap;
import java.util.Map;

public class MapDemo {
   
    public static void main(String[] args) {
   
        Map<String,String> map=new HashMap<>();
        map.put("student1","anna");
        map.put("student2","tom");
        map.put("student3","we");
        map.put("student3","change");

        System.out.println(map);
    }
}

输出结果:

{
   student2=tom, student1=anna, student3=change}

注意:
这里的HashMap重写了toString方法,这里的输出结果用=号连接.
key第二次出现时,value会覆盖.

5.2 Map集合的基本功能

方法名 说明
V put(K key,V value) 添加元素
V remove(Object key) 根据键删除键值对元素
void clear() 删除Map集合中所有的元素
boolean containsKey(Object value) 判断集合是否包含指定的值
boolean isEmpty() 判断集合是否为空
int size() 集合的长度,也就是集合中键值对的个数
import java.util.HashMap;
import java.util.Map;

public class MapDemo01 {
   
    public static void main(String[] args) {
   
        Map<String,String> m=new HashMap<String,String>();

        m.put("zwj","zm");
        m.put("gj","hr");
        m.put("yg","xln");

        m.remove("zwj");
        System.out.println(m);
        System.out.println(m.containsKey("gj"));
        System.out.println(m.containsValue("zm"));
        System.out.println(m.isEmpty());
        System.out.println(m.size());
        m.clear();
        System.out.println(m.size());

    }
}

输出结果:

{
   gj=hr, yg=xln}
true
false
false
2
0

5.3 Map集合的获取功能

方法名 说明
V get(Object key) 根据键获取值
SetkeySet() 获取所有键的集合
Collectionvalues() 获取所有值的集合
Set<Map.Entry<K,V>>entrySet() 获取所有键值对象的集合
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MapDemo02 {
   
    public static void main(String[] args) {
   
        Map<String,String> map=new HashMap<String,String>();
        map.put("first","anan");
        map.put("second","bob");
        map.put("third","cow");
        map.put("fourth","dead");

        System.out.println(map.get("fourth"));
        System.out.println("-----");
        System.out.println(map.keySet());
        Set<String> ketSet=map.keySet();
        for(String s:ketSet){
   
            System.out.println(s);
        }
        System.out.println("-----");

        System.out.println(map.values());
        Collection<
  • 20
    点赞
  • 116
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值