Uso de la interfaz del mapa

package com.sean.base.mapStudy;

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

/**
 * Map接口的使用
 * 特点:(1)存储键值对(2)键不能重复,值可以重复(3)无序
 * entrySet效率高于keySet
 * @create 2021-02-26 8:35
 */
public class Demo01 {
    
    
    public static void main(String[] args) {
    
    
        //创建Map集合
        Map<String, String> map = new HashMap<>();
        //1添加元素
        map.put("cn","中国");
        map.put("uk","英国");
        map.put("un","欧盟");
        map.put("us","美国");

        System.out.println("元素个数:"+ map.size());
        System.out.println(map.toString());
        //2删除操作
        //hashMap.remove("us");
        //System.out.println("删除之后:"+hashMap.size());
        //3遍历
        //3.1使用keySet
        System.out.println("-------使用keySet-------");
        Set<String> keyset= map.keySet();
        for (String string:keyset
             ) {
    
    
            System.out.println(string+"----------"+map.get(string));

        }
        //3.2使用entrySet
        System.out.println("-------3.2使用entrySet-------");
        for (Map.Entry<String,String> entry:map.entrySet()
             ) {
    
    
            System.out.println(entry.getKey()+"-------"+entry.getValue());

        }
        //4判断
        System.out.println(map.containsKey("cn"));
        System.out.println(map.isEmpty());

    }
}

Supongo que te gusta

Origin blog.csdn.net/qq_43021902/article/details/114111910
Recomendado
Clasificación