• Map的常用API


    • `public V put(K key, V value)`: 把指定的键与指定的值添加到Map集合中。

    • public V remove(Object key): 把指定的键 所对应的键值对元素 在Map集合中删除,返回被删除元素的值。

    • public V get(Object key) 根据指定的键,在Map集合中获取对应的值。

    • boolean containsKey(Object key) 判断集合中是否包含指定的键。

    • public Set<K> keySet(): 获取Map集合中所有的键,存储到Set集合中。

    • public Set<Map.Entry<K,V>> entrySet(): 获取到Map集合中所有的键值对对象的集合(Set集合)。sf 

    1.遍历key

     public static void main(String[] args) {
            //创建Map集合对象 
            HashMap<String, String> map = new HashMap<String,String>();
            //添加元素到集合 
            map.put("胡歌", "霍建华");
            map.put("郭德纲", "于谦");
            map.put("薛之谦", "大张伟");
    
            //获取所有的键  获取键集
            Set<String> keys = map.keySet();
            // 遍历键集 得到 每一个键
            for (String key : keys) {
                  //key  就是键
                //获取对应值
                String value = map.get(key);
                System.out.println(key+"的CP是:"+value);
            }  
        }

     

    1.遍历ke'y和value

       public static void main(String[] args) {
            // 创建Map集合对象 
            HashMap<String, String> map = new HashMap<String,String>();
            // 添加元素到集合 
            map.put("胡歌", "霍建华");
            map.put("郭德纲", "于谦");
            map.put("薛之谦", "大张伟");
    
            // 获取 所有的 entry对象  entrySet
            Set<Entry<String,String>> entrySet = map.entrySet();
    
            // 遍历得到每一个entry对象
            for (Entry<String, String> entry : entrySet) {
                   // 解析 
                String key = entry.getKey();
                String value = entry.getValue();  
                System.out.println(key+"的CP是:"+value);
            }
        }
  • 相关阅读:
    Android防止手动添加的本地库文件被NDK工具清理掉
    将驱动编译进Linux内核
    cocos2d-x入门学习笔记——Hello world分析
    linux内核开发入门学习
    makefile工程管理
    GDB程序调试工具
    ios学习笔记_20140308
    Mac Os学习笔记-下载黑屏
    时间过得好快
    做一个关于预防接种的app
  • 原文地址:https://www.cnblogs.com/liushisaonian/p/11278300.html
Copyright © 2020-2023  润新知